Skip to content

Commit

Permalink
Avoid mutex change of ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier R. Guérin committed Oct 21, 2020
1 parent e06a250 commit 6b64236
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions lsp/src/scheduler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ end = struct
; work_available = Condition.create ()
}
in
Mutex.lock t.mutex;
t.state <- Running (Thread.create run (do_, t));
Mutex.unlock t.mutex;
with_mutex t.mutex ~f:(fun () ->
t.state <- Running (Thread.create run (do_, t)));
t

let add_work (type a) (t : a t) (w : a) =
Expand Down Expand Up @@ -225,7 +224,8 @@ let time_loop t =
add_events t !to_run;
loop ()
in
loop ()
Condition.signal t.timers_available;
with_mutex t.time_mutex ~f:loop

let wake_loop t =
let rec loop () =
Expand Down Expand Up @@ -253,8 +253,9 @@ let create () =
; detached = Queue.create ()
}
in
Mutex.lock t.time_mutex;
t.time <- Thread.create time_loop t;
with_mutex t.time_mutex ~f:(fun () ->
t.time <- Thread.create time_loop t;
Condition.wait t.timers_available t.time_mutex);
t.waker <- Thread.create wake_loop t;
t

Expand Down
4 changes: 2 additions & 2 deletions lsp/test/fiber_thread_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ let worker = Scheduler.create_thread s
let fb () =
Scheduler.async worker (fun () ->
Thread.delay 1.0;
print_endline "pre epmtive thread finished")
print_endline "preemptive thread finished")
|> Scheduler.await_no_cancel

let _ =
Thread.create
(fun () ->
Thread.delay 3.0;
Thread.delay 10.0;
print_endline "unexpected termination";
exit 1)
()
Expand Down

0 comments on commit 6b64236

Please sign in to comment.