Skip to content

Commit

Permalink
Add Computation.with_action
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Apr 7, 2024
1 parent ea13e24 commit 0968b1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/picos/bootstrap/picos_bootstrap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ module Computation = struct
let balance_and_mode = Bool.to_int (mode == `FIFO) in
Atomic.make (Continue { balance_and_mode; triggers = [] })

let with_action ?(mode : [ `FIFO | `LIFO ] = `FIFO) x y action =
let balance_and_mode = one + Bool.to_int (mode == `FIFO) in
let trigger = Trigger.from_action x y action in
Atomic.make (Continue { balance_and_mode; triggers = [ trigger ] })

let is_canceled t =
match Atomic.get t with
| Canceled _ -> true
Expand Down
22 changes: 21 additions & 1 deletion lib/picos/picos.mli
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,6 @@ module Computation : sig

val wait : _ t -> unit
(** [wait computation] waits for the computation to complete. *)

(** {2 Interface for propagating cancelation} *)

val canceler : from:_ t -> into:_ t -> Trigger.t
Expand All @@ -767,6 +766,27 @@ module Computation : sig

include Intf.Computation with type 'a t := 'a t with type exn_bt := Exn_bt.t

val with_action :
?mode:[ `FIFO | `LIFO ] ->
'x ->
'y ->
(Trigger.t -> 'x -> 'y -> unit) ->
'a t
[@@alert
handler
"This is an escape hatch for experts implementing schedulers or \
structured concurrency mechanisms. If you know what you are doing, use \
[@alert \"-handler\"]."]
(** [with_action x y resume] is equivalent to
{@ocaml skip[
let computation = create () in
let trigger = Trigger.from_action x y resume in
let _ : bool = try_attach computation trigger in
computation
]}
⚠️ The same warnings as with {!Trigger.from_action} apply. *)

(** {2 Design rationale}
The computation concept can be seen as a kind of single-shot atomic event
Expand Down

0 comments on commit 0968b1a

Please sign in to comment.