|
1 | 1 | open Picos |
2 | 2 |
|
3 | | -let wrap_all t main _ = |
4 | | - if Bundle.is_running t then begin |
5 | | - try main () with exn -> Bundle.error t exn (Printexc.get_raw_backtrace ()) |
6 | | - end; |
7 | | - Bundle.decr t |
| 3 | +let[@inline never] wrap_all t main = |
| 4 | + match main () with |
| 5 | + | () -> Bundle.decr t |
| 6 | + | exception exn -> Bundle.raised exn t |
8 | 7 |
|
9 | | -let wrap_any t main _ = |
10 | | - if Bundle.is_running t then begin |
11 | | - match main () with |
12 | | - | () -> Bundle.terminate t |
13 | | - | exception exn -> Bundle.error t exn (Printexc.get_raw_backtrace ()) |
14 | | - end; |
15 | | - Bundle.decr t |
| 8 | +let[@inline never] wrap_any t main = |
| 9 | + match main () with |
| 10 | + | () -> |
| 11 | + Bundle.terminate t; |
| 12 | + Bundle.decr t |
| 13 | + | exception exn -> Bundle.raised exn t |
16 | 14 |
|
17 | | -let rec spawn (Bundle r as t : Bundle.t) wrap = function |
| 15 | +let rec spawn (Bundle r as t : Bundle.t) ~all = function |
18 | 16 | | [] -> () |
19 | 17 | | [ main ] -> |
20 | 18 | Bundle.unsafe_incr t; |
21 | | - let unused_fake_fiber = Obj.magic () in |
22 | | - wrap t main unused_fake_fiber |
| 19 | + if Bundle.is_running t then |
| 20 | + if all then wrap_all t main else wrap_any t main |
| 21 | + else Bundle.decr t |
23 | 22 | | main :: mains -> |
24 | 23 | Bundle.unsafe_incr t; |
25 | 24 | let fiber = Fiber.create_packed ~forbid:false r.bundle in |
26 | 25 | (* Note that [Fiber.spawn] checks the cancelation status of the bundle. *) |
27 | | - Fiber.spawn fiber (wrap t main); |
28 | | - spawn t wrap mains |
| 26 | + Fiber.spawn fiber (fun _ -> |
| 27 | + if Bundle.is_running t then |
| 28 | + if all then wrap_all t main else wrap_any t main |
| 29 | + else Bundle.decr t); |
| 30 | + spawn t ~all mains |
29 | 31 |
|
30 | | -let run actions wrap = |
31 | | - Bundle.join_after @@ fun (Bundle _ as t : Bundle.t) -> |
32 | | - try spawn t wrap actions |
33 | | - with exn -> |
34 | | - let bt = Printexc.get_raw_backtrace () in |
35 | | - Bundle.decr t; |
36 | | - Bundle.error t exn bt |
| 32 | +let run actions ~all = |
| 33 | + Bundle.join_after @@ fun (t : Bundle.t) -> |
| 34 | + try spawn t ~all actions with exn -> Bundle.raised exn t |
37 | 35 |
|
38 | | -let all actions = run actions wrap_all |
39 | | -let any actions = run actions wrap_any |
| 36 | +let all actions = run actions ~all:true |
| 37 | +let any actions = run actions ~all:false |
0 commit comments