Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
In native thread programs, Sys.Break does not get raised inside C blocking sections?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-07-27 (20:21)
From: Aleksey Nogin <nogin@m...>
Subject: In native thread programs, Sys.Break does not get raised inside C blocking sections?
I am having a problem, where the Ctrl-C stops working as soon as I start 
using the Thread module.

To demonstrate, suppose I have the following code:

------- C --------
#include <caml/mlvalues.h>
#include <caml/signals.h>

value sleep_forever(value foo) {
         while(1) {
------- OCaml -------
external sleep_forever : unit -> unit = "sleep_forever"

let () =
    Sys.catch_break true;
       sleep_forever ()
    with exn ->
       Format.printf "Signal received: %s@." (Printexc.to_string exn)

If I compile it normally, run it, and press Ctrl-C, it prints "Signal 
received: Sys.Break" and exits. If I compile it with native threads (and 
  use -linkall to make sure the Thread module is linked in), the exact 
same code fails to react to Ctrl-C!

As far as I can tell, without threads OCaml interrupt handler raises the 
exception right away. With threads, however it simply records the fact 
that Ctrl-C was invoked, and returns back to the C code. When the 
leave_blocking_section is called, the Sys.Break would finally be raised, 
but in my example this never happens :-(.

I understand that in multi-threaded programs the leave_blocking_section 
is needed before returning to OCaml world (as it would acquire the "big 
OCaml lock"), but why can't the interrupt handler simply force that to 
happen? Is there any easy way to have Ctrl-C automatically force 
leave_blocking_section, even in multi-threaded programs?

P.S. The above example is obviously very contrived, but it came from a 
real-world program (OMake) that may have a single thread waiting 
"forever" for an external event (filesystem change notification) inside 
C code...



Aleksey Nogin, Research Scientist
Advanced Technologies Department, Information & System Sciences Lab
HRL Laboratories, LLC, Malibu, CA