Skip to content

Commit 95b3364

Browse files
committed
Change poison to poison_at and add poison with ?callstack
This should make `poison`able data structures more convenient to use.
1 parent b006be7 commit 95b3364

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Forbid cancelation propagation during `release` calls in the
44
`picos_std.finally` library (@polytypic)
5+
- Renamed `(Ivar|Stream).poison` to `(Ivar|Stream).poison_at` and added
6+
`(Ivar|Stream).poison` with optional `?callstack:int` (@polytypic)
57

68
## 0.5.0
79

lib/picos_std.sync/ivar.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ let create () = Computation.create ()
77
let of_value = Computation.returned
88
let try_fill = Computation.try_return
99
let fill = Computation.return
10-
let try_poison = Computation.try_cancel
11-
let poison = Computation.cancel
10+
let try_poison_at = Computation.try_cancel
11+
let poison_at = Computation.cancel
1212

1313
let peek_opt ivar =
1414
match Computation.peek_exn ivar with
1515
| value -> Some value
1616
| exception Computation.Running -> None
1717

18+
let try_poison ?(callstack = 0) ivar exn =
19+
try_poison_at ivar exn (Printexc.get_callstack callstack)
20+
1821
let read = Computation.await
1922
let read_evt = Event.from_computation
23+
24+
let[@inline always] poison ?(callstack = 0) ivar exn =
25+
poison_at ivar exn (Printexc.get_callstack callstack)

lib/picos_std.sync/picos_std_sync.mli

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,25 @@ module Ivar : sig
305305
(** [fill ivar value] is equivalent to
306306
{{!try_fill} [try_fill ivar value |> ignore]}. *)
307307

308-
val try_poison : 'a t -> exn -> Printexc.raw_backtrace -> bool
309-
(** [try_poison ivar exn bt] attempts to poison the incremental variable with
310-
the specified exception and backtrace. Returns [true] on success and
308+
val try_poison_at : 'a t -> exn -> Printexc.raw_backtrace -> bool
309+
(** [try_poison_at ivar exn bt] attempts to poison the incremental variable
310+
with the specified exception and backtrace. Returns [true] on success and
311311
[false] in case the variable had already been poisoned or assigned a
312312
value. *)
313313

314-
val poison : 'a t -> exn -> Printexc.raw_backtrace -> unit
315-
(** [poison ivar exn bt] is equivalent to
316-
{{!try_poison} [try_poison ivar exn bt |> ignore]}. *)
314+
val try_poison : ?callstack:int -> 'a t -> exn -> bool
315+
(** [try_poison ivar exn] is equivalent to
316+
{{!try_poison_at} [try_poison_at ivar exn (Printexc.get_callstack n)]}
317+
where [n] defaults to [0]. *)
318+
319+
val poison_at : 'a t -> exn -> Printexc.raw_backtrace -> unit
320+
(** [poison_at ivar exn bt] is equivalent to
321+
{{!try_poison_at} [try_poison_at ivar exn bt |> ignore]}. *)
322+
323+
val poison : ?callstack:int -> 'a t -> exn -> unit
324+
(** [poison ivar exn] is equivalent to
325+
{{!poison_at} [poison_at ivar exn (Printexc.get_callstack n)]}
326+
where [n] defaults to [0]. *)
317327

318328
val peek_opt : 'a t -> 'a option
319329
(** [peek_opt ivar] either returns [Some value] in case the variable has been
@@ -351,11 +361,16 @@ module Stream : sig
351361
has been {{!poison} poisoned} in which case only the exception given to
352362
{!poison} will be raised. *)
353363

354-
val poison : 'a t -> exn -> Printexc.raw_backtrace -> unit
355-
(** [poison stream exn bt] marks the stream as poisoned at the current
364+
val poison_at : 'a t -> exn -> Printexc.raw_backtrace -> unit
365+
(** [poison_at stream exn bt] marks the stream as poisoned at the current
356366
position, which means that subsequent attempts to {!push} to the [stream]
357367
will raise the given exception with backtrace. *)
358368

369+
val poison : ?callstack:int -> 'a t -> exn -> unit
370+
(** [poison stream exn] is equivalent to
371+
{{!poison_at} [poison_at stream exn (Printexc.get_callstack n)]}
372+
where [n] defaults to [0]. *)
373+
359374
type !'a cursor
360375
(** Represents a (past or current) position in a stream. *)
361376

lib/picos_std.sync/stream.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ open Picos_std_event
44
type 'a cursor = Cons of ('a * 'a cursor) Computation.t [@@unboxed]
55
type 'a t = 'a cursor Atomic.t
66

7-
let create ?padded () =
8-
Multicore_magic.copy_as ?padded
9-
(Atomic.make (Cons (Computation.create ~mode:`LIFO ())))
10-
117
let[@inline] advance t tail curr =
128
if Atomic.get t == Cons tail then
139
Atomic.compare_and_set t (Cons tail) curr |> ignore
@@ -16,6 +12,10 @@ let[@inline] help t tail =
1612
let _, curr = Computation.peek_exn tail in
1713
advance t tail curr
1814

15+
let create ?padded () =
16+
Multicore_magic.copy_as ?padded
17+
(Atomic.make (Cons (Computation.create ~mode:`LIFO ())))
18+
1919
let rec push t ((_, curr) as next) backoff =
2020
let (Cons tail) = Atomic.get t in
2121
if Computation.try_return tail next then advance t tail curr
@@ -28,20 +28,23 @@ let push t value =
2828
let next = (value, Cons (Computation.create ~mode:`LIFO ())) in
2929
push t next Backoff.default
3030

31-
let rec poison t exn bt backoff =
31+
let rec poison_at t exn bt backoff =
3232
let (Cons tail) = Atomic.get t in
3333
if not (Computation.try_cancel tail exn bt) then begin
3434
let backoff = Backoff.once backoff in
3535
help t tail;
36-
poison t exn bt backoff
36+
poison_at t exn bt backoff
3737
end
3838

3939
let peek_opt (Cons at) =
4040
match Computation.peek_exn at with
4141
| value -> Some value
4242
| exception Computation.Running -> None
4343

44-
let poison t exn bt = poison t exn bt Backoff.default
44+
let poison_at t exn bt = poison_at t exn bt Backoff.default
4545
let tap = Atomic.get
4646
let read (Cons at) = Computation.await at
4747
let read_evt (Cons at) = Event.from_computation at
48+
49+
let[@inline always] poison ?(callstack = 0) t exn =
50+
poison_at t exn (Printexc.get_callstack callstack)

0 commit comments

Comments
 (0)