Skip to content

Commit

Permalink
How does Lock perform?
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Jan 16, 2025
1 parent 76a076e commit 3eacdc3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
42 changes: 21 additions & 21 deletions bench/bench_bounded_q.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,70 @@ module Bounded_q : sig
val pop_opt : 'a t -> 'a option
end = struct
type 'a t = {
mutex : Mutex.t;
mutex : Lock.t;
queue : 'a Queue.t;
capacity : int;
not_empty : Condition.t;
not_full : Condition.t;
not_empty : Lock.Condition.t;
not_full : Lock.Condition.t;
}

let create ?(capacity = Int.max_int) () =
if capacity < 0 then invalid_arg "negative capacity"
else
let mutex = Mutex.create ~padded:true ()
let mutex = Lock.create ~padded:true ()
and queue = Queue.create () |> Multicore_magic.copy_as_padded
and not_empty = Condition.create ~padded:true ()
and not_full = Condition.create ~padded:true () in
and not_empty = Lock.Condition.create ~padded:true ()
and not_full = Lock.Condition.create ~padded:true () in
{ mutex; queue; capacity; not_empty; not_full }
|> Multicore_magic.copy_as_padded

let is_empty t =
Mutex.lock ~checked:false t.mutex;
Lock.lock t.mutex;
let result = Queue.is_empty t.queue in
Mutex.unlock ~checked:false t.mutex;
Lock.unlock t.mutex;
result

let is_full_unsafe t = t.capacity <= Queue.length t.queue

let push t x =
Mutex.lock ~checked:false t.mutex;
Lock.lock t.mutex;
match
while is_full_unsafe t do
Condition.wait t.not_full t.mutex
Lock.Condition.wait t.not_full t.mutex
done
with
| () ->
Queue.push x t.queue;
let n = Queue.length t.queue in
Mutex.unlock ~checked:false t.mutex;
if n = 1 then Condition.broadcast t.not_empty
Lock.unlock t.mutex;
if n = 1 then Lock.Condition.broadcast t.not_empty
| exception exn ->
Mutex.unlock ~checked:false t.mutex;
Lock.unlock t.mutex;
raise exn

let pop t =
Mutex.lock ~checked:false t.mutex;
Lock.lock t.mutex;
match
while Queue.length t.queue = 0 do
Condition.wait t.not_empty t.mutex
Lock.Condition.wait t.not_empty t.mutex
done
with
| () ->
let n = Queue.length t.queue in
let elem = Queue.pop t.queue in
Mutex.unlock ~checked:false t.mutex;
if n = t.capacity then Condition.broadcast t.not_full;
Lock.unlock t.mutex;
if n = t.capacity then Lock.Condition.broadcast t.not_full;
elem
| exception exn ->
Mutex.unlock ~checked:false t.mutex;
Lock.unlock t.mutex;
raise exn

let pop_opt t =
Mutex.lock ~checked:false t.mutex;
Lock.lock t.mutex;
let n = Queue.length t.queue in
let elem_opt = Queue.take_opt t.queue in
Mutex.unlock ~checked:false t.mutex;
if n = t.capacity then Condition.broadcast t.not_full;
Lock.unlock t.mutex;
if n = t.capacity then Lock.Condition.broadcast t.not_full;
elem_opt
end

Expand Down
6 changes: 3 additions & 3 deletions bench/bench_mutex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let run_one ~budgetf ~n_fibers ~use_domains () =

let v = ref 0 in
let n_ops_todo = Countdown.create ~n_domains () in
let mutex = Mutex.create ~padded:true () in
let mutex = Lock.create ~padded:true () in

let batch = if use_domains || n_fibers < 16 then 1000 else 100 in

Expand All @@ -34,13 +34,13 @@ let run_one ~budgetf ~n_fibers ~use_domains () =
if n <> 0 then
let rec loop n =
if 0 < n then begin
Mutex.lock mutex;
Lock.lock mutex;
let x = !v in
v := x + 1;
Control.yield ();
assert (!v = x + 1);
v := x;
Mutex.unlock mutex;
Lock.unlock mutex;
loop (n - 1)
end
else work ()
Expand Down
10 changes: 5 additions & 5 deletions bench/bench_ref_mutex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ type t = Op : string * 'a * ('a Ref.t -> unit) * ('a Ref.t -> unit) -> t

let run_one ~budgetf ?checked ?(n_iter = 250 * Util.iter_factor)
(Op (name, value, op1, op2)) =
let mutex = Mutex.create () in
let mutex = Lock.create () in
let loc = Ref.make value in

let init _ = () in
let wrap _ () = Scheduler.run in
let work _ () =
let rec loop i =
if i > 0 then begin
Mutex.lock ?checked mutex;
Lock.lock mutex;
op1 loc;
Mutex.unlock ?checked mutex;
Mutex.lock ?checked mutex;
Lock.unlock mutex;
Lock.lock mutex;
op2 loc;
Mutex.unlock ?checked mutex;
Lock.unlock mutex;
loop (i - 2)
end
in
Expand Down

0 comments on commit 3eacdc3

Please sign in to comment.