|
| 1 | +open Picos |
| 2 | + |
| 3 | +let[@inline never] impossible () = failwith "impossible" |
| 4 | +let[@inline never] not_in_a_hoot () = invalid_arg "Not in a hoot" |
| 5 | + |
| 6 | +module Message = struct |
| 7 | + type t = .. |
| 8 | +end |
| 9 | + |
| 10 | +type _ tdt = |
| 11 | + | Nil : [> `Nil ] tdt |
| 12 | + | Message : { |
| 13 | + message : Message.t; |
| 14 | + next : [ `Nil | `Message ] tdt; |
| 15 | + } |
| 16 | + -> [> `Message ] tdt |
| 17 | + | Wait : Trigger.t -> [> `Wait ] tdt |
| 18 | + | Pid : { |
| 19 | + computation : unit Computation.t; |
| 20 | + terminated : unit Computation.t; |
| 21 | + incoming : incoming Atomic.t; |
| 22 | + mutable received : [ `Nil | `Message ] tdt; |
| 23 | + } |
| 24 | + -> [> `Pid ] tdt |
| 25 | + |
| 26 | +and incoming = In : [< `Nil | `Message | `Wait ] tdt -> incoming [@@unboxed] |
| 27 | + |
| 28 | +module Pid = struct |
| 29 | + type t = [ `Pid ] tdt |
| 30 | + |
| 31 | + let key : [ `Nil | `Pid ] tdt Fiber.FLS.t = Fiber.FLS.create () |
| 32 | +end |
| 33 | + |
| 34 | +let self_of fiber = |
| 35 | + match Fiber.FLS.get_exn fiber Pid.key with |
| 36 | + | Pid _ as t -> t |
| 37 | + | Nil | (exception Fiber.FLS.Not_set) -> not_in_a_hoot () |
| 38 | + |
| 39 | +let self () : Pid.t = self_of @@ Fiber.current () |
| 40 | + |
| 41 | +exception Terminate |
| 42 | + |
| 43 | +let run main = |
| 44 | + let fiber = Fiber.current () in |
| 45 | + let before = Fiber.FLS.get fiber Pid.key ~default:Nil in |
| 46 | + let computation = Computation.create ~mode:`LIFO () in |
| 47 | + let inner = Computation.Packed computation in |
| 48 | + let (Pid p as t) : Pid.t = |
| 49 | + let terminated = Computation.create ~mode:`LIFO () |
| 50 | + and incoming = Atomic.make (In Nil) |> Multicore_magic.copy_as_padded in |
| 51 | + Pid { computation; terminated; incoming; received = Nil } |
| 52 | + in |
| 53 | + Fiber.FLS.set fiber Pid.key t; |
| 54 | + let (Packed parent as outer) = Fiber.get_computation fiber in |
| 55 | + let canceler = Computation.attach_canceler ~from:parent ~into:computation in |
| 56 | + Fiber.set_computation fiber inner; |
| 57 | + begin |
| 58 | + match main () with |
| 59 | + | () | (exception Terminate) -> Computation.finish p.terminated |
| 60 | + | exception exn -> |
| 61 | + let bt = Printexc.get_raw_backtrace () in |
| 62 | + Computation.cancel p.terminated exn bt |
| 63 | + end; |
| 64 | + Computation.finish p.computation; |
| 65 | + Fiber.set_computation fiber outer; |
| 66 | + Computation.detach parent canceler; |
| 67 | + Fiber.FLS.set fiber Pid.key before; |
| 68 | + (* An otherwise unhandled exception except [Terminate] will be raised. *) |
| 69 | + Computation.check p.terminated |
| 70 | + |
| 71 | +let wait (Pid p : Pid.t) = Computation.wait p.terminated |
| 72 | + |
| 73 | +let spawn main = |
| 74 | + let (Pid p as t) : Pid.t = |
| 75 | + let computation = Computation.create ~mode:`LIFO () |
| 76 | + and terminated = Computation.create ~mode:`LIFO () |
| 77 | + and incoming = Atomic.make (In Nil) in |
| 78 | + Pid { computation; terminated; incoming; received = Nil } |
| 79 | + in |
| 80 | + let fiber = Fiber.create ~forbid:false p.computation in |
| 81 | + Fiber.FLS.set fiber Pid.key t; |
| 82 | + begin |
| 83 | + Fiber.spawn fiber @@ fun fiber -> |
| 84 | + let (Pid p) : Pid.t = self_of fiber in |
| 85 | + (* An unhandled exception except [Terminate] will be treated as a fatal |
| 86 | + error. *) |
| 87 | + begin |
| 88 | + match main () with |
| 89 | + | () | (exception Terminate) -> Computation.finish p.terminated |
| 90 | + end; |
| 91 | + Computation.finish p.computation |
| 92 | + end; |
| 93 | + t |
| 94 | + |
| 95 | +let rec rev_to (Message _ as ms : [ `Message ] tdt) : |
| 96 | + [ `Nil | `Message ] tdt -> _ = function |
| 97 | + | Nil -> ms |
| 98 | + | Message r -> rev_to (Message { message = r.message; next = ms }) r.next |
| 99 | + |
| 100 | +let rev (Message r : [ `Message ] tdt) = |
| 101 | + rev_to (Message { message = r.message; next = Nil }) r.next |
| 102 | + |
| 103 | +let rec receive (Pid p as t : Pid.t) = |
| 104 | + match Atomic.get p.incoming with |
| 105 | + | In Nil as before -> |
| 106 | + let trigger = Trigger.create () in |
| 107 | + let after = In (Wait trigger) in |
| 108 | + if Atomic.compare_and_set p.incoming before after then begin |
| 109 | + match Trigger.await trigger with |
| 110 | + | None -> () |
| 111 | + | Some (exn, bt) -> |
| 112 | + (* At this point the trigger has been signaled and cannot leak |
| 113 | + arbitrary amoun of space. There is no need to remove it. *) |
| 114 | + Printexc.raise_with_backtrace exn bt |
| 115 | + end; |
| 116 | + receive t |
| 117 | + | _ -> begin |
| 118 | + match Atomic.exchange p.incoming (In Nil) with |
| 119 | + | In (Wait _ | Nil) -> impossible () |
| 120 | + | In (Message _ as ms) -> |
| 121 | + let (Message r : [ `Message ] tdt) = rev ms in |
| 122 | + p.received <- r.next; |
| 123 | + r.message |
| 124 | + end |
| 125 | + |
| 126 | +let receive () = |
| 127 | + let (Pid p as t) = self () in |
| 128 | + match p.received with |
| 129 | + | Message r -> |
| 130 | + p.received <- r.next; |
| 131 | + r.message |
| 132 | + | Nil -> receive t |
| 133 | + |
| 134 | +let rec send (Pid p as t : Pid.t) message backoff = |
| 135 | + match Atomic.get p.incoming with |
| 136 | + | In ((Nil | Message _) as before) -> |
| 137 | + let after = Message { message; next = before } in |
| 138 | + if not (Atomic.compare_and_set p.incoming (In before) (In after)) then |
| 139 | + send t message (Backoff.once backoff) |
| 140 | + | In (Wait trigger as before) -> |
| 141 | + let after = Message { message; next = Nil } in |
| 142 | + if Atomic.compare_and_set p.incoming (In before) (In after) then |
| 143 | + Trigger.signal trigger |
| 144 | + else send t message (Backoff.once backoff) |
| 145 | + |
| 146 | +let[@inline] send t message = send t message Backoff.default |
| 147 | + |
| 148 | +type Message.t += Terminated of Pid.t |
| 149 | + |
| 150 | +let monitor ~at ~the:(Pid the_p as the : Pid.t) = |
| 151 | + let[@alert "-handler"] trigger = |
| 152 | + Trigger.from_action at the @@ fun _ at the -> send at (Terminated the) |
| 153 | + in |
| 154 | + if not (Computation.try_attach the_p.terminated trigger) then |
| 155 | + send at (Terminated the) |
| 156 | + |
| 157 | +let empty_bt = Printexc.get_callstack 0 |
| 158 | + |
| 159 | +let link (Pid p1 as t1 : Pid.t) (Pid p2 as t2 : Pid.t) = |
| 160 | + let[@alert "-handler"] trigger = |
| 161 | + Trigger.from_action t1 t2 @@ fun _ (Pid p1 : Pid.t) (Pid p2 : Pid.t) -> |
| 162 | + Computation.cancel p1.computation Terminate empty_bt; |
| 163 | + Computation.cancel p2.computation Terminate empty_bt |
| 164 | + in |
| 165 | + if |
| 166 | + (not (Computation.try_attach p1.terminated trigger)) |
| 167 | + || not (Computation.try_attach p2.terminated trigger) |
| 168 | + then begin |
| 169 | + Computation.cancel p1.computation Terminate empty_bt; |
| 170 | + Computation.cancel p2.computation Terminate empty_bt |
| 171 | + end |
0 commit comments