-
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.
Add a poisonable
Barrier
to picos_std.sync
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
open Picos_std_awaitable | ||
|
||
type t = int Awaitable.t | ||
|
||
let parties_bits = (Sys.int_size - 2) / 2 | ||
let max_parties = (1 lsl parties_bits) - 1 | ||
let parties_mask = max_parties | ||
let sense_bit = 1 lsl parties_bits | ||
let awaiting_shift = 1 + parties_bits | ||
let awaiting_one = 1 lsl awaiting_shift | ||
let poisoned_bit = Int.min_int | ||
|
||
let create ?padded parties = | ||
if parties <= 0 || max_parties < parties then | ||
invalid_arg "invalid number of parties"; | ||
Awaitable.make ?padded (parties lor (parties lsl awaiting_shift)) | ||
|
||
exception Poisoned | ||
|
||
let await t = | ||
let before = Awaitable.fetch_and_add t (-awaiting_one) - awaiting_one in | ||
let after_sense = lnot before land sense_bit in | ||
if before < awaiting_one then | ||
let after = | ||
let parties = before land parties_mask in | ||
parties lor after_sense lor (parties lsl awaiting_shift) | ||
in | ||
if Awaitable.compare_and_set t (before land lnot poisoned_bit) after then | ||
Awaitable.broadcast t | ||
else | ||
let _ : int = Awaitable.fetch_and_add t awaiting_one in | ||
raise Poisoned | ||
else | ||
let state = ref before in | ||
while !state land sense_bit <> after_sense do | ||
Awaitable.await t !state; | ||
state := Awaitable.get t | ||
done; | ||
if 0 <= !state then () else raise Poisoned | ||
|
||
let rec poison t = | ||
let before = Awaitable.get t in | ||
if 0 < before then | ||
let after = | ||
let parties = before land parties_mask in | ||
let after_sense = lnot before land sense_bit in | ||
parties lor after_sense lor (parties lsl awaiting_shift) lor poisoned_bit | ||
in | ||
if Awaitable.compare_and_set t before after then Awaitable.broadcast t | ||
else poison t |
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
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