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