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 *)
|
|
|
|
(* under the terms of the GNU Library General Public License. *)
|
1995-10-30 02:21:56 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* User-level threads *)
|
|
|
|
|
|
|
|
type t
|
|
|
|
|
1996-04-03 02:02:34 -08:00
|
|
|
let critical_section = ref false
|
1995-11-14 09:11:31 -08:00
|
|
|
|
1996-04-18 09:32:04 -07:00
|
|
|
type resumption_status =
|
|
|
|
Resumed_wakeup
|
|
|
|
| Resumed_delay
|
|
|
|
| Resumed_join
|
1998-11-06 02:25:45 -08:00
|
|
|
| Resumed_io
|
1997-04-11 06:57:34 -07:00
|
|
|
| Resumed_select of
|
|
|
|
Unix.file_descr list * Unix.file_descr list * Unix.file_descr list
|
1996-04-29 06:21:09 -07:00
|
|
|
| Resumed_wait of int * Unix.process_status
|
1996-04-18 09:32:04 -07:00
|
|
|
|
1996-04-03 02:02:34 -08:00
|
|
|
(* It is mucho important that the primitives that reschedule are called
|
|
|
|
through an ML function call, not directly. That's because when such a
|
|
|
|
primitive returns, the bytecode interpreter is only semi-obedient:
|
|
|
|
it takes sp from the new thread, but keeps pc from the old thread.
|
|
|
|
But that's OK if all calls to rescheduling primitives are immediately
|
|
|
|
followed by a RETURN operation, which will restore the correct pc
|
1997-03-05 06:37:59 -08:00
|
|
|
from the stack. Furthermore, the RETURNs must all have the same
|
|
|
|
frame size, which means that both the primitives and their ML wrappers
|
|
|
|
must take exactly one argument. *)
|
1995-10-30 02:21:56 -08:00
|
|
|
|
1996-04-03 02:02:34 -08:00
|
|
|
external thread_initialize : unit -> unit = "thread_initialize"
|
|
|
|
external thread_new : (unit -> unit) -> t = "thread_new"
|
|
|
|
external thread_yield : unit -> unit = "thread_yield"
|
|
|
|
external thread_sleep : unit -> unit = "thread_sleep"
|
1998-11-06 02:25:45 -08:00
|
|
|
external thread_wait_read : Unix.file_descr -> unit = "thread_wait_read"
|
|
|
|
external thread_wait_write : Unix.file_descr -> unit = "thread_wait_write"
|
|
|
|
external thread_wait_timed_read :
|
|
|
|
Unix.file_descr * float -> resumption_status (* remember: 1 arg *)
|
|
|
|
= "thread_wait_timed_read"
|
|
|
|
external thread_wait_timed_write :
|
|
|
|
Unix.file_descr * float -> resumption_status (* remember: 1 arg *)
|
|
|
|
= "thread_wait_timed_write"
|
1997-04-11 06:57:34 -07:00
|
|
|
external thread_select :
|
|
|
|
Unix.file_descr list * Unix.file_descr list * (* remember: 1 arg *)
|
|
|
|
Unix.file_descr list * float -> resumption_status
|
|
|
|
= "thread_select"
|
1996-04-03 02:02:34 -08:00
|
|
|
external thread_join : t -> unit = "thread_join"
|
1996-04-18 09:32:04 -07:00
|
|
|
external thread_delay : float -> unit = "thread_delay"
|
1996-10-17 02:59:18 -07:00
|
|
|
external thread_wait_pid : int -> resumption_status = "thread_wait_pid"
|
1996-04-03 02:02:34 -08:00
|
|
|
external thread_wakeup : t -> unit = "thread_wakeup"
|
|
|
|
external thread_self : unit -> t = "thread_self"
|
|
|
|
external thread_kill : t -> unit = "thread_kill"
|
|
|
|
|
|
|
|
external id : t -> int = "thread_id"
|
|
|
|
|
|
|
|
(* In sleep() below, we rely on the fact that signals are detected
|
|
|
|
only at function applications and beginning of loops,
|
|
|
|
making all other operations atomic. *)
|
|
|
|
|
|
|
|
let sleep () = critical_section := false; thread_sleep()
|
|
|
|
let delay duration = thread_delay duration
|
|
|
|
let join th = thread_join th
|
|
|
|
let wakeup pid = thread_wakeup pid
|
|
|
|
let self () = thread_self()
|
|
|
|
let kill pid = thread_kill pid
|
|
|
|
let exit () = thread_kill(thread_self())
|
|
|
|
|
1997-04-11 06:57:34 -07:00
|
|
|
let select_aux arg = thread_select arg
|
|
|
|
|
|
|
|
let select readfds writefds exceptfds delay =
|
|
|
|
match select_aux (readfds, writefds, exceptfds, delay) with
|
|
|
|
Resumed_select(r, w, e) -> (r, w, e)
|
|
|
|
| _ -> ([], [], [])
|
|
|
|
|
1998-11-06 02:25:45 -08:00
|
|
|
let wait_read fd = thread_wait_read fd
|
|
|
|
let wait_write fd = thread_wait_write fd
|
|
|
|
|
|
|
|
let wait_timed_read_aux arg = thread_wait_timed_read arg
|
|
|
|
let wait_timed_write_aux arg = thread_wait_timed_write arg
|
|
|
|
|
1997-04-11 06:57:34 -07:00
|
|
|
let wait_timed_read fd delay =
|
1998-11-06 02:25:45 -08:00
|
|
|
match wait_timed_read_aux (fd, delay) with Resumed_io -> true | _ -> false
|
|
|
|
|
1997-04-11 06:57:34 -07:00
|
|
|
let wait_timed_write fd delay =
|
1998-11-06 02:25:45 -08:00
|
|
|
match wait_timed_write_aux (fd, delay) with Resumed_io -> true | _ -> false
|
1997-04-11 06:57:34 -07:00
|
|
|
|
1996-10-17 02:59:18 -07:00
|
|
|
let wait_pid_aux pid = thread_wait_pid pid
|
1996-04-18 09:32:04 -07:00
|
|
|
|
1996-10-17 02:59:18 -07:00
|
|
|
let wait_pid pid =
|
|
|
|
match wait_pid_aux pid with
|
|
|
|
Resumed_wait(pid, status) -> (pid, status)
|
|
|
|
| _ -> invalid_arg "Thread.wait_pid"
|
1996-04-18 09:32:04 -07:00
|
|
|
|
1998-08-08 09:54:14 -07:00
|
|
|
let wait_signal sigs =
|
|
|
|
let gotsig = ref 0 in
|
|
|
|
let self = thread_self() in
|
|
|
|
let sighandler s = gotsig := s; wakeup self in
|
|
|
|
let oldhdlrs =
|
|
|
|
List.map (fun s -> Sys.signal s (Sys.Signal_handle sighandler)) sigs in
|
|
|
|
if !gotsig = 0 then sleep();
|
1998-11-12 06:51:27 -08:00
|
|
|
List.iter2 Sys.set_signal sigs oldhdlrs;
|
1998-08-08 09:54:14 -07:00
|
|
|
!gotsig
|
|
|
|
|
1997-03-05 06:37:59 -08:00
|
|
|
(* For Thread.create, make sure the function passed to thread_new
|
|
|
|
always terminates by calling Thread.exit. *)
|
1995-10-30 02:21:56 -08:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let create fn arg =
|
1995-10-30 02:21:56 -08:00
|
|
|
thread_new
|
|
|
|
(fun () ->
|
|
|
|
try
|
1996-04-03 02:02:34 -08:00
|
|
|
Printexc.print fn arg; exit()
|
1995-10-30 02:21:56 -08:00
|
|
|
with x ->
|
1996-04-03 02:02:34 -08:00
|
|
|
flush stdout; flush stderr; exit())
|
1995-11-14 09:11:31 -08:00
|
|
|
|
|
|
|
(* Preemption *)
|
|
|
|
|
1996-04-03 02:02:34 -08:00
|
|
|
let preempt signal =
|
|
|
|
if !critical_section then () else thread_yield()
|
1995-10-30 02:21:56 -08:00
|
|
|
|
|
|
|
(* Initialization of the scheduler *)
|
|
|
|
|
|
|
|
let _ =
|
1998-11-12 06:51:27 -08:00
|
|
|
Sys.set_signal Sys.sigvtalrm (Sys.Signal_handle preempt);
|
1995-10-30 02:21:56 -08:00
|
|
|
thread_initialize()
|