1996-09-04 07:17:43 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1997-11-25 07:21:57 -08:00
|
|
|
(* Objective Caml *)
|
1996-09-04 07:17:43 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy and Pascal Cuoq, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1997-11-25 07:21:57 -08:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1996-09-04 07:17:43 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* User-level threads *)
|
|
|
|
|
|
|
|
type t
|
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
external thread_initialize : unit -> unit = "caml_thread_initialize"
|
|
|
|
external thread_new : (unit -> unit) -> t = "caml_thread_new"
|
|
|
|
|
|
|
|
external yield : unit -> unit = "caml_thread_yield"
|
|
|
|
external self : unit -> t = "caml_thread_self"
|
|
|
|
external id : t -> int = "caml_thread_id"
|
|
|
|
external join : t -> unit = "caml_thread_join"
|
1996-09-04 07:17:43 -07:00
|
|
|
|
|
|
|
(* For new, make sure the function passed to thread_new never
|
|
|
|
raises an exception. *)
|
|
|
|
|
1997-11-25 07:21:57 -08:00
|
|
|
exception Thread_exit
|
|
|
|
|
1996-09-04 07:17:43 -07:00
|
|
|
let create fn arg =
|
|
|
|
thread_new
|
|
|
|
(fun () ->
|
|
|
|
try
|
1997-11-25 07:21:57 -08:00
|
|
|
fn arg; ()
|
|
|
|
with Thread_exit -> ()
|
|
|
|
| exn ->
|
|
|
|
Printf.eprintf "Uncaught exception in thread %d: %s\n"
|
|
|
|
(id(self())) (Printexc.to_string exn);
|
|
|
|
flush stderr)
|
|
|
|
|
|
|
|
let exit () = raise Thread_exit
|
|
|
|
|
|
|
|
(* Thread.kill is currently not implemented due to problems with
|
|
|
|
cleanup handlers on several platforms *)
|
|
|
|
|
|
|
|
let kill th = invalid_arg "Thread.kill: not implemented"
|
1996-09-04 07:17:43 -07:00
|
|
|
|
|
|
|
(* Preemption *)
|
|
|
|
|
|
|
|
let preempt signal = yield()
|
|
|
|
|
|
|
|
(* Initialization of the scheduler *)
|
|
|
|
|
|
|
|
let _ =
|
1997-11-17 05:04:18 -08:00
|
|
|
Sys.signal Sys.sigvtalrm (Sys.Signal_handle preempt);
|
1996-09-04 07:17:43 -07:00
|
|
|
thread_initialize()
|
|
|
|
|
1996-09-09 05:25:20 -07:00
|
|
|
(* Wait functions *)
|
|
|
|
|
|
|
|
let delay time = Unix.select [] [] [] time; ()
|
|
|
|
|
1996-09-04 07:17:43 -07:00
|
|
|
let wait_read fd = ()
|
|
|
|
let wait_write fd = ()
|
1996-09-09 05:25:20 -07:00
|
|
|
|
|
|
|
let wait_timed_read fd d =
|
|
|
|
match Unix.select [fd] [] [] d with ([], _, _) -> false | (_, _, _) -> true
|
|
|
|
let wait_timed_write fd d =
|
|
|
|
match Unix.select [] [fd] [] d with (_, [], _) -> false | (_, _, _) -> true
|
1997-04-23 11:55:42 -07:00
|
|
|
let select = Unix.select
|
1996-09-04 07:17:43 -07:00
|
|
|
|
|
|
|
let wait_pid p = Unix.waitpid [] p
|
1996-09-09 05:25:20 -07:00
|
|
|
|
1998-08-08 09:54:45 -07:00
|
|
|
let wait_signal sigs =
|
|
|
|
let gotsig = ref 0 in
|
|
|
|
let sem = Semaphore.create 0 in
|
|
|
|
let sighandler s = gotsig := s; Semaphore.post sem in
|
|
|
|
let oldhdlrs =
|
|
|
|
List.map (fun s -> Sys.signal s (Sys.Signal_handle sighandler)) sigs in
|
|
|
|
Semaphore.wait sem;
|
|
|
|
List.iter2 (fun s act -> Sys.signal s act; ()) sigs oldhdlrs;
|
|
|
|
!gotsig
|