File tree Expand file tree Collapse file tree 1 file changed +33
-16
lines changed Expand file tree Collapse file tree 1 file changed +33
-16
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,16 @@ class ThreadError<StandardError
17
17
Thread . abort_on_exception = true
18
18
end
19
19
20
+ def Thread . exclusive
21
+ begin
22
+ Thread . critical = true
23
+ r = yield
24
+ ensure
25
+ Thread . critical = false
26
+ end
27
+ r
28
+ end
29
+
20
30
class Mutex
21
31
def initialize
22
32
@waiting = [ ]
@@ -68,39 +78,46 @@ def synchronize
68
78
unlock
69
79
end
70
80
end
81
+
82
+ def exclusive_unlock
83
+ return unless @locked
84
+ Thread . exclusive do
85
+ t = @waiting . shift
86
+ @locked = false
87
+ t . wakeup if t
88
+ yield
89
+ end
90
+ self
91
+ end
71
92
end
72
93
73
94
class ConditionVariable
74
95
def initialize
75
96
@waiters = [ ]
76
- @waiters_mutex = Mutex . new
77
- @waiters . taint # enable tainted comunication
78
- self . taint
79
97
end
80
98
81
99
def wait ( mutex )
82
- mutex . unlock
83
- @waiters_mutex . synchronize {
100
+ mutex . exclusive_unlock do
84
101
@waiters . push ( Thread . current )
85
- }
86
- Thread . stop
102
+ Thread . stop
103
+ end
87
104
mutex . lock
88
105
end
89
106
90
107
def signal
91
- @waiters_mutex . synchronize {
92
- t = @waiters . shift
93
- t . run if t
94
- }
108
+ t = @waiters . shift
109
+ t . run if t
95
110
end
96
111
97
112
def broadcast
98
- @waiters_mutex . synchronize {
99
- for t in @waiters
100
- t . run
101
- end
113
+ waitors0 = nil
114
+ Thread . exclusive do
115
+ waiters0 = @waitors . dup
102
116
@waiters . clear
103
- }
117
+ end
118
+ for t in waiters0
119
+ t . run
120
+ end
104
121
end
105
122
end
106
123
You can’t perform that action at this time.
0 commit comments