Commit Graph

19 Commits (c3f6cd7ff7d53a1b97651ad59d2c0c81f98751a8)

Author SHA1 Message Date
Stephen Dolan 63d516bda1 Avoid running signal handlers while holding locks in io.c 2020-07-27 17:29:39 +01:00
Stephen Dolan 32feddc1fc Do not run OCaml code inside signal handlers 2020-07-27 17:29:39 +01:00
Gabriel Scherer 646d30404e Merge pull request #9253 from Octachron/easier_compat
4.10, #9205: move back caml_* to thematic headers

(cherry picked from commit 036495ba5c97ff96962d1e6f746e72a5836a4946)
2020-01-21 22:02:57 +01:00
Guillaume Munch-Maccagnoni 7820dd7a96 In long-running C code, force examining all callbacks at the next safe
point following every minor collection or major slice.

Also run signal handlers first.

Indeed, in some cases, caml_something_to_do is not reliable (spotted
by @jhjourdan):

* We could get into caml_process_pending_actions when
  caml_something_to_do is seen as set but not caml_pending_signals,
  making us miss the signal.

* If there are two different callbacks (say, a signal and a finaliser)
  arriving at the same time, then we set caml_something_to_do to 0
  when starting the first one while the second one is still waiting.
  We may want to run the second one if the first one is taking long.

In the latter case, the additional fix is to favour signals, which
have a lower latency requirement, whereas the latency of finalisers
keeps the same order of magnitude, and memprof callbacks are served on
a best-effort basis.
2019-10-17 20:14:19 +02:00
Guillaume Munch-Maccagnoni d0f70f757a Resource-safe C interface for async callbacks
Introduce caml_process_pending_actions and
caml_process_pending_actions_exn: a variant of the former which does
not raise but returns a value that has to be checked against
Is_exception_value.

I keep the current conventions from caml_callback{,_exn}: For a
resource-safe interface, we mostly care about the _exn variants, but
every time there is a public _exn function I provide a function that
raises directly for convenience.

They are introduced and documented in caml/signals.h.

Private functions are converted to their _exn variant on the way as
needed: for internal functions of the runtime, it is desirable to go
towards a complete elimination of functions that raise implicitly.

Get rid of the distant logic of caml_raise_in_async_callback. Instead,
caml_process_pending_events takes care itself of its something_to_do
"resource". This avoids calling the former function in places
unrelated to asynchronous callbacks.
2019-10-17 20:14:19 +02:00
Guillaume Munch-Maccagnoni 2433b9373f Separate caml_check_urgent_gc back into GC actions and async callbacks
In 8691, caml_check_urgent_gc was merged with the function that runs
asynchronous callbacks. The rationale was that caml_check_urgent_gc
already runs finalisers, and so could have run any asynchronous
callbacks.

We agreed on a different strategy: we know that users could not rely
on asynchronous callbacks being called at this point, so take the
opportunity to make it callback-safe, like was done for allocation
functions.

The new check_urgent_gc no longer calls finalisers (nor any
callbacks), and instead two internal functions are introduced:

* caml_do_urgent_gc_and_callbacks : function to perform actions
  unconditionally.

* caml_check_urgent_gc_and_callbacks : function that checks for
  something to do, and then executes all actions (GC and callbacks).
