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 22, 2020
1 parent 8d1ec0b commit a430d82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 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
2 changes: 1 addition & 1 deletion lsp/test/fiber_thread_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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 _ =
Expand Down

0 comments on commit a430d82

Please sign in to comment.