1995-10-30 02:21:56 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-10-30 02:21:56 -08:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../../LICENSE. *)
|
1995-10-30 02:21:56 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Lightweight threads. *)
|
1995-10-30 02:21:56 -08:00
|
|
|
|
|
|
|
type t
|
2001-12-04 08:16:05 -08:00
|
|
|
(** The type of thread handles. *)
|
1995-11-15 08:40:01 -08:00
|
|
|
|
|
|
|
|
2001-12-28 15:14:14 -08:00
|
|
|
(** {6 Thread creation and termination} *)
|
2001-10-29 12:07:20 -08:00
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val create : ('a -> 'b) -> 'a -> t
|
2001-10-29 12:07:20 -08:00
|
|
|
(** [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. *)
|
|
|
|
|
1996-04-03 02:02:34 -08:00
|
|
|
val self : unit -> t
|
2001-12-04 08:16:05 -08:00
|
|
|
(** Return the thread currently executing. *)
|
2001-10-29 12:07:20 -08:00
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
external id : t -> int = "thread_id"
|
2001-10-29 12:07:20 -08:00
|
|
|
(** 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. *)
|
|
|
|
|
1996-04-03 02:02:34 -08:00
|
|
|
val exit : unit -> unit
|
2001-12-04 08:16:05 -08:00
|
|
|
(** Terminate prematurely the currently executing thread. *)
|
2001-10-29 12:07:20 -08:00
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val kill : t -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Terminate prematurely the thread whose handle is given.
|
|
|
|
This functionality is available only with bytecode-level threads. *)
|
1995-11-15 08:40:01 -08:00
|
|
|
|
2001-12-28 15:14:14 -08:00
|
|
|
(** {6 Suspending threads} *)
|
1995-11-15 08:40:01 -08:00
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val delay : float -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** [delay d] suspends the execution of the calling thread for
|
|
|
|
[d] seconds. The other program threads continue to run during
|
|
|
|
this time. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val join : t -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** [join th] suspends the execution of the calling thread
|
|
|
|
until the thread [th] has terminated. *)
|
|
|
|
|
1996-04-18 09:32:04 -07:00
|
|
|
val wait_read : Unix.file_descr -> unit
|
2001-12-04 08:16:05 -08:00
|
|
|
(** See {!Thread.wait_write}.*)
|
|
|
|
|
|
|
|
val wait_write : Unix.file_descr -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** 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. *)
|
|
|
|
|
2001-09-06 01:52:32 -07:00
|
|
|
val wait_timed_read : Unix.file_descr -> float -> bool
|
2004-07-13 05:25:21 -07:00
|
|
|
(** See {!Thread.wait_timed_write}.*)
|
2001-12-04 08:16:05 -08:00
|
|
|
|
|
|
|
val wait_timed_write : Unix.file_descr -> float -> bool
|
2001-10-29 12:07:20 -08:00
|
|
|
(** 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. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val select :
|
|
|
|
Unix.file_descr list -> Unix.file_descr list -> Unix.file_descr list ->
|
|
|
|
float ->
|
|
|
|
Unix.file_descr list * Unix.file_descr list * Unix.file_descr list
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Suspend the execution of the calling thead until input/output
|
|
|
|
becomes possible on the given Unix file descriptors.
|
|
|
|
The arguments and results have the same meaning as for
|
|
|
|
{!Unix.select}. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val wait_pid : int -> int * Unix.process_status
|
2001-10-29 12:07:20 -08:00
|
|
|
(** [wait_pid p] suspends the execution of the calling thread
|
|
|
|
until the Unix process specified by the process identifier [p]
|
|
|
|
terminates. A pid [p] of [-1] means wait for any child.
|
|
|
|
A pid of [0] means wait for any child in the same process group
|
|
|
|
as the current process. Negative pid arguments represent
|
|
|
|
process groups. Returns the pid of the child caught and
|
|
|
|
its termination status, as per {!Unix.wait}. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val wait_signal : int list -> int
|
2001-10-29 12:07:20 -08:00
|
|
|
(** [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. Do not call [wait_signal] concurrently
|
|
|
|
from several threads on the same signals. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val yield : unit -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** 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. *)
|
1999-12-23 09:34:19 -08:00
|
|
|
|
2001-10-30 08:52:43 -08:00
|
|
|
(**/**)
|
1996-04-03 02:02:34 -08:00
|
|
|
|
2001-12-28 15:14:14 -08:00
|
|
|
(** {6 Synchronization primitives}
|
2001-10-29 12:07:20 -08:00
|
|
|
|
|
|
|
The following primitives provide the basis for implementing
|
1996-04-03 02:02:34 -08:00
|
|
|
synchronization functions between threads. Their direct use is
|
|
|
|
discouraged, as they are very low-level and prone to race conditions
|
2001-10-29 12:07:20 -08:00
|
|
|
and deadlocks. The modules {!Mutex}, {!Condition} and {!Event}
|
1996-04-03 02:02:34 -08:00
|
|
|
provide higher-level synchronization primitives. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val critical_section : bool ref
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Setting this reference to [true] deactivate thread preemption
|
|
|
|
(the timer interrupt that transfers control from thread to thread),
|
|
|
|
causing the current thread to run uninterrupted until
|
|
|
|
[critical_section] is reset to [false] or the current thread
|
|
|
|
explicitely relinquishes control using [sleep], [delay],
|
|
|
|
[wait_inchan] or [wait_descr]. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val sleep : unit -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Suspend the calling thread until another thread reactivates it
|
|
|
|
using {!Thread.wakeup}. Just before suspending the thread,
|
|
|
|
{!Thread.critical_section} is reset to [false]. Resetting
|
|
|
|
{!Thread.critical_section} and suspending the calling thread is an
|
|
|
|
atomic operation. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val wakeup : t -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Reactivate the given thread. After the call to [wakeup],
|
|
|
|
the suspended thread will resume execution at some future time. *)
|
|
|
|
|