2019-10-15 19:24:41 +02:00
Guillaume Munch-Maccagnoni e64a8618fe [cleanup] Rename caml_set_something_to_do into caml_set_action_pending 2019-10-15 14:02:22 +02:00
Guillaume Munch-Maccagnoni 6dbbc26f98 [cleanup] Remove async_action_hook
This is dead code since removal of vmthreads.
2019-10-15 14:02:22 +02:00
Xavier Leroy d29a01e049
Merge pull request #8670 from stedolan/stack-overflow-fix
Fix stack overflow detection with systhreads.
2019-10-11 10:30:01 +02:00
Stephen Dolan dcf346450b Ensure that C allocation functions do not trigger callbacks.
Fixes a bug in #8691 and adds a test.
2019-08-29 09:35:42 +01:00
Stephen Dolan 8b20b69a16 Fix stack overflow detection with systhreads 2019-08-27 14:58:07 +01:00
KC Sivaramakrishnan cededf23b9 Move local_roots, compare unorderd and gc request variables to domain state 2019-08-23 09:50:05 +05:30
Jacques-Henri Jourdan 42ab59aab1 Use [caml_something_to_do] both in native and bytecode mode to remember that callbacks are pending.
This make us able to get rid of to xxx_to_do variables in `final.c`
and `memprof.c`. The variable is reset to 0 when entering
`caml_check_urgent_gc`, which is now the main entry point for
asynchronous callbacks. In case a callback raises an exception, we
need to set it back to 1 to make sure no callback is missed.
2019-06-06 16:08:06 +02:00
Jacques-Henri Jourdan 79088fb09d Guarantee that no finalisers will be called while allocating memory in OCaml heap from C code.
The finalizers and all the other asynchronous callbacks (including
signal handlers, memprof callbacks and finalizers) are now called in a
common function, [caml_async_callbacks]. It is called in
[caml_check_urgent_gc] and wherever [caml_process_pending_signals] was
called.

This makes it possible to simplify the [caml_gc_dispatch] logic by
removing the loop it contains, since it no longer calls finalizers.
2019-06-05 14:25:26 +02:00
Jacques-Henri Jourdan cea1ff7534 Memprof sampling for blocks in the minor heap, allocated by C code.
Allocations ignored by this version
- Marshalling
- In the minor heap by natively-compiled OCaml code

Allocations potentially sampled
- In the major heap
- In the minor heap by C code and OCaml code in bytecode mode
2019-05-20 13:04:28 +02:00
Jacques-Henri Jourdan 08ed1e85cd Fix Thread.sigmask, take 2 (#2211)
Fix Thread.sigmaks, by checking whether a signal is masked before handling it.

We use [sigprocmask] (if available) to check whether a signal is
blocked when the systhread library is not loaded. As soon as the
[Thread] module gets loaded, we use [pthread_sigmask] instead, and
redirect all the calls to [sigprocmask] to [pthread_sigmask].  Indeed,
the latter has unspecified behavior in a multi-threaded context
anyway.  In practice, this should not change the semantics of
[Unix.sigprocmask] on Linux, since on this platform, [pthread_sigmask]
is actually an alias for [sigprocmask]. On MacOSX, the semantics will
change, since [sigprocmask] changes the masks of the whole process on
this platform.

Also, include [caml_pending_signals] in signals returned by
[Unix.sigpending]. Indeed, some signals might have been handled in the
POSIX sense by the C handler in the OCaml runtime, but not been
handled in the OCaml sense (for example, because they are blocked).

This commit un-reverts 1c82c481a, which has been reverted in
79eb572e4. The issues of the original commit are corrected in this commit.
2019-03-01 14:14:29 +01:00
David Allsopp 79eb572e42 Revert 1c82c481a5.
Problem with the test on macOS and FreeBSD + issue with downstream use
of signals_are_pending
2018-12-21 17:05:17 +01:00
Jacques-Henri Jourdan 1c82c481a5 Fix Thread.sigmaks, by checking whether a signal is masked before handling it.
We use [sigprocmask] (if available) to check whether a signal is blocked when
the systhread library is not loaded. When it gets loaded, we use
[pthread_sigmask] instead.

Also, include [caml_pending_signals] in signals returned by
[Unix.sigpending]. Indeed, some signals might have been handled in the
POSIX sense by the C handler in the OCaml runtime, but not been
handled in the OCaml sense (for example, because they are blocked).
2018-12-19 10:32:37 +00:00
Sébastien Hinderer d3e73595e5 Merge the asmrun and byterun directories into the runtime directory 2018-06-28 17:50:33 +02:00