Skip to content

Commit

Permalink
Avoid unnessary signal and broadcast calls
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Jan 10, 2025
1 parent 9909e19 commit 05a57de
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/picos_std.sync/rwlock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,26 @@ end = struct

let create ?padded () = Awaitable.make ?padded 0
let is_zero t = Awaitable.get t == 0
let incr t = Awaitable.incr t
let decr t = Awaitable.fetch_and_add t (-1) = 1
let incr t = Awaitable.fetch_and_add t 2 |> ignore

let decr t =
let was = Awaitable.fetch_and_add t (-2) in
was = 3 && Awaitable.compare_and_set t 1 0

let signal = Awaitable.signal
let broadcast = Awaitable.broadcast
let await t = if Awaitable.get t <> 0 then Awaitable.await t Is 0

let rec await t before =
if before land 1 <> 0 then Awaitable.await t Is 0
else if Awaitable.compare_and_set t before (before lor 1) then
Awaitable.await t Is 0
else
let before = Awaitable.get t in
if before <> 0 then await t before

let[@inline] await t =
let before = Awaitable.get t in
if before <> 0 then await t before
end

module Tree_zero : sig
Expand Down

0 comments on commit 05a57de

Please sign in to comment.