* threads.h: Include "coop-pthreads.h" when requested.
authorMarius Vollmer <mvo@zagadka.de>
Sun, 27 Oct 2002 20:12:37 +0000 (20:12 +0000)
committerMarius Vollmer <mvo@zagadka.de>
Sun, 27 Oct 2002 20:12:37 +0000 (20:12 +0000)
(scm_threads_make_mutex, scm_threads_lock_mutex,
scm_threads_unlock_mutex, scm_threads_monitor): Removed, they were
not implemented anyway.
(scm_init_thread_procs, scm_try_mutex,
scm_timed_condition_variable_wait,
scm_broadcast_condition_variable, scm_c_thread_exited_p,
scm_thread_exited_p): New prototypes.
(struct timespec): Define if not already defined.
(scm_t_mutex, scm_mutex_init, scm_mutex_lock, scm_mutex_trylock,
scm_mutex_unlock, scm_mutex_destroy, scm_t_cond, scm_cond_init,
scm_cond_wait, scm_cond_timedwait, scm_cond_signal,
scm_cond_broadcast, scm_cond_destroy): Declarations moved here and
deprecated.

* threads.c: Include <errno.h>.  Include "coop-pthreads.c" when
requested.
(scm_thread_exited_p): New.
(scm_try_mutex, scm_broadcast_condition_variable): Newly
registered procedures.
(scm_wait_condition_variable, scm_timed_wait_condition_variable):
Use the latter as the procedure for "wait-condition-variable",
thus offering a optional timeout parameter to Scheme.
(scm_wait_condition_variable): Implement in terms of
scm_timed_wait_condition_variable.
(scm_mutex_init, scm_mutex_lock, scm_mutex_trylock,
scm_mutex_unlock, scm_mutex_destroy, scm_cond_init,
scm_cond_wait, scm_cond_timedwait, scm_cond_signal,
scm_cond_broadcast, scm_cond_destroy): Implement in terms of
scm_make_mutex, etc, and deprecate.
(scm_init_threads): Do not create smobs, leave this to
scm_threads_init.  Do not include "threads.x" file.
(scm_init_thread_procs): New, include "threads.x" here.

* null-threads.h (scm_null_mutex, scm_null_mutex_init,
scm_null_mutex_lock, scm_null_mutex_unlock,
scm_null_mutex_destroy, scm_null_condvar, scm_null_condvar_init,
scm_null_condvar_wait, scm_null_condvar_signal,
scm_null_condvar_destroy): Removed.
(scm_mutex_init, scm_mutex_lock, scm_mutex_unlock, scm_cond_init,
scm_cond_wait, scm_cond_signal, scm_cond_broadcast,
scm_cond_destory): Do not define, they are now deprecated and
handled by threads.{h,c}.

* null-threads.c (scm_null_mutex, scm_null_cond): Define here.
(scm_threads_init): Create smobs here, using the appropriate
sizes.
(block): Removed, now unused.
(scm_c_thread_exited_p): New.
(scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock,
scm_null_mutex_destroy, scm_null_condvar_init,
scm_null_condvar_wait, scm_null_condvar_signal,
scm_null_condvar_destroy): Removed and updated users to do their
task directly.
(scm_try_mutex, timeval_subtract,
scm_timed_wait_condition_variable,
scm_broadcast_condition_variable): New.
(scm_wait_condition_variable): Removed.

* coop-threads.c (scm_threads_init): Create smobs here, using the
appropriate sizes.
(scm_c_thread_exited_p, scm_try_mutex,
scm_timed_wait_condition_variable,
scm_broadcast_condition_variable): New.
(scm_wait_condition_variable): Removed.

libguile/threads.c
libguile/threads.h

index f6762c9..9896d44 100644 (file)
@@ -60,6 +60,8 @@
     * second #inclusion
 */
 
+#include <errno.h>
+
 #include "libguile/_scm.h"
 #include "libguile/dynwind.h"
 #include "libguile/smob.h"
@@ -109,6 +111,15 @@ SCM_REGISTER_PROC(s_join_thread, "join-thread", 1, 0, 0, scm_join_thread);
 terminates, unless the target @var{thread} has already terminated.
 */
 
