|
| 1 | +open Picos |
| 2 | +module S = Picos_std_sync |
| 3 | + |
| 4 | +module List_ext = struct |
| 5 | + let[@tail_mod_cons] rec drop_first_or_not_found x' = function |
| 6 | + | [] -> raise_notrace Not_found |
| 7 | + | x :: xs -> if x == x' then xs else x :: drop_first_or_not_found x' xs |
| 8 | +end |
| 9 | + |
| 10 | +module Id = struct |
| 11 | + type t = T of int [@@unboxed] |
| 12 | + |
| 13 | + let[@inline] compare (T l) (T r) = Int.compare l r |
| 14 | + let key = Fiber.FLS.create () |
| 15 | + let next_id = Atomic.make 0 |
| 16 | + |
| 17 | + let get_as fiber = |
| 18 | + match Fiber.FLS.get_exn fiber key with |
| 19 | + | id -> id |
| 20 | + | exception Fiber.FLS.Not_set -> |
| 21 | + let id = T (Atomic.fetch_and_add next_id 1) in |
| 22 | + Fiber.FLS.set fiber key id; |
| 23 | + id |
| 24 | + |
| 25 | + let get () = get_as @@ Fiber.current () |
| 26 | +end |
| 27 | + |
| 28 | +module Priority = struct |
| 29 | + type t = T of int [@@unboxed] |
| 30 | + |
| 31 | + let[@inline] compare (T l) (T r) = Int.compare l r |
| 32 | + let[@inline] max (T l) (T r) = T (Int.max l r) |
| 33 | + let default = T 0 |
| 34 | + let higher (T p) = T (p + 1) |
| 35 | + let key = Fiber.FLS.create () |
| 36 | + let get_as fiber = Fiber.FLS.get fiber key ~default |
| 37 | + let get () = get_as @@ Fiber.current () |
| 38 | + |
| 39 | + let set priority = |
| 40 | + let fiber = Fiber.current () in |
| 41 | + if priority = default then Fiber.FLS.remove fiber key |
| 42 | + else Fiber.FLS.set fiber key priority |
| 43 | +end |
| 44 | + |
| 45 | +module Priority_inv = struct |
| 46 | + type t = Priority.t |
| 47 | + |
| 48 | + let compare l r = Priority.compare r l |
| 49 | +end |
| 50 | + |
| 51 | +module Pq_hi = Psq.Make (Id) (Priority_inv) |
| 52 | + |
| 53 | +(* |
| 54 | +type _ tdt = |
| 55 | + | Nothing : [> `Nothing ] tdt |
| 56 | + | Holder : |
| 57 | +*) |
| 58 | +type mutex = { waiters : Pq_hi.t Atomic.t; mutex : S.Mutex.t } |
| 59 | + |
| 60 | +module Mutex = struct |
| 61 | + type t = mutex |
| 62 | + |
| 63 | + let key = Fiber.FLS.create () |
| 64 | + let get_as fiber = Fiber.FLS.get fiber key ~default:[] |
| 65 | + |
| 66 | + let add_mutex_as fiber t = |
| 67 | + get_as fiber |> List.cons t |> Fiber.FLS.set fiber key |
| 68 | + |
| 69 | + let remove_mutex_as fiber t = |
| 70 | + get_as fiber |
| 71 | + |> List_ext.drop_first_or_not_found t |
| 72 | + |> Fiber.FLS.set fiber key |
| 73 | + |
| 74 | + let rec add_waiter t id priority backoff = |
| 75 | + let before = Atomic.get t.waiters in |
| 76 | + let after = Pq_hi.add id priority before in |
| 77 | + if not (Atomic.compare_and_set t.waiters before after) then |
| 78 | + add_waiter t id priority (Backoff.once backoff) |
| 79 | + |
| 80 | + let rec remove_waiter t id backoff = |
| 81 | + let before = Atomic.get t.waiters in |
| 82 | + let after = Pq_hi.remove id before in |
| 83 | + if not (Atomic.compare_and_set t.waiters before after) then |
| 84 | + remove_waiter t id (Backoff.once backoff) |
| 85 | + |
| 86 | + let max_waiter t = |
| 87 | + match Pq_hi.min (Atomic.get t.waiters) with |
| 88 | + | None -> Priority.default |
| 89 | + | Some (_id, priority) -> priority |
| 90 | + |
| 91 | + let create ?padded () = |
| 92 | + let waiters = Atomic.make Pq_hi.empty |> Multicore_magic.copy_as ?padded in |
| 93 | + let mutex = S.Mutex.create ?padded () in |
| 94 | + Multicore_magic.copy_as ?padded { waiters; mutex } |
| 95 | + |
| 96 | + let lock t = |
| 97 | + let fiber = Fiber.current () in |
| 98 | + let id = Id.get_as fiber in |
| 99 | + let priority = Priority.get_as fiber in |
| 100 | + add_waiter t id priority Backoff.default; |
| 101 | + match S.Mutex.lock t.mutex with |
| 102 | + | () -> |
| 103 | + remove_waiter t id Backoff.default; |
| 104 | + add_mutex_as fiber t |
| 105 | + | exception exn -> |
| 106 | + remove_waiter t id Backoff.default; |
| 107 | + raise exn |
| 108 | + |
| 109 | + let unlock t = |
| 110 | + let fiber = Fiber.current () in |
| 111 | + remove_mutex_as fiber t; |
| 112 | + S.Mutex.unlock t.mutex |
| 113 | +end |
| 114 | + |
| 115 | +module Condition = struct |
| 116 | + type t = S.Condition.t |
| 117 | + |
| 118 | + let create = S.Condition.create |
| 119 | + let wait t m = S.Condition.wait t m.mutex |
| 120 | + let broadcast = S.Condition.broadcast |
| 121 | +end |
| 122 | + |
| 123 | +let _get_dynamic_priority_as fiber = |
| 124 | + Mutex.get_as fiber |
| 125 | + |> List.fold_left |
| 126 | + (fun p m -> Priority.max p (Mutex.max_waiter m)) |
| 127 | + (Priority.get_as fiber) |
| 128 | + |
| 129 | +let run_fiber ?fatal_exn_handler:_ _fiber _main = failwith "XXX" |
| 130 | +let run ?fatal_exn_handler:_ ?forbid:_ _main = failwith "XXX" |
0 commit comments