/
core_thread.mli
156 lines (121 loc) · 6 KB
/
core_thread.mli
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(** Lightweight threads. *)
open! Import
(** The type of thread handles. *)
type t [@@deriving sexp_of]
(** {6 Thread creation and termination} *)
val create : ('a -> 'b) -> 'a -> t
(** [Thread.create funct arg] creates a new thread of control, in which the
function application [funct arg] is executed concurrently with the other
threads of the program. The application of [Thread.create] returns the
handle of the newly created thread. The new thread terminates when the
application [funct arg] returns, either normally or by raising an uncaught
exception. In the latter case, the exception is printed on standard error,
but not propagated back to the parent thread. Similarly, the result of the
application [funct arg] is discarded and not directly accessible to the
parent thread. *)
val self : unit -> t
(** Return the thread currently executing. *)
val id : t -> int
(** Return the identifier of the given thread. A thread identifier
is an integer that identifies uniquely the thread.
It can be used to build data structures indexed by threads. *)
val exit : unit -> unit
(** Terminate prematurely the currently executing thread. *)
(** This has been deliberately removed from the interface because it is an inherently
unsafe operation and is never required.
{[
(** Terminate prematurely the thread whose handle is given.
This functionality is available only with bytecode-level threads. *)
val kill : t -> unit
]}
*)
(** {6 Suspending threads} *)
val delay : float -> unit
(** [delay d] suspends the execution of the calling thread for
[d] seconds. The other program threads continue to run during
this time. *)
val join : t -> unit
(** [join th] suspends the execution of the calling thread
until the thread [th] has terminated. *)
val wait_read : Unix.file_descr -> unit
(** See {!Thread.wait_write}.*)
val wait_write : Unix.file_descr -> unit
(** Suspend the execution of the calling thread until at least
one character is available for reading ({!Thread.wait_read}) or
one character can be written without blocking ([wait_write])
on the given Unix file descriptor. *)
val wait_timed_read : Unix.file_descr -> float -> bool
(** See {!Thread.wait_timed_write}.*)
val wait_timed_write : Unix.file_descr -> float -> bool
(** Same as {!Thread.wait_read} and {!Thread.wait_write}, but wait for at most
the amount of time given as second argument (in seconds).
Return [true] if the file descriptor is ready for input/output
and [false] if the timeout expired. *)
val yield : unit -> unit
(** Re-schedule the calling thread without suspending it.
This function can be used to give scheduling hints,
telling the scheduler that now is a good time to
switch to other threads. *)
(** {6 Management of signals} *)
(** Signal handling follows the POSIX thread model: signals generated
by a thread are delivered to that thread; signals generated externally
are delivered to one of the threads that does not block it.
Each thread possesses a set of blocked signals, which can be modified
using {!Thread.sigmask}. This set is inherited at thread creation time.
Per-thread signal masks are supported only by the system thread library
under Unix, but not under Win32, nor by the VM thread library. *)
val sigmask : Signal.sigprocmask_command -> Signal.t list -> Signal.t list
(** [sigmask cmd sigs] changes the set of blocked signals for the
calling thread.
If [cmd] is [`Set], blocked signals are set to those in
the list [sigs].
If [cmd] is [`Block], the signals in [sigs] are added to
the set of blocked signals.
If [cmd] is [`Unblock], the signals in [sigs] are removed
from the set of blocked signals.
[sigmask] returns the set of previously blocked signals for the thread. *)
val wait_signal : Signal.t list -> int
(** [wait_signal sigs] suspends the execution of the calling thread
until the process receives one of the signals specified in the
list [sigs]. It then returns the number of the signal received.
Signal handlers attached to the signals in [sigs] will not
be invoked. The signals [sigs] are expected to be blocked before
calling [wait_signal]. *)
(** Jane Street extensions *)
(** [true] iff Thread.create has ever been called, even if there is
currently only one running thread. *)
val threads_have_been_created : unit -> bool
(** [num_threads ()] attempts to return the number of currently running
threads by parsing /proc. Since this is an operation frought with
potential failure, we return an option in cases of failure *)
val num_threads : unit -> int option
(** [block_forever ()] will block the calling thread forever. *)
val block_forever : unit -> 'a
(** {2 Non-portable pthread extensions}
The following operations may not be supported on all platforms. Before you
can use them, you must first check that they do not contain error values.
For example, if you wanted to use [setaffinity_self_exn] then you would
first do:
{[
let setaffinity_self_exn =
match Thread.setaffinity_self_exn with
| Ok f -> f
| Error err -> (* raise or provide a default implementation. *)
]}
If your application requires that one of these operations be present then,
you could just do this instead:
{[
let setaffinity_self_exn = Or_error.ok_exn Thread.setaffinity_self_exn
]}
*)
(** Sets the core affinity of the currently-running thread to the set
specified.
This function is implemented using [pthread_setaffinity_np(3)], when
available. See the man page for situations when this function may return an
error, and therefore raise. *)
val setaffinity_self_exn : (Int.Set.t -> unit) Or_error.t
(** Gets the core affinity of the currently-running thread.
This function is implemented using [pthread_getaffinity_np(3)], when
available. See the man page for situations when this function may return an
error, and therefore raise. *)
val getaffinity_self_exn : (unit -> Int.Set.t) Or_error.t