mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 19:55:31 -05:00
small refactor
This commit is contained in:
parent
f53b19545c
commit
7d8369ab83
1 changed files with 17 additions and 28 deletions
|
|
@ -12,6 +12,15 @@ let spawn2 f x y = Thread.create (fun () -> f x y) ()
|
||||||
|
|
||||||
let detach f = ignore (Thread.create f ())
|
let detach f = ignore (Thread.create f ())
|
||||||
|
|
||||||
|
let finally_ f x ~h =
|
||||||
|
try
|
||||||
|
let res = f x in
|
||||||
|
ignore (h ());
|
||||||
|
res
|
||||||
|
with e ->
|
||||||
|
ignore (h());
|
||||||
|
raise e
|
||||||
|
|
||||||
module Arr = struct
|
module Arr = struct
|
||||||
let spawn n f =
|
let spawn n f =
|
||||||
Array.init n (fun i -> Thread.create f i)
|
Array.init n (fun i -> Thread.create f i)
|
||||||
|
|
@ -42,13 +51,7 @@ module Barrier = struct
|
||||||
|
|
||||||
let with_lock_ b f =
|
let with_lock_ b f =
|
||||||
Mutex.lock b.lock;
|
Mutex.lock b.lock;
|
||||||
try
|
finally_ f () ~h:(fun () -> Mutex.unlock b.lock)
|
||||||
let x = f () in
|
|
||||||
Mutex.unlock b.lock;
|
|
||||||
x
|
|
||||||
with e ->
|
|
||||||
Mutex.unlock b.lock;
|
|
||||||
raise e
|
|
||||||
|
|
||||||
let reset b = with_lock_ b (fun () -> b.activated <- false)
|
let reset b = with_lock_ b (fun () -> b.activated <- false)
|
||||||
|
|
||||||
|
|
@ -57,17 +60,14 @@ module Barrier = struct
|
||||||
(fun () ->
|
(fun () ->
|
||||||
while not b.activated do
|
while not b.activated do
|
||||||
Condition.wait b.cond b.lock
|
Condition.wait b.cond b.lock
|
||||||
done
|
done)
|
||||||
)
|
|
||||||
|
|
||||||
let activate b =
|
let activate b =
|
||||||
with_lock_ b
|
with_lock_ b
|
||||||
(fun () ->
|
(fun () ->
|
||||||
if not b.activated then (
|
if not b.activated then (
|
||||||
b.activated <- true;
|
b.activated <- true;
|
||||||
Condition.broadcast b.cond
|
Condition.broadcast b.cond))
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
let activated b = with_lock_ b (fun () -> b.activated)
|
let activated b = with_lock_ b (fun () -> b.activated)
|
||||||
end
|
end
|
||||||
|
|
@ -109,13 +109,7 @@ module Queue = struct
|
||||||
|
|
||||||
let with_lock_ q f =
|
let with_lock_ q f =
|
||||||
Mutex.lock q.lock;
|
Mutex.lock q.lock;
|
||||||
try
|
finally_ f () ~h:(fun () -> Mutex.unlock q.lock)
|
||||||
let x = f () in
|
|
||||||
Mutex.unlock q.lock;
|
|
||||||
x
|
|
||||||
with e ->
|
|
||||||
Mutex.unlock q.lock;
|
|
||||||
raise e
|
|
||||||
|
|
||||||
let push q x =
|
let push q x =
|
||||||
with_lock_ q
|
with_lock_ q
|
||||||
|
|
@ -127,8 +121,7 @@ module Queue = struct
|
||||||
Queue.push x q.q;
|
Queue.push x q.q;
|
||||||
(* if there are blocked receivers, awake one of them *)
|
(* if there are blocked receivers, awake one of them *)
|
||||||
incr_size_ q;
|
incr_size_ q;
|
||||||
Condition.broadcast q.cond;
|
Condition.broadcast q.cond)
|
||||||
)
|
|
||||||
|
|
||||||
let take q =
|
let take q =
|
||||||
with_lock_ q
|
with_lock_ q
|
||||||
|
|
@ -140,8 +133,7 @@ module Queue = struct
|
||||||
(* if there are blocked senders, awake one of them *)
|
(* if there are blocked senders, awake one of them *)
|
||||||
decr_size_ q;
|
decr_size_ q;
|
||||||
Condition.broadcast q.cond;
|
Condition.broadcast q.cond;
|
||||||
x
|
x)
|
||||||
)
|
|
||||||
|
|
||||||
(*$R
|
(*$R
|
||||||
let q = Queue.create 1 in
|
let q = Queue.create 1 in
|
||||||
|
|
@ -179,8 +171,7 @@ module Queue = struct
|
||||||
done;
|
done;
|
||||||
let l = push_ q l in
|
let l = push_ q l in
|
||||||
Condition.broadcast q.cond;
|
Condition.broadcast q.cond;
|
||||||
l
|
l)
|
||||||
)
|
|
||||||
in
|
in
|
||||||
aux q l
|
aux q l
|
||||||
in aux q l
|
in aux q l
|
||||||
|
|
@ -266,9 +257,7 @@ module Queue = struct
|
||||||
|
|
||||||
let peek q =
|
let peek q =
|
||||||
with_lock_ q
|
with_lock_ q
|
||||||
(fun () ->
|
(fun () -> try Some (Queue.peek q.q) with Queue.Empty -> None)
|
||||||
try Some (Queue.peek q.q) with Queue.Empty -> None
|
|
||||||
)
|
|
||||||
|
|
||||||
let size q = with_lock_ q (fun () -> q.size)
|
let size q = with_lock_ q (fun () -> q.size)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue