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 @@ -13,7 +13,6 @@ module Support
13
13
# @private
14
14
class ReentrantMutex
15
15
def initialize
16
- @owner = nil
17
16
@count = 0
18
17
@mutex = Mutex . new
19
18
end
@@ -28,16 +27,16 @@ def synchronize
28
27
private
29
28
30
29
def enter
31
- @mutex . lock if @owner != Thread . current
32
- @owner = Thread . current
30
+ @mutex . lock unless @mutex . owned?
33
31
@count += 1
34
32
end
35
33
36
34
def exit
35
+ unless @mutex . owned?
36
+ raise ThreadError , "Attempt to unlock a mutex which is locked by another thread/fiber"
37
+ end
37
38
@count -= 1
38
- return unless @count == 0
39
- @owner = nil
40
- @mutex . unlock
39
+ @mutex . unlock if @count == 0
41
40
end
42
41
end
43
42
Original file line number Diff line number Diff line change 27
27
mutex . synchronize { order . pass_to :thread , :resume_on => :sleep }
28
28
order . join_all
29
29
end
30
+
31
+ if RUBY_VERSION >= '3.0'
32
+ it 'waits when trying to lock from another Fiber' do
33
+ mutex . synchronize do
34
+ ready = false
35
+ f = Fiber . new do
36
+ expect {
37
+ ready = true
38
+ mutex . send ( :enter )
39
+ raise 'should reach here: mutex is already locked on different Fiber'
40
+ } . to raise_error ( Exception , 'waited correctly' )
41
+ end
42
+
43
+ main_thread = Thread . current
44
+
45
+ t = Thread . new do
46
+ Thread . pass until ready and main_thread . stop?
47
+ main_thread . raise Exception , 'waited correctly'
48
+ end
49
+ f . resume
50
+ t . join
51
+ end
52
+ end
53
+ end
30
54
end
You can’t perform that action at this time.
0 commit comments