Skip to content

Commit

Permalink
CP-43755 - reformat
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Lindig <[email protected]>
  • Loading branch information
Christian Lindig committed Mar 4, 2024
1 parent 1166feb commit 4537f85
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 61 deletions.
10 changes: 7 additions & 3 deletions ocaml/xapi/locking_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ let release_lock (target : API.ref_VM) (token : token) =
Thread_state.released (lock_of_vm target) ;
debug "Released lock on VM %s with token %Ld" (Ref.string_of target) token


module Semaphore = struct
type t = {
name: string
Expand All @@ -316,8 +315,13 @@ module Semaphore = struct
; mutable max: int
}

let create ?(max=1) name =
{name; sem= Semaphore_vendored.Counting.make max; max_lock= Mutex.create (); max}
let create ?(max = 1) name =
{
name
; sem= Semaphore_vendored.Counting.make max
; max_lock= Mutex.create ()
; max
}

let execute (x : t) f =
Semaphore_vendored.Counting.acquire x.sem ;
Expand Down
1 change: 0 additions & 1 deletion ocaml/xapi/locking_helpers.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module Named_mutex : sig
val execute : t -> (unit -> 'a) -> 'a
end


module Semaphore : sig
(** a semaphore that allows at most N operations to proceed at a time *)
type t
Expand Down
130 changes: 73 additions & 57 deletions semaphore_vendored.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,87 @@
(** Semaphores *)

type sem = {
mut: Mutex.t; (* protects [v] *)
mutable v: int; (* the current value *)
nonzero: Condition.t (* signaled when [v > 0] *)
mut: Mutex.t
; (* protects [v] *)
mutable v: int
; (* the current value *)
nonzero: Condition.t (* signaled when [v > 0] *)
}

module Counting = struct

type t = sem

let make v =
if v < 0 then invalid_arg "Semaphore.Counting.init: wrong initial value";
{ mut = Mutex.create(); v; nonzero = Condition.create() }

let release s =
Mutex.lock s.mut;
if s.v < max_int then begin
s.v <- s.v + 1;
Condition.signal s.nonzero;
type t = sem

let make v =
if v < 0 then invalid_arg "Semaphore.Counting.init: wrong initial value" ;
{mut= Mutex.create (); v; nonzero= Condition.create ()}

let release s =
Mutex.lock s.mut ;
if s.v < max_int then (
s.v <- s.v + 1 ;
Condition.signal s.nonzero ;
Mutex.unlock s.mut
) else (
Mutex.unlock s.mut ;
raise (Sys_error "Semaphore.Counting.release: overflow")
)

let acquire s =
Mutex.lock s.mut ;
while s.v = 0 do
Condition.wait s.nonzero s.mut
done ;
s.v <- s.v - 1 ;
Mutex.unlock s.mut
end else begin
Mutex.unlock s.mut;
raise (Sys_error "Semaphore.Counting.release: overflow")
end

let acquire s =
Mutex.lock s.mut;
while s.v = 0 do Condition.wait s.nonzero s.mut done;
s.v <- s.v - 1;
Mutex.unlock s.mut

let try_acquire s =
Mutex.lock s.mut;
let ret = if s.v = 0 then false else (s.v <- s.v - 1; true) in
Mutex.unlock s.mut;
ret

let get_value s = s.v

let try_acquire s =
Mutex.lock s.mut ;
let ret =
if s.v = 0 then
false
else (
s.v <- s.v - 1 ;
true
)
in
Mutex.unlock s.mut ; ret

let get_value s = s.v
end

module Binary = struct
type t = sem

let make b =
{
mut= Mutex.create ()
; v= (if b then 1 else 0)
; nonzero= Condition.create ()
}

let release s =
Mutex.lock s.mut ;
s.v <- 1 ;
Condition.signal s.nonzero ;
Mutex.unlock s.mut

type t = sem

let make b =
{ mut = Mutex.create();
v = if b then 1 else 0;
nonzero = Condition.create() }

let release s =
Mutex.lock s.mut;
s.v <- 1;
Condition.signal s.nonzero;
Mutex.unlock s.mut

let acquire s =
Mutex.lock s.mut;
while s.v = 0 do Condition.wait s.nonzero s.mut done;
s.v <- 0;
Mutex.unlock s.mut

let try_acquire s =
Mutex.lock s.mut;
let ret = if s.v = 0 then false else (s.v <- 0; true) in
Mutex.unlock s.mut;
ret
let acquire s =
Mutex.lock s.mut ;
while s.v = 0 do
Condition.wait s.nonzero s.mut
done ;
s.v <- 0 ;
Mutex.unlock s.mut

let try_acquire s =
Mutex.lock s.mut ;
let ret =
if s.v = 0 then
false
else (
s.v <- 0 ;
true
)
in
Mutex.unlock s.mut ; ret
end

0 comments on commit 4537f85

Please sign in to comment.