Commit Graph

56 Commits (540996d21ee3793a1cecce252c81fb76a6b9fd61)

Author SHA1 Message Date
Nicolás Ojeda Bär 540996d21e Remove Spacetime 2020-10-08 20:28:12 +02:00
Xavier Leroy 786ee521cf
Use "error checking" mutexes in the threads library (#9846)
- Mutex.lock raises Sys_error if the mutex is already locked by the
  calling thread.
- Mutex.unlock raises Sys_error if the mutex is unlocked or locked
  by another thread.

Add the corresponding tests.

Co-authored-by: David Allsopp <david.allsopp@metastack.com>
2020-10-05 15:00:58 +02:00
David Allsopp 001c2d1283 Restore Cygwin64 support 2020-09-21 13:36:03 +01:00
Stephen Dolan 32feddc1fc Do not run OCaml code inside signal handlers 2020-07-27 17:29:39 +01:00
Jacques-Henri Jourdan 97eb98db94 Memprof: fatal error if thread is stopped from a callback.
This is specified as undefined behavior in gc.mli.

We now use dedicated functions for the interraction between Memprof
and systhreads.
2020-05-11 15:55:07 +02:00
David Allsopp 3aab294513 Replace static inline with Caml_inline
The inline keyword is consequently no longer forced on MSVC builds.
2020-02-11 09:33:55 +00:00
Jacques-Henri Jourdan 7dbbfce890 New ephemeron-free API for Memprof.
The user can register several callbacks, which are called for various
events during the block's lifetime. We need to maintain a data
structure for tracked blocks in the runtime. When using threads,
callbacks can be called concurrently in a reentrant way, so the
functions manipulating this data structure need to be reentrant.
2019-12-21 10:21:48 +01: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
Stephen Dolan 8b20b69a16 Fix stack overflow detection with systhreads 2019-08-27 14:58:07 +01:00
KC Sivaramakrishnan 126383b2b2 Fix long lines 2019-08-23 09:50:05 +05:30
KC Sivaramakrishnan de5ef602fd Rename exn_handler to exception_pointer 2019-08-23 09:50:05 +05:30
KC Sivaramakrishnan cededf23b9 Move local_roots, compare unorderd and gc request variables to domain state 2019-08-23 09:50:05 +05:30
KC Sivaramakrishnan 3357490de0 Move native runtime globals used for roots to domain state 2019-08-23 09:50:05 +05:30
KC Sivaramakrishnan 111f6af469 Move bytecode global variables to domain state 2019-08-23 09:50:05 +05:30
KC Sivaramakrishnan c06038a0ee Move backtrace support global variables to domain state.
Since we cannot access backtrace position in cmmgen.ml anymore,
Cmm.raise_kind in removed. Instead, we use Lambda.raise_kind. When
assembly code is generated, we reset the backtrace position to 0 in the
case of regular raise. Importantly, the semantics remains the same.
2019-08-23 09:50:05 +05:30
KC Sivaramakrishnan fc6f028492 Introduce domain state and steal exception pointer 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 052a950dea Statistical memory profiling of blocks allocated in the major heap. 2019-05-09 16:40:45 +02:00
Stephen Dolan 838e44a245
Update issue numbers in comments after Mantis -> Github migration. (#8505) 2019-03-18 09:42:23 +00:00
ahh 7a27cd56b1 Thread.yield fairness with multiple busy yielding threads (#2112)
Thread.yield invoked a trivial blocking section, which basically woke
up a competitor and then raced with them to get the ocaml lock back, invoking
nanosleep() to help guarantee that the yielder would lose the race. However,
until the yielder woke up again and attempted to take the ocaml lock, it
wouldn't be marked as a waiter.

As a result, if two threads A and B yielded to each other in a tight loop, A's
first yield would work well, but then B would execute 10000+ iterations of the
loop before A could mark itself as a waiter and be yielded to. This works even
worse if A and B are pinned to the same CPU, in which case A can't be marked as
a waiter until the kernel preempts B, which can take tens or hundreds of
milliseconds!

So we reimplement yield; instead of dropping the lock and taking it again (with
a wait in the middle), atomically wake a competitor and mark the yielding thread
as a waiter. (We essentially inline a failed masterlock_acquire into
masterlock_release, specialized for the case where we know another waiter exists
and we want them to run instead.)

Now, threads yielding to each other very consistently succeed--in that same
tight loop, we see a change of control on every iteration (with some very rare
exceptions, most likely from other uncommon blocking region invocations.)

This also means we don't have to worry about the vagaries of kernel scheduling
and whether or not a yielding or a yielded-to thread gets to run first; we
consistently let a competing thread run whenever we yield, which is what the API
claims to do.
2019-02-08 17:18:27 +01:00
Damien Doligez 2fd0186648
Merge pull request #1683 from stedolan/marshal-custom-length
Make marshalled Custom_tag objects store their length.
2018-09-14 10:58:27 +02:00
Stephen Dolan e9b04bb0d5 Add CODE_CUSTOM_FIXED for fixed-size custom serializers.
As a space optimisation, custom serializers that always consume
the same number of bytes need not send the lengths explicitly.
2018-08-22 10:33:09 +01:00
sliquister 2d445f5cc1 Make Printexc.get_callstack work in newly-created, native-code threads (#1895)
The `top_of_stack` field of the `th` descriptor for the new thread was initialized too late, causing `caml_top_of_stack` to be NULL when the thread starts running.  The fix is to initialize `th->top_of_stack` earlier.
2018-07-12 19:32:10 +02:00
David Allsopp b5d1929e87 Whitespace and overlong line fixes. 2018-06-14 15:15:34 +01:00
Xavier Leroy 1c4e822bed
Cleaning up the C code (#1812)
Running Clang 6.0 and GCC 8 with full warnings on suggests a few simple improvements and clean-ups to the C code of OCaml.  This commit implements them.

* Remove old-style, unprototyped function declarations

It's `int f(void)`, not `int f()`.  [-Wstrict-prototypes]

* Be more explicit about conversions involving `float` and `double`

byterun/bigarray.c, byterun/ints.c:
  add explicit casts to clarify the intent
  renamed float field of conversion union from `d` to `f`.

byterun/compact.c, byterun/gc_ctrl.c:
  some local variables were of type `float` while all FP computations
  here are done in double precision;
  turned these variables into `double`.

[-Wdouble-promotion -Wfloat-conversion]

*Add explicit initialization of struct field `compare_ext`

[-Wmissing-field-initializers]

* Declare more functions "noreturn"

[-Wmissing-noreturn]

* Make CAMLassert compliant with ISO C

In `e1 ? e2 : e3`, expressions `e2` and `e3` must have the same type.
`e2` of type `void` and `e3` of type `int`, as in the original code,
is a GNU extension.

* Remove or conditionalize unused macros

Some macros were defined and never used.
Some other macros were always defined but conditionally used.

[-Wunused-macros]

* Replace some uses of `int` by more appropriate types like `intnat`

On a 64-bit platform, `int` is only 32 bits and may not represent correctly
the length of a string or the size of an OCaml heap block.

This commit replaces a number of uses of `int` by other types that
are 64-bit wide on 64-bit architectures, such as `intnat` or `uintnat`
or `size_t` or `mlsize_t`.

Sometimes an `intnat` was used as an `int` and is intended as a Boolean
(0 or 1); then it was replaced by an `int`.

There are many remaining cases where we assign a 64-bit quantity to a
32-bit `int` variable.  Either I believe these cases are safe
(e.g. the 64-bit quantity is the difference between two pointers
within an I/O buffer, something that always fits in 32 bits), or
the code change was not obvious and too risky.

[-Wshorten-64-to-32]

* Put `inline` before return type

`static inline void f(void)` is cleaner than `static void inline f(void)`.

[-Wold-style-declaration]

* Unused assignment to unused parameter

Looks very useless.  [-Wunused-but-set-parameter]
2018-06-07 12:55:09 +02:00
Valentin Gatien-Baron e26c27e002 Remove 50ms delay at exit for programs using threads
$ cat /tmp/b.ml
let () = Thread.join (Thread.create ignore ())
let () = for _ = 0 to 100000; do () done
$ ocamlopt -I +threads unix.cmxa threads.cmxa /tmp/b.ml -o b
$ time ./b # before this commit

real	0m0.053s
user	0m0.000s
sys	0m0.000s
$ time ./b # after this commit

real	0m0.003s
user	0m0.000s
sys	0m0.000s
2017-08-24 22:10:23 -04:00
Max Mouratov 02a8b999f0 runtime: replacing direct calls to malloc/calloc/realloc/free with calls to caml_stat_*
A few more wrappers were added (caml_stat_alloc_noexc, caml_stat_resize_noexc,
caml_stat_calloc_noexc) that do not throw an exception in case of errors and
offer a compatible substitute to the corresponding stdlib functions.
2017-03-17 20:39:03 +05:00
Xavier Leroy 84be1bcfae PR#7158: Event.sync, Mutex.create, Condition.create cause too many GCs
Before, mutexes and condition variables were allocated with caml_alloc_custom and cost factor 1/N with N of a few hundreds or thousands.  Hence GCs were triggered every N allocations approximately, which is bad.  The motivation for this allocation cost was to cover the possibility that mutexes and condvars consume rare kernel resources.  This appears not to be the case in Linux nor in Windows, and is unlikely to be the case in any robust implementation of POSIX threads.  Hence this fix sets the cost factor to 0 in allocations of mutexes and condvars.
2017-02-16 14:06:42 +01:00
Fabrice Le Fessant a6329a0c11 Correctly update caml_top_of_stack in systhreads (#996) 2017-01-17 14:36:28 +01:00
Xavier Leroy 20115917b2 PR#7457: double pthread_mutex_destroy on a I/O buffer mutex
The mutex can be destroyed for the first time when finalizing the I/O buffer.
If the buffer contains unflushed data, it is kept in the list of buffers.
Then Unix.fork() causes caml_thread_reinitialize() to reset all buffers in
this list, destroying the mutex a second time.
2017-01-12 11:42:09 +01:00
Sébastien Hinderer 13945a71ed Do not use the compatibility macros in the C stub code. (#892)
* Don't use the compatibility macros, neither in the C stub code nor in the testsuite.

* Make sure compiler sources do not use deprecated C identifiers.

This is achieved by ensuring that the CAML_NAME_SPACE macro is defined
everytime a C source file is compiled, rather than being defined only
in a few places. Defining this macro guarantees that the compatibility.h
header (where these deprecated identifiers are defined) will not be
included.
2016-11-17 11:03:09 +01:00
Mark Shinwell 6e29398daf Fix wrong #include after recent Spacetime .h file move 2016-09-27 08:07:50 +01:00
Mark Shinwell 67ee66a915 Fix Spacetime excessive memory consumption by sharing call graphs across threads 2016-09-08 11:03:09 +01:00
Mark Shinwell cd0bd8aa73 Spacetime: a new memory profiler (#585) 2016-07-29 15:07:10 +01:00
Fabrice Le Fessant 63a3924f47 Install all include files, and access internals with CAML_INTERNALS 2016-07-12 17:53:24 +02:00
Damien Doligez 520fb2df50 Merge tag 4.03.0 into trunk. 2016-04-28 16:13:21 +02:00
Damien Doligez 5401ce8473 Update headers for the new license.
Remains to be done: remove all headers in testsuite/tests.
2016-02-18 16:59:16 +01:00
Xavier Leroy 906558a155 PR#7038: out of memory condition in caml_io_mutex_lock
Check the return code of st_mutex_create so that, in particular, out-of-memory conditions cause an exception.
2015-11-27 15:33:15 +01:00
Gabriel Scherer 91d1e34e2d Document backtrace infrastructure, make types more explicit
From: Frédéric Bour <frederic.bour@lakaban.net>

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@16366 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2015-08-22 06:04:20 +00:00
Damien Doligez b014f28923 PR#6776: Failure to kill the "tick" thread, segfault when exiting the runtime
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@15975 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2015-03-31 20:46:10 +00:00
Gabriel Scherer 7ca29ef3f7 PR#5887: move the byterun/*.h headers to byterun/caml/*.h to avoid header name clashes
(Jérôme Vouillon and Adrien Nader and Peter Zotov)


git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@15757 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2014-12-27 14:41:49 +00:00
Damien Doligez 7844495624 Merge branch 4.01 from branching point to 4.01.0+rc1
Command line used:
  svn merge --accept postpone -r 13776:14055 $REPO/version/4.01 .


git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14060 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2013-09-04 15:12:37 +00:00
Wojciech Meyer dc9834c4a4 misc: replace stat_(alloc|free|resize) occurences with caml_stat_(...).
byterun/compatibility.h defines:
  #define stat_alloc caml_stat_alloc
  #define stat_free caml_stat_free
  #define stat_resize caml_stat_resize

Having the "caml_" prefix seems cleaner to me, it also avoids some
issues for cross-compilation but I don't remember well which ones.

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@13314 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2013-02-25 03:01:31 +00:00
Damien Doligez def31744f9 remove all $Id keywords
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@13013 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2012-10-15 17:50:56 +00:00
Damien Doligez 997a678d5e clean up TABs and whitespace
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@12799 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2012-07-30 18:04:46 +00:00
Xavier Leroy 0d2e9941eb PR#5295: OS threads: problem with caml_c_thread_unregister()
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@12325 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2012-04-08 17:12:46 +00:00
Damien Doligez e7f5b858c2 More renaming to OCaml
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@12149 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2012-02-10 16:15:24 +00:00