|
| 1 | +(* |
| 2 | + * Copyright (c) 2015, Théo Laurent <[email protected]> |
| 3 | + * Copyright (c) 2015, KC Sivaramakrishnan <[email protected]> |
| 4 | + * |
| 5 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 6 | + * purpose with or without fee is hereby granted, provided that the above |
| 7 | + * copyright notice and this permission notice appear in all copies. |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | + *) |
| 17 | + |
| 18 | +(* Michael-Scott queue *) |
| 19 | + |
| 20 | +type 'a node = Nil | Next of 'a * 'a node Atomic.t |
| 21 | +type 'a t = { head : 'a node Atomic.t; tail : 'a node Atomic.t } |
| 22 | + |
| 23 | +let create () = |
| 24 | + let head = Next (Obj.magic (), Atomic.make Nil) in |
| 25 | + { head = Atomic.make head; tail = Atomic.make head } |
| 26 | + |
| 27 | +let is_empty q = |
| 28 | + match Atomic.get q.head with |
| 29 | + | Nil -> failwith "MSQueue.is_empty: impossible" |
| 30 | + | Next (_, x) -> ( match Atomic.get x with Nil -> true | _ -> false) |
| 31 | + |
| 32 | +let pop q = |
| 33 | + let b = Backoff.create () in |
| 34 | + let rec loop () = |
| 35 | + let s = Atomic.get q.head in |
| 36 | + let nhead = |
| 37 | + match s with |
| 38 | + | Nil -> failwith "MSQueue.pop: impossible" |
| 39 | + | Next (_, x) -> Atomic.get x |
| 40 | + in |
| 41 | + match nhead with |
| 42 | + | Nil -> None |
| 43 | + | Next (v, _) when Atomic.compare_and_set q.head s nhead -> Some v |
| 44 | + | _ -> |
| 45 | + Backoff.once b; |
| 46 | + loop () |
| 47 | + in |
| 48 | + loop () |
| 49 | + |
| 50 | +let push q v = |
| 51 | + let rec find_tail_and_enq curr_end node = |
| 52 | + if Atomic.compare_and_set curr_end Nil node then () |
| 53 | + else |
| 54 | + match Atomic.get curr_end with |
| 55 | + | Nil -> find_tail_and_enq curr_end node |
| 56 | + | Next (_, n) -> find_tail_and_enq n node |
| 57 | + in |
| 58 | + let newnode = Next (v, Atomic.make Nil) in |
| 59 | + let tail = Atomic.get q.tail in |
| 60 | + match tail with |
| 61 | + | Nil -> failwith "HW_MSQueue.push: impossible" |
| 62 | + | Next (_, n) -> |
| 63 | + find_tail_and_enq n newnode; |
| 64 | + ignore (Atomic.compare_and_set q.tail tail newnode) |
| 65 | + |
| 66 | +let clean_until q f = |
| 67 | + let b = Backoff.create () in |
| 68 | + let rec loop () = |
| 69 | + let s = Atomic.get q.head in |
| 70 | + let nhead = |
| 71 | + match s with |
| 72 | + | Nil -> failwith "MSQueue.pop: impossible" |
| 73 | + | Next (_, x) -> Atomic.get x |
| 74 | + in |
| 75 | + match nhead with |
| 76 | + | Nil -> () |
| 77 | + | Next (v, _) -> |
| 78 | + if not (f v) then |
| 79 | + if Atomic.compare_and_set q.head s nhead then ( |
| 80 | + Backoff.reset b; |
| 81 | + loop ()) |
| 82 | + else ( |
| 83 | + Backoff.once b; |
| 84 | + loop ()) |
| 85 | + else () |
| 86 | + in |
| 87 | + loop () |
| 88 | + |
| 89 | +type 'a cursor = 'a node |
| 90 | + |
| 91 | +let snapshot q = |
| 92 | + match Atomic.get q.head with |
| 93 | + | Nil -> failwith "MSQueue.snapshot: impossible" |
| 94 | + | Next (_, n) -> Atomic.get n |
| 95 | + |
| 96 | +let next c = match c with Nil -> None | Next (a, n) -> Some (a, Atomic.get n) |
0 commit comments