From ee63e8d356b5717a32b0739fbe957f8e92d6e2c1 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Tue, 5 Mar 1996 10:12:07 +0000 Subject: [PATCH] Ajout de la fonction Thread.id git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@679 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02 --- otherlibs/threads/scheduler.c | 12 ++++++++++-- otherlibs/threads/thread.ml | 2 ++ otherlibs/threads/thread.mli | 22 ++++++++++++---------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/otherlibs/threads/scheduler.c b/otherlibs/threads/scheduler.c index c005970ed..be90f1972 100644 --- a/otherlibs/threads/scheduler.c +++ b/otherlibs/threads/scheduler.c @@ -176,9 +176,17 @@ value thread_new(clos) /* ML */ return (value) th; } +/* Return the thread identifier */ + +value thread_id(th) /* ML */ + value th; +{ + return ((struct thread_struct *)th)->ident; +} + /* Return the current time as a floating-point number */ -double timeofday() +static double timeofday() { struct timeval tv; gettimeofday(&tv, NULL); @@ -190,7 +198,7 @@ double timeofday() #define FOREACH_THREAD(x) x = curr_thread; do { x = x->next; #define END_FOREACH(x) } while (x != curr_thread) -void schedule_thread() +static void schedule_thread() { thread_t run_thread, th; fd_set readfds, writefds; diff --git a/otherlibs/threads/thread.ml b/otherlibs/threads/thread.ml index 36f31eba7..04161cca0 100644 --- a/otherlibs/threads/thread.ml +++ b/otherlibs/threads/thread.ml @@ -39,6 +39,8 @@ 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. *) diff --git a/otherlibs/threads/thread.mli b/otherlibs/threads/thread.mli index 8da79f664..6b14a4a1a 100644 --- a/otherlibs/threads/thread.mli +++ b/otherlibs/threads/thread.mli @@ -14,7 +14,7 @@ (* Module [Thread]: user-level lightweight threads *) type t - (* The type of thread identifiers. *) + (* The type of thread handles. *) (** Thread creation and termination *) @@ -22,7 +22,7 @@ val new : ('a -> 'b) -> 'a -> t (* [new 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 [new] - returns the identifier of the newly created thread. + 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, @@ -30,11 +30,15 @@ val new : ('a -> 'b) -> 'a -> t result of the application [funct arg] is discarded and not directly accessible to the parent thread. *) val self : unit -> t - (* Return the identifier of the calling thread. *) + (* Return the thread currently executing. *) +external id : t -> int = "thread_id" + (* 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 calling thread. *) + (* Terminate prematurely the currently executing thread. *) val kill : t -> unit - (* Terminate prematurely the thread whose identifier is given. *) + (* Terminate prematurely the thread whose handle is given. *) (** Suspending threads *) @@ -84,8 +88,6 @@ val sleep : unit -> unit [critical_section] and suspending the calling thread is an atomic operation. *) val wakeup : t -> unit - (* Reactivate the thread whose identifier is given. This thread - is assumed to be suspended on a call to [sleep], [delay], - [wait_inchan] or [wait_descr]. After the call to [wakeup], - the suspended thread will resume execution at some future time. - [wakeup] does nothing if the thread was not suspended. *) + (* Reactivate the given thread. This thread is assumed to + be suspended on a call to [sleep]. After the call to [wakeup], + the suspended thread will resume execution at some future time. *)