| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
module PriorityQueue : sig |
| 12 |
type 'a t |
| 13 |
val create : unit -> 'a t |
| 14 |
val set_rel : ('a -> 'a -> int) -> 'a t -> unit |
| 15 |
val is_empty : 'a t -> bool |
| 16 |
val exists : ('a -> bool) -> 'a t -> bool |
| 17 |
val pop : ?picky:('a -> bool) -> 'a t -> 'a |
| 18 |
val push : 'a t -> 'a -> unit |
| 19 |
val clear : 'a t -> unit |
| 20 |
val length : 'a t -> int |
| 21 |
end = struct |
| 22 |
type 'a item = int * 'a |
| 23 |
type 'a rel = 'a item -> 'a item -> int |
| 24 |
type 'a t = ('a item list * 'a rel) ref |
| 25 |
let sort_timestamp (i,_) (j,_) = i - j |
| 26 |
let age = ref 0 |
| 27 |
let create () = ref ([],sort_timestamp) |
| 28 |
let is_empty t = fst !t = [] |
| 29 |
let exists p t = List.exists (fun (_,x) -> p x) (fst !t) |
| 30 |
let pop ?(picky=(fun _ -> true)) t = |
| 31 |
let (l, rel) = !t in |
| 32 |
let rec aux acc = function |
| 33 |
| [] -> raise Queue.Empty |
| 34 |
| (_,x) :: xs when picky x -> t := (List.rev acc @ xs, rel); x |
| 35 |
| (_,x) as hd :: xs -> aux (hd :: acc) xs in |
| 36 |
aux [] l |
| 37 |
let push t x = |
| 38 |
let (xs, rel) = !t in |
| 39 |
incr age; |
| 40 |
|
| 41 |
t := (List.sort rel (xs @ [!age,x]), rel) |
| 42 |
let clear t = t := ([], snd !t) |
| 43 |
let set_rel rel t = |
| 44 |
let rel (_,x) (_,y) = rel x y in |
| 45 |
t := (List.sort rel (fst !t), rel) |
| 46 |
let length t = List.length (fst !t) |
| 47 |
end |
| 48 |
|
| 49 |
type 'a t = { |
| 50 |
queue: 'a PriorityQueue.t; |
| 51 |
lock : Mutex.t; |
| 52 |
cond : Condition.t; |
| 53 |
mutable nwaiting : int; |
| 54 |
cond_waiting : Condition.t; |
| 55 |
mutable release : bool; |
| 56 |
} |
| 57 |
|
| 58 |
exception BeingDestroyed |
| 59 |
|
| 60 |
let create () = { |
| 61 |
queue = PriorityQueue.create (); |
| 62 |
lock = Mutex.create (); |
| 63 |
cond = Condition.create (); |
| 64 |
nwaiting = 0; |
| 65 |
cond_waiting = Condition.create (); |
| 66 |
release = false; |
| 67 |
} |
| 68 |
|
| 69 |
let pop ?(picky=(fun _ -> true)) ?(destroy=ref false) tq = |
| 70 |
let { queue = q; lock = m; cond = c; cond_waiting = cn } = tq in |
| 71 |
CThread.with_lock m ~scope:(fun () -> |
| 72 |
if tq.release then raise BeingDestroyed; |
| 73 |
while not (PriorityQueue.exists picky q || !destroy) do |
| 74 |
tq.nwaiting <- tq.nwaiting + 1; |
| 75 |
Condition.broadcast cn; |
| 76 |
Condition.wait c m; |
| 77 |
tq.nwaiting <- tq.nwaiting - 1; |
| 78 |
if tq.release || !destroy then raise BeingDestroyed |
| 79 |
done; |
| 80 |
if !destroy then raise BeingDestroyed; |
| 81 |
let x = PriorityQueue.pop ~picky q in |
| 82 |
Condition.signal c; |
| 83 |
Condition.signal cn; |
| 84 |
x) |
| 85 |
|
| 86 |
let broadcast tq = |
| 87 |
let { lock = m; cond = c } = tq in |
| 88 |
CThread.with_lock m ~scope:(fun () -> |
| 89 |
Condition.broadcast c) |
| 90 |
|
| 91 |
let push tq x = |
| 92 |
let { queue = q; lock = m; cond = c; release } = tq in |
| 93 |
if release then CErrors.anomaly(Pp.str |
| 94 |
"TQueue.push while being destroyed! Only 1 producer/destroyer allowed."); |
| 95 |
CThread.with_lock m ~scope:(fun () -> |
| 96 |
PriorityQueue.push q x; |
| 97 |
Condition.broadcast c) |
| 98 |
|
| 99 |
let length tq = |
| 100 |
let { queue = q; lock = m } = tq in |
| 101 |
CThread.with_lock m ~scope:(fun () -> |
| 102 |
PriorityQueue.length q) |
| 103 |
|
| 104 |
let clear tq = |
| 105 |
let { queue = q; lock = m; cond = c } = tq in |
| 106 |
CThread.with_lock m ~scope:(fun () -> |
| 107 |
PriorityQueue.clear q) |
| 108 |
|
| 109 |
let clear_saving tq f = |
| 110 |
let { queue = q; lock = m; cond = c } = tq in |
| 111 |
let saved = ref [] in |
| 112 |
CThread.with_lock m ~scope:(fun () -> |
| 113 |
while not (PriorityQueue.is_empty q) do |
| 114 |
let elem = PriorityQueue.pop q in |
| 115 |
match f elem with |
| 116 |
| Some x -> saved := x :: !saved |
| 117 |
| None -> () |
| 118 |
done); |
| 119 |
List.rev !saved |
| 120 |
|
| 121 |
let is_empty tq = PriorityQueue.is_empty tq.queue |
| 122 |
|
| 123 |
let destroy tq = |
| 124 |
tq.release <- true; |
| 125 |
while tq.nwaiting > 0 do |
| 126 |
CThread.with_lock tq.lock ~scope:(fun () -> |
| 127 |
Condition.broadcast tq.cond) |
| 128 |
done; |
| 129 |
tq.release <- false |
| 130 |
|
| 131 |
let wait_until_n_are_waiting_and_queue_empty j tq = |
| 132 |
CThread.with_lock tq.lock ~scope:(fun () -> |
| 133 |
while not (PriorityQueue.is_empty tq.queue) || tq.nwaiting < j do |
| 134 |
Condition.wait tq.cond_waiting tq.lock |
| 135 |
done) |
| 136 |
|
| 137 |
let wait_until_n_are_waiting_then_snapshot j tq = |
| 138 |
let l = ref [] in |
| 139 |
CThread.with_lock tq.lock ~scope:(fun () -> |
| 140 |
while not (PriorityQueue.is_empty tq.queue) do |
| 141 |
l := PriorityQueue.pop tq.queue :: !l |
| 142 |
done; |
| 143 |
while tq.nwaiting < j do Condition.wait tq.cond_waiting tq.lock done; |
| 144 |
List.iter (PriorityQueue.push tq.queue) (List.rev !l); |
| 145 |
if !l <> [] then Condition.broadcast tq.cond); |
| 146 |
List.rev !l |
| 147 |
|
| 148 |
let set_order tq rel = |
| 149 |
CThread.with_lock tq.lock ~scope:(fun () -> |
| 150 |
PriorityQueue.set_rel rel tq.queue) |