* second #inclusion
*/
+#include <errno.h>
+
#include "libguile/_scm.h"
#include "libguile/dynwind.h"
#include "libguile/smob.h"
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. */
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
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"
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);
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 */