Use Util.with_mutex/Util.with_mutex0 to avoid unbalanced locking
This commit is contained in:
parent
de04135a99
commit
b7385e4927
5
TODO
5
TODO
|
@ -1,5 +0,0 @@
|
||||||
Running the server and test3_latency, and then starting and
|
|
||||||
interrupting test1_latency repeatedly, the server eventually corrupts
|
|
||||||
itself and will no longer let test1_latency bind and receive
|
|
||||||
messages. Sometimes test3_latency stops too.
|
|
||||||
[TENTATIVE FIX: see try/with in write_sexp in relay.ml]
|
|
10
log.ml
10
log.ml
|
@ -3,10 +3,12 @@ open Sexp
|
||||||
let mtx = Mutex.create ()
|
let mtx = Mutex.create ()
|
||||||
let write_to_log label body =
|
let write_to_log label body =
|
||||||
Mutex.lock mtx;
|
Mutex.lock mtx;
|
||||||
print_string label;
|
(try
|
||||||
print_string ": ";
|
print_string label;
|
||||||
output_sexp_human stdout body;
|
print_string ": ";
|
||||||
print_newline ();
|
output_sexp_human stdout body;
|
||||||
|
print_newline ()
|
||||||
|
with _ -> ());
|
||||||
Mutex.unlock mtx
|
Mutex.unlock mtx
|
||||||
|
|
||||||
let hook = ref write_to_log
|
let hook = ref write_to_log
|
||||||
|
|
10
relay.ml
10
relay.ml
|
@ -42,9 +42,7 @@ let flush_output mtx flush_control cout =
|
||||||
match Event.poll (Event.receive flush_control) with
|
match Event.poll (Event.receive flush_control) with
|
||||||
| Some () -> ()
|
| Some () -> ()
|
||||||
| None ->
|
| None ->
|
||||||
Mutex.lock mtx;
|
let ok = Util.with_mutex0 mtx (fun () -> try flush cout; true with _ -> false) in
|
||||||
let ok = try flush cout; true with _ -> false in
|
|
||||||
Mutex.unlock mtx;
|
|
||||||
if ok then (Thread.delay 0.1; loop ()) else ()
|
if ok then (Thread.delay 0.1; loop ()) else ()
|
||||||
in loop ()
|
in loop ()
|
||||||
|
|
||||||
|
@ -59,11 +57,7 @@ let relay_main peername cin cout =
|
||||||
Str "", Str "",
|
Str "", Str "",
|
||||||
Str "", Str ""));
|
Str "", Str ""));
|
||||||
let mtx = Mutex.create () in
|
let mtx = Mutex.create () in
|
||||||
let write_sexp s =
|
let write_sexp = Util.with_mutex mtx (output_sexp cout) in
|
||||||
Mutex.lock mtx;
|
|
||||||
(try output_sexp cout s with Sys_error _ -> ()); (* TODO: try removing this *)
|
|
||||||
Mutex.unlock mtx
|
|
||||||
in
|
|
||||||
let flush_control = Event.new_channel () in
|
let flush_control = Event.new_channel () in
|
||||||
ignore (Util.create_thread (endpoint_name peername ^ " flush") None
|
ignore (Util.create_thread (endpoint_name peername ^ " flush") None
|
||||||
(flush_output mtx flush_control) cout);
|
(flush_output mtx flush_control) cout);
|
||||||
|
|
12
util.ml
12
util.ml
|
@ -15,3 +15,15 @@ let create_thread name cleanup main initarg =
|
||||||
| None -> ())
|
| None -> ())
|
||||||
in
|
in
|
||||||
Thread.create guarded_main initarg
|
Thread.create guarded_main initarg
|
||||||
|
|
||||||
|
let with_mutex m f arg =
|
||||||
|
Mutex.lock m;
|
||||||
|
try
|
||||||
|
let result = f arg in
|
||||||
|
Mutex.unlock m;
|
||||||
|
result
|
||||||
|
with e ->
|
||||||
|
Mutex.unlock m;
|
||||||
|
raise e
|
||||||
|
|
||||||
|
let with_mutex0 m thunk = with_mutex m thunk ()
|
||||||
|
|
Loading…
Reference in New Issue