summaryrefslogtreecommitdiff
path: root/otherlibs/systhreads/threads.h
blob: 616138da609004c54e98a8cdaea2dce127450271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/***********************************************************************/
/*                                                                     */
/*                                OCaml                                */
/*                                                                     */
/*         Xavier Leroy and Damien Doligez, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1995 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License, with    */
/*  the special exception on linking described in file ../../LICENSE.  */
/*                                                                     */
/***********************************************************************/

#ifndef CAML_THREADS_H
#define CAML_THREADS_H

#ifdef __cplusplus
extern "C" {
#endif

CAMLextern void caml_enter_blocking_section (void);
CAMLextern void caml_leave_blocking_section (void);
#define caml_acquire_runtime_system caml_leave_blocking_section
#define caml_release_runtime_system caml_enter_blocking_section

/* Manage the master lock around the OCaml run-time system.
   Only one thread at a time can execute OCaml compiled code or
   OCaml run-time system functions.

   When OCaml calls a C function, the current thread holds the master
   lock.  The C function can release it by calling
   [caml_release_runtime_system].  Then, another thread can execute OCaml
   code.  However, the calling thread must not access any OCaml data,
   nor call any runtime system function, nor call back into OCaml.

   Before returning to its OCaml caller, or accessing OCaml data,
   or call runtime system functions, the current thread must
   re-acquire the master lock by calling [caml_acquire_runtime_system].

   Symmetrically, if a C function (not called from OCaml) wishes to
   call back into OCaml code, it should invoke [caml_acquire_runtime_system]
   first, then do the callback, then invoke [caml_release_runtime_system].

   For historical reasons, alternate names can be used:
     [caml_enter_blocking_section]  instead of  [caml_release_runtime_system]
     [caml_leave_blocking_section]  instead of  [caml_acquire_runtime_system]
   Intuition: a ``blocking section'' is a piece of C code that does not
   use the runtime system (typically, a blocking I/O operation).
*/

CAMLextern int caml_c_thread_register(void);
CAMLextern int caml_c_thread_unregister(void);

/* If a thread is created by C code (instead of by OCaml itself),
   it must be registered with the OCaml runtime system before
   being able to call back into OCaml code or use other runtime system
   functions.  Just call [caml_c_thread_register] once.
   Before the thread finishes, it must call [caml_c_thread_unregister].
   Both functions return 1 on success, 0 on error.
*/

#ifdef __cplusplus
}
#endif

#endif /* CAML_THREADS_H */