Use Util.with_mutex/Util.with_mutex0 to avoid unbalanced locking

This commit is contained in:
Tony Garnock-Jones 2012-01-08 14:52:03 -05:00
parent de04135a99
commit b7385e4927
4 changed files with 20 additions and 17 deletions

5
TODO
View File

@ -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
View File

@ -3,10 +3,12 @@ open Sexp
let mtx = Mutex.create ()
let write_to_log label body =
Mutex.lock mtx;
print_string label;
print_string ": ";
output_sexp_human stdout body;
print_newline ();
(try
print_string label;
print_string ": ";
output_sexp_human stdout body;
print_newline ()
with _ -> ());
Mutex.unlock mtx
let hook = ref write_to_log

View File

@ -42,9 +42,7 @@ let flush_output mtx flush_control cout =
match Event.poll (Event.receive flush_control) with
| Some () -> ()
| None ->
Mutex.lock mtx;
let ok = try flush cout; true with _ -> false in
Mutex.unlock mtx;
let ok = Util.with_mutex0 mtx (fun () -> try flush cout; true with _ -> false) in
if ok then (Thread.delay 0.1; loop ()) else ()
in loop ()
@ -59,11 +57,7 @@ let relay_main peername cin cout =
Str "", Str "",
Str "", Str ""));
let mtx = Mutex.create () in
let write_sexp s =
Mutex.lock mtx;
(try output_sexp cout s with Sys_error _ -> ()); (* TODO: try removing this *)
Mutex.unlock mtx
in
let write_sexp = Util.with_mutex mtx (output_sexp cout) in
let flush_control = Event.new_channel () in
ignore (Util.create_thread (endpoint_name peername ^ " flush") None
(flush_output mtx flush_control) cout);

12
util.ml
View File

@ -15,3 +15,15 @@ let create_thread name cleanup main initarg =
| None -> ())
in
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 ()