This repository was archived by the owner on Nov 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,6 @@ class << self
17
17
# @private
18
18
class ReentrantMutex
19
19
def initialize
20
- @owner = nil
21
20
@count = 0
22
21
@mutex = Mutex . new
23
22
end
@@ -32,16 +31,16 @@ def synchronize
32
31
private
33
32
34
33
def enter
35
- @mutex . lock if @owner != Thread . current
36
- @owner = Thread . current
34
+ @mutex . lock unless @mutex . owned?
37
35
@count += 1
38
36
end
39
37
40
38
def exit
39
+ unless @mutex . owned?
40
+ raise ThreadError , "Attempt to unlock a mutex which is locked by another thread/fiber"
41
+ end
41
42
@count -= 1
42
- return unless @count == 0
43
- @owner = nil
44
- @mutex . unlock
43
+ @mutex . unlock if @count == 0
45
44
end
46
45
end
47
46
end
Original file line number Diff line number Diff line change 34
34
mutex . synchronize { order . pass_to :thread , :resume_on => :sleep }
35
35
order . join_all
36
36
end
37
+
38
+ if RUBY_VERSION >= '3.0'
39
+ it 'waits when trying to lock from another Fiber' do
40
+ mutex . synchronize do
41
+ ready = false
42
+ f = Fiber . new do
43
+ expect {
44
+ ready = true
45
+ mutex . send ( :enter )
46
+ raise 'should reach here: mutex is already locked on different Fiber'
47
+ } . to raise_error ( Exception , 'waited correctly' )
48
+ end
49
+
50
+ main_thread = Thread . current
51
+
52
+ t = Thread . new do
53
+ Thread . pass until ready and main_thread . stop?
54
+ main_thread . raise Exception , 'waited correctly'
55
+ end
56
+ f . resume
57
+ t . join
58
+ end
59
+ end
60
+ end
37
61
end
You can’t perform that action at this time.
0 commit comments