File tree 1 file changed +13
-16
lines changed
ruby/producer-consumer-model 1 file changed +13
-16
lines changed Original file line number Diff line number Diff line change 3
3
# ref: https://jyywiki.cn/OS/2023/build/lect9.ipynb
4
4
5
5
# Q: what's the difference between `MUTEX.lock {}` and `MUTEX.lock + MUTEX.unlock` ?
6
+ # A: it should be `MUTEX.synchronize {}`
6
7
# Q: what's the meaning of @quota?
7
8
8
9
FSM = [
@@ -34,26 +35,22 @@ def can_print?(event)
34
35
end
35
36
36
37
def fish_before ( event )
37
- MUTEX . lock
38
-
39
- while !can_print? ( event ) do
40
- COND_VAR . wait ( MUTEX )
38
+ MUTEX . synchronize do
39
+ while !can_print? ( event ) do
40
+ COND_VAR . wait ( MUTEX )
41
+ end
42
+ @quota -= 1
41
43
end
42
- @quota -= 1
43
-
44
- MUTEX . unlock
45
44
end
46
45
47
46
def fish_after ( event )
48
- MUTEX . lock
49
-
50
- @quota += 1
51
- @current_state = next_state ( event )
52
- # assert @current_state
53
- raise "invalid transition, current_state: #{ @current_state } , event: #{ event } " if @current_state . nil?
54
- COND_VAR . broadcast
55
-
56
- MUTEX . unlock
47
+ MUTEX . synchronize do
48
+ @quota += 1
49
+ @current_state = next_state ( event )
50
+ # assert @current_state
51
+ raise "invalid transition, current_state: #{ @current_state } , event: #{ event } " if @current_state . nil?
52
+ COND_VAR . broadcast
53
+ end
57
54
end
58
55
59
56
def fish_thread ( char )
You can’t perform that action at this time.
0 commit comments