-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
59 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
type 'a t = { bag: 'a list Atomic.t } [@@unboxed] | ||
|
||
let create () = | ||
let bag = Atomic.make [] in | ||
{ bag } | ||
|
||
module Backoff = struct | ||
type t = int | ||
|
||
let default = 2 | ||
|
||
let once (b : t) : t = | ||
for _i = 1 to b do | ||
Relax_.cpu_relax () | ||
done; | ||
min (b * 2) 256 | ||
end | ||
|
||
let rec add backoff t x = | ||
let before = Atomic.get t.bag in | ||
let after = x :: before in | ||
if not (Atomic.compare_and_set t.bag before after) then | ||
add (Backoff.once backoff) t x | ||
|
||
let[@inline] add t x = add Backoff.default t x | ||
|
||
exception Empty | ||
|
||
let[@inline] pop_all t : _ list = | ||
match Atomic.exchange t.bag [] with | ||
| [] -> raise_notrace Empty | ||
| l -> l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(** A multi-producer, single-consumer bag *) | ||
|
||
type 'a t | ||
|
||
val create : unit -> 'a t | ||
|
||
val add : 'a t -> 'a -> unit | ||
(** [add q x] adds [x] in the bag. *) | ||
|
||
exception Empty | ||
|
||
val pop_all : 'a t -> 'a list | ||
(** Return all current items in an unspecified order. | ||
@raise Empty if empty *) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.