|
| 1 | +open Kcas |
| 2 | + |
| 3 | +type 'a t = { head : 'a head_pack Loc.t; tail : 'a tail_pack Loc.t } |
| 4 | + |
| 5 | +and ('a, _) head = |
| 6 | + | Cons : { |
| 7 | + counter : int; |
| 8 | + value : 'a; |
| 9 | + suffix : 'a head_pack; |
| 10 | + } |
| 11 | + -> ('a, [> `Cons ]) head |
| 12 | + | Head : { counter : int } -> ('a, [> `Head ]) head |
| 13 | + |
| 14 | +and 'a head_pack = H : ('a, [< `Cons | `Head ]) head -> 'a head_pack |
| 15 | +[@@unboxed] |
| 16 | + |
| 17 | +and ('a, _) tail = |
| 18 | + | Snoc : { |
| 19 | + counter : int; |
| 20 | + prefix : 'a tail_pack; |
| 21 | + value : 'a; |
| 22 | + } |
| 23 | + -> ('a, [> `Snoc ]) tail |
| 24 | + | Tail : { |
| 25 | + counter : int; |
| 26 | + mutable move : ('a, [ `Snoc ]) tail; |
| 27 | + } |
| 28 | + -> ('a, [> `Tail ]) tail |
| 29 | + |
| 30 | +and 'a tail_pack = T : ('a, [< `Snoc | `Tail ]) tail -> 'a tail_pack |
| 31 | +[@@unboxed] |
| 32 | + |
| 33 | +let create () = |
| 34 | + let head = Loc.make ~padded:true (H (Head { counter = 1 })) in |
| 35 | + let tail = |
| 36 | + Loc.make ~padded:true (T (Tail { counter = 0; move = Obj.magic () })) |
| 37 | + in |
| 38 | + { head; tail } |> Multicore_magic.copy_as_padded |
| 39 | + |
| 40 | +let rec rev (suffix : (_, [< `Cons ]) head) = function |
| 41 | + | T (Snoc { counter; prefix; value }) -> |
| 42 | + rev (Cons { counter; value; suffix = H suffix }) prefix |
| 43 | + | T (Tail _) -> suffix |
| 44 | + |
| 45 | +let[@inline] rev = function |
| 46 | + | (Snoc { counter; prefix; value } : (_, [< `Snoc ]) tail) -> |
| 47 | + rev |
| 48 | + (Cons { counter; value; suffix = H (Head { counter = counter + 1 }) }) |
| 49 | + prefix |
| 50 | + |
| 51 | +let[@inline] counter_of_head = function |
| 52 | + | (Head r : (_, [< `Head ]) head) -> r.counter |
| 53 | + |
| 54 | +let[@inline] counter_of_snoc = function |
| 55 | + | (Snoc r : (_, [< `Snoc ]) tail) -> r.counter |
| 56 | + |
| 57 | +let[@inline] counter_of_tail = function |
| 58 | + | (Tail r : (_, [< `Tail ]) tail) -> r.counter |
| 59 | + |
| 60 | +let clear_move = function |
| 61 | + | (Tail tail_r : (_, [< `Tail ]) tail) -> tail_r.move <- Obj.magic () |
| 62 | + |
| 63 | +let is_tail = function T (Tail _) -> true | T (Snoc _) -> false |
| 64 | + |
| 65 | +let rec push backoff t value = |
| 66 | + match Loc.fenceless_get t.tail with |
| 67 | + | T (Snoc snoc_r) as prefix -> |
| 68 | + let after = T (Snoc { counter = snoc_r.counter + 1; prefix; value }) in |
| 69 | + if not (Loc.compare_and_set t.tail prefix after) then |
| 70 | + push (Backoff.once backoff) t value |
| 71 | + | T (Tail tail_r as tail) -> |
| 72 | + let move = tail_r.move in |
| 73 | + if move != Obj.magic () then |
| 74 | + match Loc.fenceless_get t.head with |
| 75 | + | H (Head _ as head) when counter_of_head head < counter_of_snoc move -> |
| 76 | + let after = rev move in |
| 77 | + if Loc.compare_and_set t.head (H head) (H after) then |
| 78 | + clear_move tail; |
| 79 | + push backoff t value |
| 80 | + | _ -> push_with backoff t (counter_of_tail tail) (T tail) value |
| 81 | + else push_with backoff t (counter_of_tail tail) (T tail) value |
| 82 | + |
| 83 | +and push_with backoff t counter prefix value = |
| 84 | + let after = Snoc { counter = counter + 1; prefix; value } in |
| 85 | + if not (Loc.compare_and_set t.tail prefix (T after)) then |
| 86 | + push (Backoff.once backoff) t value |
| 87 | + |
| 88 | +let[@inline] push t value = push Backoff.default t value |
| 89 | + |
| 90 | +exception Empty |
| 91 | + |
| 92 | +let rec pop backoff t = |
| 93 | + match Loc.get t.head with |
| 94 | + | H (Cons cons_r) as before -> |
| 95 | + let after = cons_r.suffix in |
| 96 | + if Loc.compare_and_set t.head before after then cons_r.value |
| 97 | + else pop (Backoff.once backoff) t |
| 98 | + | H (Head _ as head) -> begin |
| 99 | + match Loc.fenceless_get t.tail with |
| 100 | + | T (Snoc snoc_r as move) -> |
| 101 | + if is_tail snoc_r.prefix then begin |
| 102 | + let tail = |
| 103 | + Tail { counter = snoc_r.counter - 1; move = Obj.magic () } |
| 104 | + in |
| 105 | + if |
| 106 | + Loc.fenceless_get t.head == H head |
| 107 | + && Loc.compare_and_set t.tail (T move) (T tail) |
| 108 | + then snoc_r.value |
| 109 | + else pop backoff t |
| 110 | + end |
| 111 | + else |
| 112 | + let tail = Tail { counter = snoc_r.counter; move } in |
| 113 | + if |
| 114 | + Loc.fenceless_get t.head == H head |
| 115 | + && Loc.compare_and_set t.tail (T move) (T tail) |
| 116 | + then pop_moving backoff t head move tail |
| 117 | + else pop backoff t |
| 118 | + | T (Tail tail_r as tail) -> |
| 119 | + let move = tail_r.move in |
| 120 | + if move == Obj.magic () then pop_emptyish backoff t head |
| 121 | + else pop_moving backoff t head move tail |
| 122 | + end |
| 123 | + |
| 124 | +and pop_moving backoff t head move tail = |
| 125 | + if counter_of_head head < counter_of_snoc move then |
| 126 | + match rev move with |
| 127 | + | Cons cons_r -> |
| 128 | + if Loc.compare_and_set t.head (H head) cons_r.suffix then begin |
| 129 | + clear_move tail; |
| 130 | + cons_r.value |
| 131 | + end |
| 132 | + else pop backoff t |
| 133 | + else pop_emptyish backoff t head |
| 134 | + |
| 135 | +and pop_emptyish backoff t head = |
| 136 | + if Loc.get t.head == H head then raise_notrace Empty else pop backoff t |
| 137 | + |
| 138 | +let[@inline] pop_opt t = |
| 139 | + match pop Backoff.default t with |
| 140 | + | value -> Some value |
| 141 | + | exception Empty -> None |
| 142 | + |
| 143 | +let[@inline] pop t = pop Backoff.default t |
| 144 | + |
| 145 | +let rec length t = |
| 146 | + let head = Loc.get t.head in |
| 147 | + let tail = Loc.get t.tail in |
| 148 | + if head != Loc.get t.head then length t |
| 149 | + else |
| 150 | + let head_at = |
| 151 | + match head with H (Cons r) -> r.counter | H (Head r) -> r.counter |
| 152 | + in |
| 153 | + let tail_at = |
| 154 | + match tail with T (Snoc r) -> r.counter | T (Tail r) -> r.counter |
| 155 | + in |
| 156 | + tail_at - head_at + 1 |
0 commit comments