+SCM_DEFINE (scm_thread_exited_p, "thread-exited?", 1, 0, 0,
+           (SCM thread),
+           "Return @code{#t} iff @var{thread} has exited.\n")
+#define FUNC_NAME s_scm_thread_exited_p
+{
+  return SCM_BOOL (scm_c_thread_exited_p (thread));
+}
+#undef FUNC_NAME
+
 SCM_REGISTER_PROC(s_make_mutex, "make-mutex", 0, 0, 0, scm_make_mutex);
 /* Create a new mutex object. */
 
@@ -117,6 +128,10 @@ SCM_REGISTER_PROC(s_lock_mutex, "lock-mutex", 1, 0, 0, scm_lock_mutex);
 blocks until the mutex becomes available. The function returns when
 the calling thread owns the lock on @var{mutex}. */
 
+SCM_REGISTER_PROC(s_try_mutex, "try-mutex", 1, 0, 0, scm_try_mutex);
+/* Try to lock @var{mutex}. If the mutex is already locked by someone
+else, return @code{#f}.  Else lock the mutex and return @code{#t}. */
+
 SCM_REGISTER_PROC(s_unlock_mutex, "unlock-mutex", 1, 0, 0, scm_unlock_mutex);
 /* Unlocks @var{mutex} if the calling thread owns the lock on @var{mutex}.
 Calling unlock-mutex on a mutex not owned by the current thread results
@@ -125,33 +140,122 @@ blocked on @var{mutex} is awakened and grabs the mutex lock. */
 
 SCM_REGISTER_PROC(s_make_condition_variable, "make-condition-variable", 0, 0, 0, scm_make_condition_variable);
 
-SCM_REGISTER_PROC(s_wait_condition_variable, "wait-condition-variable", 2, 0, 0, scm_wait_condition_variable);
+SCM_REGISTER_PROC(s_wait_condition_variable, "wait-condition-variable", 2, 1, 0, scm_timed_wait_condition_variable);
 
 SCM_REGISTER_PROC(s_signal_condition_variable, "signal-condition-variable", 1, 0, 0, scm_signal_condition_variable);
 
+SCM_REGISTER_PROC(s_broadcast_condition_variable, "broadcast-condition-variable", 1, 0, 0, scm_broadcast_condition_variable);
+
+SCM
+scm_wait_condition_variable (SCM c, SCM m)
+{
+  return scm_timed_wait_condition_variable (c, m, SCM_UNDEFINED);
+}
+
 \f
 
 #ifdef USE_COOP_THREADS
 #include "libguile/coop-threads.c"
 #else
+#ifdef USE_COPT_THREADS
+#include "libguile/coop-pthreads.c"
+#else
 #include "libguile/null-threads.c"
 #endif
+#endif
+
+\f
+
+#if (SCM_ENABLE_DEPRECATED == 1)
+
+SCM_API int
+scm_mutex_init (scm_t_mutex *m)
+{
+  scm_gc_protect_object (m->m = scm_make_mutex ());
+  return 0;
+}
+
+SCM_API int
+scm_mutex_lock (scm_t_mutex *m)
+{
+  scm_lock_mutex (m->m);
+  return 0;
+}
+
+SCM_API int
+scm_mutex_trylock (scm_t_mutex *m)
+{
+  return SCM_FALSEP (scm_try_mutex (m->m))? EBUSY : 0;
+}
+
+SCM_API int
+scm_mutex_unlock (scm_t_mutex *m)
+{
+  scm_unlock_mutex (m->m);
+  return 0;
+}
+
+SCM_API int
+scm_mutex_destroy (scm_t_mutex *m)
+{
+  scm_gc_unprotect_object (m->m);
+  return 0;
+}
+
+SCM_API int
+scm_cond_init (scm_t_cond *c)
+{
+  scm_gc_protect_object (c->c = scm_make_condition_variable ());
+  return 0;
+}
+
+SCM_API int
+scm_cond_wait (scm_t_cond *c, scm_t_mutex *m)
+{
+  scm_wait_condition_variable (c->c, m->m);
+  return 0;
+}
+
+SCM_API int
+scm_cond_timedwait (scm_t_cond *c, scm_t_mutex *m,
+                   const struct timespec *t)
+{
+  return !SCM_FALSEP (scm_timed_wait_condition_variable (
+    c->c, m->m, scm_cons (scm_long2num (t->tv_sec),
+                         scm_long2num (t->tv_nsec/1000))));
+}
+
+SCM_API int
+scm_cond_signal (scm_t_cond *c)
+{
+  scm_signal_condition_variable (c->c);
+  return 0;
+}
+
+SCM_API int
+scm_cond_destroy (scm_t_cond *c)
+{
+  scm_gc_unprotect_object (c->c);
+  return 0;
+}
+
+#endif
 
 \f
 
 void
 scm_init_threads (SCM_STACKITEM *i)
 {
-  scm_tc16_thread = scm_make_smob_type ("thread", 0);
-  scm_tc16_mutex = scm_make_smob_type ("mutex", sizeof (scm_t_mutex));
-  scm_tc16_condvar = scm_make_smob_type ("condition-variable",
-                                        sizeof (scm_t_cond));
-                                        
-#include "libguile/threads.x"
   /* Initialize implementation specific details of the threads support */
   scm_threads_init (i);
 }
 
