| 1 |
/**************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* OCaml */ |
| 4 |
/* */ |
| 5 |
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */ |
| 6 |
/* */ |
| 7 |
/* Copyright 1995 Institut National de Recherche en Informatique et */ |
| 8 |
/* en Automatique. */ |
| 9 |
/* */ |
| 10 |
/* All rights reserved. This file is distributed under the terms of */ |
| 11 |
/* the GNU Lesser General Public License version 2.1, with the */ |
| 12 |
/* special exception on linking described in the file LICENSE. */ |
| 13 |
/* */ |
| 14 |
/**************************************************************************/ |
| 15 |
|
| 16 |
#ifndef CAML_THREADS_H |
| 17 |
#define CAML_THREADS_H |
| 18 |
|
| 19 |
#ifdef __cplusplus |
| 20 |
extern "C" { |
| 21 |
#endif |
| 22 |
|
| 23 |
CAMLextern void caml_enter_blocking_section (void); |
| 24 |
CAMLextern void caml_leave_blocking_section (void); |
| 25 |
#define caml_acquire_runtime_system caml_leave_blocking_section |
| 26 |
#define caml_release_runtime_system caml_enter_blocking_section |
| 27 |
|
| 28 |
/* Manage the domain lock around the OCaml run-time system. |
| 29 |
Only one thread at a time can execute OCaml compiled code or |
| 30 |
OCaml run-time system functions within a domain. |
| 31 |
|
| 32 |
When OCaml calls a C function, the current thread holds the domain lock. |
| 33 |
The C function can release it by calling |
| 34 |
[caml_release_runtime_system]. Then, another thread can execute OCaml |
| 35 |
code. However, the calling thread must not access any OCaml data, |
| 36 |
nor call any runtime system function, nor call back into OCaml. |
| 37 |
|
| 38 |
Before returning to its OCaml caller, or accessing OCaml data, |
| 39 |
or call runtime system functions, the current thread must |
| 40 |
re-acquire the domain lock by calling [caml_acquire_runtime_system]. |
| 41 |
|
| 42 |
Symmetrically, if a C function (not called from OCaml) wishes to |
| 43 |
call back into OCaml code, it should invoke [caml_acquire_runtime_system] |
| 44 |
first, then do the callback, then invoke [caml_release_runtime_system]. |
| 45 |
|
| 46 |
For historical reasons, alternate names can be used: |
| 47 |
[caml_enter_blocking_section] instead of [caml_release_runtime_system] |
| 48 |
[caml_leave_blocking_section] instead of [caml_acquire_runtime_system] |
| 49 |
Intuition: a ``blocking section'' is a piece of C code that does not |
| 50 |
use the runtime system (typically, a blocking I/O operation). |
| 51 |
*/ |
| 52 |
|
| 53 |
CAMLextern int caml_c_thread_register(void); |
| 54 |
CAMLextern int caml_c_thread_unregister(void); |
| 55 |
|
| 56 |
/* If a thread is created by C code (instead of by OCaml itself), |
| 57 |
it must be registered with the OCaml runtime system before |
| 58 |
being able to call back into OCaml code or use other runtime system |
| 59 |
functions. Just call [caml_c_thread_register] once. The domain lock |
| 60 |
is not held when [caml_c_thread_register] returns. |
| 61 |
Before the thread finishes, it must call [caml_c_thread_unregister] |
| 62 |
(without holding the domain lock). |
| 63 |
Both functions return 1 on success, 0 on error. |
| 64 |
Note that threads registered by C code belong to the domain 0. |
| 65 |
*/ |
| 66 |
|
| 67 |
#ifdef __cplusplus |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
#endif /* CAML_THREADS_H */ |