@@ -26,6 +26,7 @@ end = struct
26
26
and not_empty = Condition. create ()
27
27
and not_full = Condition. create () in
28
28
{ mutex; queue; capacity; not_empty; not_full }
29
+ |> Multicore_magic. copy_as_padded
29
30
30
31
let is_empty t =
31
32
Mutex. lock t.mutex;
@@ -36,33 +37,39 @@ end = struct
36
37
let is_full_unsafe t = t.capacity < = Queue. length t.queue
37
38
38
39
let push t x =
40
+ let was_full = ref false in
39
41
Mutex. lock t.mutex;
40
42
match
41
43
while is_full_unsafe t do
44
+ was_full := true ;
42
45
Condition. wait t.not_full t.mutex
43
46
done
44
47
with
45
48
| () ->
46
49
Queue. push x t.queue;
47
50
let n = Queue. length t.queue in
48
51
Mutex. unlock t.mutex;
49
- if n = 1 then Condition. broadcast t.not_empty
52
+ if n = 1 then Condition. signal t.not_empty;
53
+ if ! was_full && n < t.capacity then Condition. signal t.not_full
50
54
| exception exn ->
51
55
Mutex. unlock t.mutex;
52
56
raise exn
53
57
54
58
let pop t =
59
+ let was_empty = ref false in
55
60
Mutex. lock t.mutex;
56
61
match
57
62
while Queue. length t.queue = 0 do
63
+ was_empty := true ;
58
64
Condition. wait t.not_empty t.mutex
59
65
done
60
66
with
61
67
| () ->
62
68
let n = Queue. length t.queue in
63
69
let elem = Queue. pop t.queue in
64
70
Mutex. unlock t.mutex;
65
- if n = t.capacity then Condition. broadcast t.not_full;
71
+ if n = t.capacity then Condition. signal t.not_full;
72
+ if ! was_empty && 1 < n then Condition. signal t.not_empty;
66
73
elem
67
74
| exception exn ->
68
75
Mutex. unlock t.mutex;
@@ -73,7 +80,7 @@ end = struct
73
80
let n = Queue. length t.queue in
74
81
let elem_opt = Queue. take_opt t.queue in
75
82
Mutex. unlock t.mutex;
76
- if n = t.capacity then Condition. broadcast t.not_full;
83
+ if n = t.capacity then Condition. signal t.not_full;
77
84
elem_opt
78
85
end
79
86
0 commit comments