+void
+scm_init_thread_procs ()
+{
+#include "libguile/threads.x"
+}
+
 /*
   Local Variables:
   c-file-style: "gnu"
index c6a6630..82ebc3e 100644 (file)
@@ -71,12 +71,7 @@ SCM_API scm_t_bits scm_tc16_condvar;
 SCM_API void scm_threads_init (SCM_STACKITEM *);
 SCM_API void scm_threads_mark_stacks (void);
 SCM_API void scm_init_threads (SCM_STACKITEM *);
-
-/* */
-SCM_API SCM scm_threads_make_mutex (void);
-SCM_API SCM scm_threads_lock_mutex (SCM);
-SCM_API SCM scm_threads_unlock_mutex (SCM);
-SCM_API SCM scm_threads_monitor (void);
+SCM_API void scm_init_thread_procs (void);
 
 SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
                              scm_t_catch_handler handler, void *handler_data);
@@ -96,21 +91,68 @@ SCM_API SCM scm_call_with_new_thread (SCM argl);
 SCM_API SCM scm_join_thread (SCM t);
 SCM_API SCM scm_make_mutex (void);
 SCM_API SCM scm_lock_mutex (SCM m);
+SCM_API SCM scm_try_mutex (SCM m);
 SCM_API SCM scm_unlock_mutex (SCM m);
 SCM_API SCM scm_make_condition_variable (void);
 SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
+SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
+                                              SCM abstime);
 SCM_API SCM scm_signal_condition_variable (SCM cond);
+SCM_API SCM scm_broadcast_condition_variable (SCM cond);
 
 SCM_API SCM scm_current_thread (void);
 SCM_API SCM scm_all_threads (void);
 
+SCM_API int scm_c_thread_exited_p (SCM thread);
+SCM_API SCM scm_thread_exited_p (SCM thread);
+
 SCM_API scm_root_state *scm_i_thread_root (SCM thread);
 
+#ifndef HAVE_STRUCT_TIMESPEC
+/* POSIX.4 structure for a time value.  This is like a `struct timeval' but
+   has nanoseconds instead of microseconds.  */
+struct timespec
+{
+  long int tv_sec;             /* Seconds.  */
+  long int tv_nsec;            /* Nanoseconds.  */
+};
+#endif
+
 #ifdef USE_COOP_THREADS
 #include "libguile/coop-defs.h"
 #else
+#ifdef USE_COPT_THREADS
+#include "libguile/coop-pthreads.h"
+#else
 #include "libguile/null-threads.h"
 #endif
+#endif
+
+#if (SCM_ENABLE_DEPRECATED == 1)
+
+typedef struct {
+  SCM m;
+} scm_t_mutex;
+
+SCM_API int scm_mutex_init (scm_t_mutex *m);
+SCM_API int scm_mutex_lock (scm_t_mutex *m);
+SCM_API int scm_mutex_trylock (scm_t_mutex *m);
+SCM_API int scm_mutex_unlock (scm_t_mutex *m);
+SCM_API int scm_mutex_destroy (scm_t_mutex *m);
+
+typedef struct {
+  SCM c;
+} scm_t_cond;
+
+SCM_API int scm_cond_init (scm_t_cond *c);
+SCM_API int scm_cond_wait (scm_t_cond *c, scm_t_mutex *m);
+SCM_API int scm_cond_timedwait (scm_t_cond *c, scm_t_mutex *m,
+                               const struct timespec *abstime);
+SCM_API int scm_cond_signal (scm_t_cond *c);
+SCM_API int scm_cond_broadcast (scm_t_cond *c);
+SCM_API int scm_cond_destroy (scm_t_cond *c);
+
+#endif /* SCM_ENABLE_DEPRECATED == 1 */
 
 #endif  /* SCM_THREADS_H */