Project

General

Profile

Actions

Bug #11496

closed

Mutex#synchronize testing code should be data race.

Added by ksss (Yuki Kurihara) almost 7 years ago. Updated almost 7 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.3.0dev (2015-08-28 trunk 51715) [x86_64-darwin14]
[ruby-dev:<unknown>]

Description

Current Mutex#synchronize testing code is not to fail every time if not use Mutex#synchronize.

100.times {
  r = 0
  max = 10
  (1..max).map{
    Thread.new{
      i=0
      while i<max*max
        i+=1
        r += 1
      end
    }
  }.each{|e|
    e.join
  }
  raise unless r == 1000
}
puts "pass"

So, I propose this patch.


Files

mutex-synchronize.patch (424 Bytes) mutex-synchronize.patch ksss (Yuki Kurihara), 08/31/2015 01:52 AM
Actions #1

Updated by kosaki (Motohiro KOSAKI) almost 7 years ago

inserting sleep is unacceptable because sleep invokes volunteer context switch and changes a meaning and a purpose of the test.
Instead, I'd like to insert a waste loop to trigger a thread preemption race. like this

100.times {
m = Mutex.new
r = 0
num_threads = 10
loop=100
(1..num_threads).map{
Thread.new do
loop.times do
m.synchronize{
tmp = r
100.times { # waste loop
}
r = tmp + 1
}
end
end
}.each{|e|
e.join
}
raise unless r == num_threads*loop
}
puts "pass"

Actions #2

Updated by kosaki (Motohiro KOSAKI) almost 7 years ago

  • Status changed from Open to Closed
  • Assignee set to kosaki (Motohiro KOSAKI)

committed at r51868.

Actions

Also available in: Atom PDF