-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #563 from talex5/condition-extras
Extend Condition API
- Loading branch information
Showing
11 changed files
with
288 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let lock _ = assert false | ||
let unlock _ = assert false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
val run : (unit -> unit) -> Eio.Cancel.t option | ||
(** [run fn] runs [fn ()] in a new fiber and returns its context so it can be cancelled. | ||
[fn] may suspend at most once. | ||
If it doesn't suspend then [run] returns [None] after it finishes. *) | ||
Returns None if it never suspended. *) | ||
|
||
val cancel : Eio.Cancel.t -> unit | ||
(** [cancel ctx] cancels the context with a suitable dummy exception. *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
let debug = false | ||
|
||
exception Abort | ||
|
||
module T = Condition | ||
|
||
(* [prod] threads increment a counter and notify a condition. | ||
A consumer watches the condition and waits until it has seen | ||
all of them. We check that the client always sees the final value. | ||
If [cancel] is set, we also try to cancel the client and accept | ||
that as success too. *) | ||
let test ~prod ~cancel () = | ||
let t = T.create () in | ||
let sent = Atomic.make 0 in | ||
for _ = 1 to prod do | ||
Atomic.spawn (fun () -> | ||
Atomic.incr sent; | ||
T.broadcast t | ||
) | ||
done; | ||
let finished = ref false in | ||
Atomic.spawn (fun () -> | ||
let ctx = | ||
Fake_sched.run @@ fun () -> | ||
try | ||
T.loop_no_mutex t (fun () -> | ||
if Atomic.get sent = prod && not cancel then Some () | ||
else None | ||
); | ||
finished := true | ||
with T.Cancel.Cancelled Abort -> | ||
finished := true | ||
in | ||
if cancel then | ||
Option.iter (fun c -> T.Cancel.cancel c Abort) ctx | ||
); | ||
Atomic.final (fun () -> | ||
Atomic.check (fun () -> !finished); | ||
if debug then ( | ||
Fmt.pr "%a@." Broadcast.dump t; | ||
); | ||
) | ||
|
||
let () = | ||
Atomic.trace (test ~prod:2 ~cancel:false); | ||
Atomic.trace (test ~prod:2 ~cancel:true) |
Oops, something went wrong.