JuliaLang/julia — pull request #2759 by stevengj (2013-04-04 21:21:23+00:00)
Title: RFC: access to sigatomic_begin/end in Julia for non-interrupt-safe C calls

See also issue #2622. This adds Julia-callable `sigatomic_begin`/`end` functions to temporarily defer `SIGINT` (ctrl-c) handling, which is essential for long-running C calls that are not interrupt safe. These are wrapped in a higher-level `disable_sigint() do ... end` construct. Moreover, it also allows one to re-enable `SIGINT` handling *within* a disabled region, e.g. for Julia callbacks that can catch the exception.

(This will enable my PyCall module, for example, to defer interrupts during Python calls, which are [not interrupt safe](https://github.com/stevengj/PyCall.jl/issues/15). But if the Python has a Julia callback, that Julia callback can still catch `InterruptException`s, convert them to Python `KeyboardInterrupt` exceptions which are propagated back through Python and returned to the top-level Julia caller, which converts it back to an `InterruptException`.)

As mentioned in #2622, in the long run we will almost certainly want to make sigatomic the default for *all* `ccall` invocations (since C code cannot generally be assumed to be interrupt-safe), and if this is done directly in the LLVM code generation it can be much more efficient than calling `disable_sigint` from Julia. However, even when that is implemented, we will *still* want "manual" access to `sigatomic_begin/end` in Julia in order to re-enable interrupts in callbacks as I mentioned above.