Skip to content

Commit 40e57b0

Browse files
committed
CP-307958: remove condition from Db_lock
No measurable performance change, but this makes it clearer for deadlock detection algorithms that we are waiting to acquire a lock. It'll also make it clearer for OCaml 5's thread sanitizer that values are modified with a lock held. Signed-off-by: Edwin Török <[email protected]>
1 parent 8b030f1 commit 40e57b0

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

ocaml/database/db_lock.ml

+5-12
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ module ReentrantLock : REENTRANT_LOCK = struct
5959
type t = {
6060
holder: tid option Atomic.t (* The holder of the lock *)
6161
; mutable holds: int (* How many holds the holder has on the lock *)
62-
; lock: Mutex.t (* Barrier to signal waiting threads *)
63-
; condition: Condition.t
64-
(* Waiting threads are signalled via this condition to reattempt to acquire the lock *)
62+
; lock: Mutex.t (* Mutex held by the holder thread *)
6563
; statistics: statistics (* Bookkeeping of time taken to acquire lock *)
6664
}
6765

@@ -73,7 +71,6 @@ module ReentrantLock : REENTRANT_LOCK = struct
7371
holder= Atomic.make None
7472
; holds= 0
7573
; lock= Mutex.create ()
76-
; condition= Condition.create ()
7774
; statistics= create_statistics ()
7875
}
7976

@@ -94,17 +91,15 @@ module ReentrantLock : REENTRANT_LOCK = struct
9491
let intended = Some me in
9592
let counter = Mtime_clock.counter () in
9693
Mutex.lock l.lock ;
97-
while not (Atomic.compare_and_set l.holder None intended) do
98-
Condition.wait l.condition l.lock
99-
done ;
94+
Atomic.set l.holder intended ;
10095
lock_acquired () ;
10196
let stats = l.statistics in
10297
let delta = Clock.Timer.span_to_s (Mtime_clock.count counter) in
10398
stats.total_time <- stats.total_time +. delta ;
10499
stats.min_time <- Float.min delta stats.min_time ;
105100
stats.max_time <- Float.max delta stats.max_time ;
106101
stats.acquires <- stats.acquires + 1 ;
107-
Mutex.unlock l.lock ;
102+
(* do not unlock, it will be done when holds reaches 0 instead *)
108103
l.holds <- 1
109104

110105
let unlock l =
@@ -114,10 +109,8 @@ module ReentrantLock : REENTRANT_LOCK = struct
114109
l.holds <- l.holds - 1 ;
115110
if l.holds = 0 then (
116111
let () = Atomic.set l.holder None in
117-
Mutex.lock l.lock ;
118-
Condition.signal l.condition ;
119-
Mutex.unlock l.lock ;
120-
lock_released ()
112+
(* the lock is held (acquired in [lock]), we only need to unlock *)
113+
Mutex.unlock l.lock ; lock_released ()
121114
)
122115
| _ ->
123116
failwith

0 commit comments

Comments
 (0)