Hi
The Cyclicbarrier (The code is here. ) is ported from Go. It is an exercise as I am aware that concurrency primitives are notoriously difficult to create.
But my simple test seems to work.
The await function which is the main driver is this.
let await b =
try Fiber.check();
let captured_round_result =
Eio.Mutex.use_rw ~protect:true b.lock @@
fun () ->
let capture_round = b.round in
if capture_round.is_broken then(
raise ( Barrier_breached "await\n" )
)
else
capture_round.count <- capture_round.count + 1;
let c = capture_round.count in
Printf.printf "Count is %d\n" capture_round.count;
if c > b.participants then
failwith ("c > participants fatal")
else
if c < b.participants then(
match (Fiber.get wait_cond) with
| Some w ->
Eio.Condition.await w (* capture_round.wait_cond *) b.lock ;
false
| None -> failwith "await error"
)
else(
true
)
in
if captured_round_result = true then(
break_barrier b (Fiber.get wait_cond) (Fiber.get breached_cond) true;
reset b (Fiber.get wait_cond) (Fiber.get breached_cond);
)
with | e ->
let msg = Printexc.to_string e
and stack = Printexc.get_backtrace () in
Printf.eprintf "there was an error: %s%s\n" msg stack;
break_barrier b (Fiber.get wait_cond) (Fiber.get breached_cond) true;
()
I have some questions.
-
I use
Fiberlocal variables but even otherwise it executes the test. Not sure if I need them. -
I didn’t find a way to check for multiple
Eio.Conditionand proceed even if one of them is broadcast. So if aFIberis cancelled or finished or if the barrier exceeds its capacity then I would like to broadcast that particular condition. I wanted to select from a list of Conditions like this API
val nchoose_split : 'atlist -> ('a list * 'atlist)tLwt.nchoose_split psis the same asLwt.nchooseps, except that when multiple promises inpsare fulfilled simultaneously (and none are rejected), the result promise is fulfilled with both the list of values of the fulfilled promises, and the list of promises that are still pending. -
I could only find
Fiber.checkto check ifFibersare cancelled or not. How do I find if theFiberis done ?
The Barrier-Actioneffect is a dummy now. Moreover I didn’t know what type of data/thread race could be caused. Should I use T-san to check ?
Thanks