From 29717c89316bb94c95a90107553a47f5ee5fb4e3 Mon Sep 17 00:00:00 2001 From: Mikael Djurfeldt Date: Wed, 18 Dec 2002 13:42:58 +0000 Subject: [PATCH] * Makefile.am (modinclude_HEADERS): Added threads-plugin.h. (EXTRA_DIST): Added threads-plugin.c. * threads-plugin.h, threads-plugin.c: New files. * threads.h: #include "libguile/threads-plugin.h". * threads.c: #include "libguile/threads-plugin.c". * pthread-threads.c: Temporarily remove debugging functions. * threads.c, threads.h (scm_yield): Added back. --- libguile/ChangeLog | 17 + libguile/Makefile.am | 4 +- libguile/pthread-threads.c | 424 ++++-------------- libguile/pthread-threads.h | 58 +-- .../{pthread-threads.c => threads-plugin.c} | 226 ++-------- libguile/threads-plugin.h | 100 +++++ libguile/threads.c | 10 + libguile/threads.h | 3 + 8 files changed, 266 insertions(+), 576 deletions(-) rewrite libguile/pthread-threads.c (68%) copy libguile/{pthread-threads.c => threads-plugin.c} (50%) create mode 100644 libguile/threads-plugin.h diff --git a/libguile/ChangeLog b/libguile/ChangeLog index 79cc105eb..b7c35e25e 100644 --- a/libguile/ChangeLog +++ b/libguile/ChangeLog @@ -1,3 +1,20 @@ +2002-12-18 Mikael Djurfeldt + + Partial introduction of real plugin interface. + + * Makefile.am (modinclude_HEADERS): Added threads-plugin.h. + (EXTRA_DIST): Added threads-plugin.c. + + * threads-plugin.h, threads-plugin.c: New files. + + * threads.h: #include "libguile/threads-plugin.h". + + * threads.c: #include "libguile/threads-plugin.c". + + * pthread-threads.c: Temporarily remove debugging functions. + + * threads.c, threads.h (scm_yield): Added back. + 2002-12-18 Mikael Djurfeldt * threads.c (really_launch): Detach before unlocking diff --git a/libguile/Makefile.am b/libguile/Makefile.am index 5d5ff2290..38ada809e 100644 --- a/libguile/Makefile.am +++ b/libguile/Makefile.am @@ -145,7 +145,7 @@ pkginclude_HEADERS = gh.h modincludedir = $(includedir)/libguile modinclude_HEADERS = __scm.h alist.h arbiters.h async.h backtrace.h boolean.h \ chars.h continuations.h convert.h debug.h \ - debug-malloc.h null-threads.h pthread-threads.h \ + debug-malloc.h threads-plugin.h null-threads.h pthread-threads.h \ deprecation.h dynl.h dynwind.h environments.h eq.h error.h eval.h \ evalext.h extensions.h feature.h filesys.h fluids.h fports.h gc.h \ gdb_interface.h gdbint.h \ @@ -173,7 +173,7 @@ EXTRA_DIST = ChangeLog-gh ChangeLog-scm ChangeLog-threads \ ChangeLog-1996-1999 ChangeLog-2000 cpp_signal.c \ cpp_errno.c cpp_err_symbols.in cpp_err_symbols.c \ cpp_sig_symbols.c cpp_sig_symbols.in cpp_cnvt.awk \ - c-tokenize.lex + c-tokenize.lex threads-plugin.c # $(DOT_DOC_FILES) $(EXTRA_DOT_DOC_FILES) \ # guile-procedures.txt guile.texi diff --git a/libguile/pthread-threads.c b/libguile/pthread-threads.c dissimilarity index 68% index 0fce9ecbf..ea2cc9888 100644 --- a/libguile/pthread-threads.c +++ b/libguile/pthread-threads.c @@ -1,337 +1,87 @@ -/* Copyright (C) 2002 Free Software Foundation, Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this software; see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * As a special exception, the Free Software Foundation gives permission - * for additional uses of the text contained in its release of GUILE. - * - * The exception is that, if you link the GUILE library with other files - * to produce an executable, this does not by itself cause the - * resulting executable to be covered by the GNU General Public License. - * Your use of that executable is in no way restricted on account of - * linking the GUILE library code into it. - * - * This exception does not however invalidate any other reasons why - * the executable file might be covered by the GNU General Public License. - * - * This exception applies only to the code released by the - * Free Software Foundation under the name GUILE. If you copy - * code from other Free Software Foundation releases into a copy of - * GUILE, as the General Public License permits, the exception does - * not apply to the code that you add in this way. To avoid misleading - * anyone as to the status of such modified files, you must delete - * this exception notice from them. - * - * If you write modifications of your own for GUILE, it is your choice - * whether to permit this exception to apply to your modifications. - * If you do not wish that, delete this exception notice. */ - - - - -#include "libguile/scmconfig.h" - -scm_t_mutexattr scm_i_plugin_mutex; - -scm_t_mutexattr scm_i_plugin_rec_mutex; - -#if !defined (SCM_MUTEX_RECURSIVE) || defined (SCM_DEBUG_THREADS) - -typedef struct rec_mutex { -#ifdef SCM_DEBUG_THREADS - int kind; -#endif - scm_t_mutex mutex; - scm_thread *owner; - int count; -} rec_mutex; - -/* Mutex for recursive mutex administration */ -static scm_t_mutex rec_mutex_mutex; - -#ifdef SCM_DEBUG_THREADS - -#define FAST_MUTEX 1 -#define REC_MUTEX 2 - -typedef struct mutex { - int kind; - pthread_mutex_t mutex; - scm_thread *owner; - int count; -} mutex; - -static pthread_mutex_t mutex_mutex; - -int -scm_i_plugin_mutex_init (scm_t_mutex *mx, const scm_t_mutexattr *a) -{ - mutex *m = (mutex *) mx; - pthread_mutex_init (&m->mutex, &scm_i_plugin_mutex); - m->owner = 0; - m->count = 0; - m->kind = FAST_MUTEX; - return 0; -} - -int -scm_i_plugin_mutex_lock (scm_t_mutex *mx) -{ - mutex *m = (mutex *) mx; - scm_thread *t = SCM_CURRENT_THREAD; - pthread_mutex_lock (&mutex_mutex); - if (m->kind != FAST_MUTEX) - { - fprintf (stderr, - m->kind == REC_MUTEX - ? "locking wrong mutex type\n" - : "locking uninitialized mutex\n"); - abort (); - } - if (m->owner == t) - { - fprintf (stderr, "locking mutex already locked by self\n"); - abort (); - } - pthread_mutex_unlock (&mutex_mutex); - pthread_mutex_lock (&m->mutex); - m->count = 1; - m->owner = t; - return 0; -} - -int -scm_i_plugin_mutex_unlock (scm_t_mutex *mx) -{ - mutex *m = (mutex *) mx; - pthread_mutex_lock (&mutex_mutex); - if (m->kind != FAST_MUTEX) - { - fprintf (stderr, - m->kind == REC_MUTEX - ? "locking wrong mutex type\n" - : "locking uninitialized mutex\n"); - abort (); - } - if (m->count != 1) - { - fprintf (stderr, - m->count == 0 - ? "unlocking unlocked mutex\n" - : "bogus internal state"); - abort (); - } - m->owner = 0; - m->count = 0; - pthread_mutex_unlock (&m->mutex); - pthread_mutex_unlock (&mutex_mutex); - return 0; -} - -int -scm_i_plugin_cond_wait (scm_t_cond *c, scm_t_mutex *mx) -{ - mutex *m = (mutex *) mx; - int res; - if (m->owner != SCM_CURRENT_THREAD || m->count != 1) - { - fprintf (stderr, "mutex not locked by self\n"); - abort (); - } - res = pthread_cond_wait ((pthread_cond_t *) c, &m->mutex); - if (m->owner != 0 || m->count != 0) - { - fprintf (stderr, "strange internal state\n"); - abort (); - } - m->owner = SCM_CURRENT_THREAD; - m->count = 1; - return res; -} - -int -scm_i_plugin_cond_timedwait (scm_t_cond *c, - scm_t_mutex *mx, - const struct timespec *t) -{ - mutex *m = (mutex *) mx; - return pthread_cond_timedwait ((pthread_cond_t *) c, &m->mutex, t); -} - -void -scm_i_assert_heap_locked () -{ - mutex *m = (mutex *) &SCM_CURRENT_THREAD->heap_mutex; - pthread_mutex_lock (&mutex_mutex); - if (gc_running) - { - fprintf (stderr, "thread running during gc\n"); - abort (); - } - if (m->count != 1) - { - fprintf (stderr, "mutex not locked\n"); - abort (); - } - pthread_mutex_unlock (&mutex_mutex); -} - -#endif - -/* The following section belongs in threads.c, or rather - thread-plugin.c. It is generic and not tied to any particular - thread library. */ - -int -scm_i_plugin_rec_mutex_init (scm_t_rec_mutex *mx, const scm_t_mutexattr *a) -{ - rec_mutex *m = (rec_mutex *) mx; - scm_i_plugin_mutex_init (&m->mutex, &scm_i_plugin_mutex); - m->owner = 0; - m->count = 0; -#ifdef SCM_DEBUG_THREADS - m->kind = REC_MUTEX; -#endif - return 0; -} - -int -scm_i_plugin_rec_mutex_lock (scm_t_rec_mutex *mx) -{ - rec_mutex *m = (rec_mutex *) mx; - scm_thread *t = SCM_CURRENT_THREAD; - scm_i_plugin_mutex_lock (&rec_mutex_mutex); -#ifdef SCM_DEBUG_THREADS - if (m->kind != REC_MUTEX) - { - fprintf (stderr, - m->kind == FAST_MUTEX - ? "locking wrong mutex type\n" - : "locking uninitialized mutex\n"); - abort (); - } -#endif - if (m->count && m->owner == t) - { - ++m->count; - scm_i_plugin_mutex_unlock (&rec_mutex_mutex); - } - else - { - scm_i_plugin_mutex_unlock (&rec_mutex_mutex); - scm_i_plugin_mutex_lock (&m->mutex); - m->count = 1; - m->owner = t; - } - return 0; -} - -int -scm_i_plugin_rec_mutex_trylock (scm_t_rec_mutex *mx) -{ - rec_mutex *m = (rec_mutex *) mx; - scm_thread *t = SCM_CURRENT_THREAD; - scm_i_plugin_mutex_lock (&rec_mutex_mutex); - if (m->owner != 0 && m->owner != t) - return EBUSY; - else if (m->count) - { - ++m->count; - scm_i_plugin_mutex_unlock (&rec_mutex_mutex); - } - else - { - scm_i_plugin_mutex_unlock (&rec_mutex_mutex); - scm_i_plugin_mutex_lock (&m->mutex); - m->count = 1; - m->owner = t; - } - return 0; -} - -int -scm_i_plugin_rec_mutex_unlock (scm_t_rec_mutex *mx) -{ - rec_mutex *m = (rec_mutex *) mx; - scm_i_plugin_mutex_lock (&rec_mutex_mutex); -#ifdef SCM_DEBUG_THREADS - if (m->kind != REC_MUTEX) - { - fprintf (stderr, - m->kind == FAST_MUTEX - ? "locking wrong mutex type\n" - : "locking uninitialized mutex\n"); - abort (); - } - if (m->count == 0) - { - fprintf (stderr, "unlocking unlocked mutex\n"); - abort (); - } -#endif - if (!--m->count) - { - m->owner = 0; - scm_i_plugin_mutex_unlock (&m->mutex); - } - scm_i_plugin_mutex_unlock (&rec_mutex_mutex); - return 0; -} - -#endif /* !PTHREAD_MUTEX_RECURSIVE */ - -#ifndef SCM_MUTEXATTR_SETTYPE_DECLARED -int pthread_mutexattr_settype (pthread_mutexattr_t *, int); -#endif - -void -scm_init_pthread_threads () -{ - size_t mutex_size, rec_mutex_size; - pthread_mutexattr_init (&scm_i_plugin_mutex); -#ifdef SCM_MUTEX_FAST - pthread_mutexattr_settype (&scm_i_plugin_mutex, SCM_MUTEX_FAST); -#endif - -#if defined (SCM_MUTEX_RECURSIVE) && !defined (SCM_DEBUG_THREADS) - rec_mutex_size = sizeof (pthread_mutex_t); - pthread_mutexattr_init (&scm_i_plugin_rec_mutex); - pthread_mutexattr_settype (&scm_i_plugin_rec_mutex, SCM_MUTEX_RECURSIVE); -#else - /* If PTHREAD_MUTEX_RECURSIVE is not defined, - scm_i_plugin_rec_mutex_init won't pay attention to it anyway - */ - rec_mutex_size = sizeof (rec_mutex); - scm_i_plugin_mutex_init (&rec_mutex_mutex, &scm_i_plugin_mutex); -#endif -#ifdef SCM_DEBUG_THREADS - mutex_size = sizeof (mutex); - pthread_mutex_init (&mutex_mutex, &scm_i_plugin_mutex); -#else - mutex_size = sizeof (pthread_mutex_t); -#endif - if (mutex_size > SCM_MUTEX_MAXSIZE || rec_mutex_size > SCM_REC_MUTEX_MAXSIZE) - { - fprintf (stderr, "Internal error: Need to upgrade mutex size\n"); - abort (); - } -} - -/* - Local Variables: - c-file-style: "gnu" - End: -*/ +/* Copyright (C) 2002 Free Software Foundation, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + * + * As a special exception, the Free Software Foundation gives permission + * for additional uses of the text contained in its release of GUILE. + * + * The exception is that, if you link the GUILE library with other files + * to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the GUILE library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the + * Free Software Foundation under the name GUILE. If you copy + * code from other Free Software Foundation releases into a copy of + * GUILE, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for GUILE, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. */ + + + + +#include "libguile/scmconfig.h" + +/* Should go to threads-plugin */ +scm_t_mutexattr scm_i_plugin_mutex; + +scm_t_mutexattr scm_i_plugin_rec_mutex; + +#ifndef SCM_MUTEXATTR_SETTYPE_DECLARED +int pthread_mutexattr_settype (pthread_mutexattr_t *, int); +#endif + +void +scm_init_pthread_threads () +{ + pthread_mutexattr_init (&scm_i_plugin_mutex); +#ifdef SCM_MUTEX_FAST + pthread_mutexattr_settype (&scm_i_plugin_mutex, SCM_MUTEX_FAST); +#endif + /* These values should be passed in a structure. */ + scm_i_plugin_mutex_size = sizeof (pthread_mutex_t); + scm_i_plugin_mutex_init = (scm_t_mutex_init) pthread_mutex_init; + scm_i_plugin_mutex_lock = (scm_t_mutex_lock) pthread_mutex_lock; + scm_i_plugin_mutex_unlock = (scm_t_mutex_unlock) pthread_mutex_unlock; + +#if defined (SCM_MUTEX_RECURSIVE) && !defined (SCM_DEBUG_THREADS) + pthread_mutexattr_init (&scm_i_plugin_rec_mutex); + pthread_mutexattr_settype (&scm_i_plugin_rec_mutex, SCM_MUTEX_RECURSIVE); + scm_i_plugin_rec_mutex_size = sizeof (pthread_mutex_t); + scm_i_plugin_rec_mutex_init = (scm_t_rec_mutex_init) pthread_mutex_init; + scm_i_plugin_rec_mutex_destroy = (scm_t_rec_mutex_destroy) pthread_mutex_destroy; + scm_i_plugin_rec_mutex_lock = (scm_t_rec_mutex_lock) pthread_mutex_lock; + scm_i_plugin_rec_mutex_trylock = (scm_t_rec_mutex_trylock) pthread_mutex_trylock; + scm_i_plugin_rec_mutex_unlock = (scm_t_rec_mutex_unlock) pthread_mutex_unlock; +#endif + + scm_init_threads_plugin (); +} + +/* + Local Variables: + c-file-style: "gnu" + End: +*/ diff --git a/libguile/pthread-threads.h b/libguile/pthread-threads.h index 2af178ae6..2595e5b61 100644 --- a/libguile/pthread-threads.h +++ b/libguile/pthread-threads.h @@ -54,6 +54,8 @@ #include #include +#include "libguile/threads-plugin.h" + /* MDJ 021209 : The separation of the plugin interface and the low-level C API (currently in threads.h) needs to be completed in a sensible way. @@ -69,73 +71,17 @@ #define scm_i_plugin_thread_self pthread_self #define scm_i_plugin_thread_yield sched_yield -/* Size is checked in scm_init_pthread_threads */ -#define SCM_MUTEX_MAXSIZE (9 * sizeof (long)) -typedef struct { char _[SCM_MUTEX_MAXSIZE]; } scm_t_mutex; -#define scm_t_mutexattr pthread_mutexattr_t - extern scm_t_mutexattr scm_i_plugin_mutex; /* The "fast" mutex. */ -/* This debug stuff made things a bit messy. This needs some - reorganization. */ -#ifdef SCM_DEBUG_THREADS -int scm_i_plugin_mutex_init (scm_t_mutex *, const scm_t_mutexattr *); -int scm_i_plugin_mutex_lock (scm_t_mutex *); -int scm_i_plugin_mutex_unlock (scm_t_mutex *); -#else -#define scm_i_plugin_mutex_init(m,a) \ - pthread_mutex_init ((pthread_mutex_t *) (m), (a)) -#define scm_i_plugin_mutex_lock(m) \ - pthread_mutex_lock ((pthread_mutex_t *) (m)) -#define scm_i_plugin_mutex_unlock(m) \ - pthread_mutex_unlock ((pthread_mutex_t *) (m)) -#endif #define scm_i_plugin_mutex_destroy(m) \ pthread_mutex_destroy ((pthread_mutex_t *) (m)) #define scm_i_plugin_mutex_trylock(m) \ pthread_mutex_trylock ((pthread_mutex_t *) (m)) -/* Size is checked in scm_init_pthread_threads */ -#define SCM_REC_MUTEX_MAXSIZE (SCM_MUTEX_MAXSIZE + 3 * sizeof (long)) -typedef struct { char _[SCM_REC_MUTEX_MAXSIZE]; } scm_t_rec_mutex; - extern scm_t_mutexattr scm_i_plugin_rec_mutex; -#if defined (SCM_MUTEX_RECURSIVE) && !defined (SCM_DEBUG_THREADS) -/* pthreads has recursive mutexes! */ -#define scm_i_plugin_rec_mutex_init(m,a) \ - pthread_mutex_init ((pthread_mutex_t *) (m), (a)) -#define scm_i_plugin_rec_mutex_destroy(m) \ - pthread_mutex_destroy ((pthread_mutex_t *) (m)) -#define scm_i_plugin_rec_mutex_lock(m) \ - pthread_mutex_lock ((pthread_mutex_t *) (m)) -#define scm_i_plugin_rec_mutex_trylock(m) \ - pthread_mutex_trylock ((pthread_mutex_t *) (m)) -#define scm_i_plugin_rec_mutex_unlock(m) \ - pthread_mutex_unlock ((pthread_mutex_t *) (m)) -#else -int scm_i_plugin_rec_mutex_init (scm_t_rec_mutex *, const scm_t_mutexattr *); -#define scm_i_plugin_rec_mutex_destroy(mx) do { (void) (mx); } while (0) -int scm_i_plugin_rec_mutex_lock (scm_t_rec_mutex *); -int scm_i_plugin_rec_mutex_trylock (scm_t_rec_mutex *); -int scm_i_plugin_rec_mutex_unlock (scm_t_rec_mutex *); -#endif - -#define scm_t_cond pthread_cond_t - #define scm_i_plugin_cond_init pthread_cond_init #define scm_i_plugin_cond_destroy pthread_cond_destroy -#ifdef SCM_DEBUG_THREADS -int scm_i_plugin_cond_wait (scm_t_cond *, scm_t_mutex *); -int scm_i_plugin_cond_timedwait (scm_t_cond *, - scm_t_mutex *, - const struct timespec *); -#else -#define scm_i_plugin_cond_wait(c, m) \ - pthread_cond_wait ((c), (pthread_mutex_t *) (m)) -#define scm_i_plugin_cond_timedwait(c, m, t) \ - pthread_cond_timedwait ((c), (pthread_mutex_t *) (m), (t)) -#endif #define scm_i_plugin_cond_signal pthread_cond_signal #define scm_i_plugin_cond_broadcast pthread_cond_broadcast diff --git a/libguile/pthread-threads.c b/libguile/threads-plugin.c similarity index 50% copy from libguile/pthread-threads.c copy to libguile/threads-plugin.c index 0fce9ecbf..e6671daf1 100644 --- a/libguile/pthread-threads.c +++ b/libguile/threads-plugin.c @@ -42,13 +42,23 @@ -#include "libguile/scmconfig.h" - -scm_t_mutexattr scm_i_plugin_mutex; - -scm_t_mutexattr scm_i_plugin_rec_mutex; - -#if !defined (SCM_MUTEX_RECURSIVE) || defined (SCM_DEBUG_THREADS) +int scm_i_plugin_mutex_size = 0; +int (*scm_i_plugin_mutex_init) (scm_t_mutex *, const scm_t_mutexattr *); +int (*scm_i_plugin_mutex_lock) (scm_t_mutex *); +int (*scm_i_plugin_mutex_unlock) (scm_t_mutex *); + +int scm_i_plugin_rec_mutex_size = 0; +int (*scm_i_plugin_rec_mutex_init) (scm_t_rec_mutex *, + const scm_t_mutexattr *); +int (*scm_i_plugin_rec_mutex_destroy) (scm_t_rec_mutex *); +int (*scm_i_plugin_rec_mutex_lock) (scm_t_rec_mutex *); +int (*scm_i_plugin_rec_mutex_trylock) (scm_t_rec_mutex *); +int (*scm_i_plugin_rec_mutex_unlock) (scm_t_rec_mutex *); + +int (*scm_i_plugin_cond_wait) (scm_t_cond *, scm_t_mutex *); +int (*scm_i_plugin_cond_timedwait) (scm_t_cond *, + scm_t_mutex *, + const struct timespec *); typedef struct rec_mutex { #ifdef SCM_DEBUG_THREADS @@ -62,141 +72,8 @@ typedef struct rec_mutex { /* Mutex for recursive mutex administration */ static scm_t_mutex rec_mutex_mutex; -#ifdef SCM_DEBUG_THREADS - -#define FAST_MUTEX 1 -#define REC_MUTEX 2 - -typedef struct mutex { - int kind; - pthread_mutex_t mutex; - scm_thread *owner; - int count; -} mutex; - -static pthread_mutex_t mutex_mutex; - -int -scm_i_plugin_mutex_init (scm_t_mutex *mx, const scm_t_mutexattr *a) -{ - mutex *m = (mutex *) mx; - pthread_mutex_init (&m->mutex, &scm_i_plugin_mutex); - m->owner = 0; - m->count = 0; - m->kind = FAST_MUTEX; - return 0; -} - -int -scm_i_plugin_mutex_lock (scm_t_mutex *mx) -{ - mutex *m = (mutex *) mx; - scm_thread *t = SCM_CURRENT_THREAD; - pthread_mutex_lock (&mutex_mutex); - if (m->kind != FAST_MUTEX) - { - fprintf (stderr, - m->kind == REC_MUTEX - ? "locking wrong mutex type\n" - : "locking uninitialized mutex\n"); - abort (); - } - if (m->owner == t) - { - fprintf (stderr, "locking mutex already locked by self\n"); - abort (); - } - pthread_mutex_unlock (&mutex_mutex); - pthread_mutex_lock (&m->mutex); - m->count = 1; - m->owner = t; - return 0; -} - -int -scm_i_plugin_mutex_unlock (scm_t_mutex *mx) -{ - mutex *m = (mutex *) mx; - pthread_mutex_lock (&mutex_mutex); - if (m->kind != FAST_MUTEX) - { - fprintf (stderr, - m->kind == REC_MUTEX - ? "locking wrong mutex type\n" - : "locking uninitialized mutex\n"); - abort (); - } - if (m->count != 1) - { - fprintf (stderr, - m->count == 0 - ? "unlocking unlocked mutex\n" - : "bogus internal state"); - abort (); - } - m->owner = 0; - m->count = 0; - pthread_mutex_unlock (&m->mutex); - pthread_mutex_unlock (&mutex_mutex); - return 0; -} - -int -scm_i_plugin_cond_wait (scm_t_cond *c, scm_t_mutex *mx) -{ - mutex *m = (mutex *) mx; - int res; - if (m->owner != SCM_CURRENT_THREAD || m->count != 1) - { - fprintf (stderr, "mutex not locked by self\n"); - abort (); - } - res = pthread_cond_wait ((pthread_cond_t *) c, &m->mutex); - if (m->owner != 0 || m->count != 0) - { - fprintf (stderr, "strange internal state\n"); - abort (); - } - m->owner = SCM_CURRENT_THREAD; - m->count = 1; - return res; -} - -int -scm_i_plugin_cond_timedwait (scm_t_cond *c, - scm_t_mutex *mx, - const struct timespec *t) -{ - mutex *m = (mutex *) mx; - return pthread_cond_timedwait ((pthread_cond_t *) c, &m->mutex, t); -} - -void -scm_i_assert_heap_locked () -{ - mutex *m = (mutex *) &SCM_CURRENT_THREAD->heap_mutex; - pthread_mutex_lock (&mutex_mutex); - if (gc_running) - { - fprintf (stderr, "thread running during gc\n"); - abort (); - } - if (m->count != 1) - { - fprintf (stderr, "mutex not locked\n"); - abort (); - } - pthread_mutex_unlock (&mutex_mutex); -} - -#endif - -/* The following section belongs in threads.c, or rather - thread-plugin.c. It is generic and not tied to any particular - thread library. */ - -int -scm_i_plugin_rec_mutex_init (scm_t_rec_mutex *mx, const scm_t_mutexattr *a) +static int +rec_mutex_init (scm_t_rec_mutex *mx, const scm_t_mutexattr *a) { rec_mutex *m = (rec_mutex *) mx; scm_i_plugin_mutex_init (&m->mutex, &scm_i_plugin_mutex); @@ -208,8 +85,15 @@ scm_i_plugin_rec_mutex_init (scm_t_rec_mutex *mx, const scm_t_mutexattr *a) return 0; } -int -scm_i_plugin_rec_mutex_lock (scm_t_rec_mutex *mx) +static int +rec_mutex_destroy (scm_t_rec_mutex *mx) +{ + rec_mutex *m = (rec_mutex *) mx; + return scm_i_plugin_mutex_destroy (&m->mutex); +} + +static int +rec_mutex_lock (scm_t_rec_mutex *mx) { rec_mutex *m = (rec_mutex *) mx; scm_thread *t = SCM_CURRENT_THREAD; @@ -239,8 +123,8 @@ scm_i_plugin_rec_mutex_lock (scm_t_rec_mutex *mx) return 0; } -int -scm_i_plugin_rec_mutex_trylock (scm_t_rec_mutex *mx) +static int +rec_mutex_trylock (scm_t_rec_mutex *mx) { rec_mutex *m = (rec_mutex *) mx; scm_thread *t = SCM_CURRENT_THREAD; @@ -262,8 +146,8 @@ scm_i_plugin_rec_mutex_trylock (scm_t_rec_mutex *mx) return 0; } -int -scm_i_plugin_rec_mutex_unlock (scm_t_rec_mutex *mx) +static int +rec_mutex_unlock (scm_t_rec_mutex *mx) { rec_mutex *m = (rec_mutex *) mx; scm_i_plugin_mutex_lock (&rec_mutex_mutex); @@ -291,47 +175,27 @@ scm_i_plugin_rec_mutex_unlock (scm_t_rec_mutex *mx) return 0; } -#endif /* !PTHREAD_MUTEX_RECURSIVE */ - #ifndef SCM_MUTEXATTR_SETTYPE_DECLARED int pthread_mutexattr_settype (pthread_mutexattr_t *, int); #endif void -scm_init_pthread_threads () +scm_init_threads_plugin () { - size_t mutex_size, rec_mutex_size; - pthread_mutexattr_init (&scm_i_plugin_mutex); -#ifdef SCM_MUTEX_FAST - pthread_mutexattr_settype (&scm_i_plugin_mutex, SCM_MUTEX_FAST); -#endif - -#if defined (SCM_MUTEX_RECURSIVE) && !defined (SCM_DEBUG_THREADS) - rec_mutex_size = sizeof (pthread_mutex_t); - pthread_mutexattr_init (&scm_i_plugin_rec_mutex); - pthread_mutexattr_settype (&scm_i_plugin_rec_mutex, SCM_MUTEX_RECURSIVE); -#else - /* If PTHREAD_MUTEX_RECURSIVE is not defined, - scm_i_plugin_rec_mutex_init won't pay attention to it anyway - */ - rec_mutex_size = sizeof (rec_mutex); - scm_i_plugin_mutex_init (&rec_mutex_mutex, &scm_i_plugin_mutex); -#endif -#ifdef SCM_DEBUG_THREADS - mutex_size = sizeof (mutex); - pthread_mutex_init (&mutex_mutex, &scm_i_plugin_mutex); -#else - mutex_size = sizeof (pthread_mutex_t); -#endif - if (mutex_size > SCM_MUTEX_MAXSIZE || rec_mutex_size > SCM_REC_MUTEX_MAXSIZE) + if (scm_i_plugin_rec_mutex_size == 0) + { + scm_i_plugin_rec_mutex_size = sizeof (rec_mutex); + scm_i_plugin_mutex_init (&rec_mutex_mutex, &scm_i_plugin_mutex); + scm_i_plugin_rec_mutex_init = rec_mutex_init; + scm_i_plugin_rec_mutex_destroy = rec_mutex_destroy; + scm_i_plugin_rec_mutex_lock = rec_mutex_lock; + scm_i_plugin_rec_mutex_trylock = rec_mutex_trylock; + scm_i_plugin_rec_mutex_unlock = rec_mutex_unlock; + } + if (scm_i_plugin_mutex_size > SCM_MUTEX_MAXSIZE + || scm_i_plugin_rec_mutex_size > SCM_REC_MUTEX_MAXSIZE) { fprintf (stderr, "Internal error: Need to upgrade mutex size\n"); abort (); } } - -/* - Local Variables: - c-file-style: "gnu" - End: -*/ diff --git a/libguile/threads-plugin.h b/libguile/threads-plugin.h new file mode 100644 index 000000000..3e0eb2096 --- /dev/null +++ b/libguile/threads-plugin.h @@ -0,0 +1,100 @@ +/* classes: h_files */ + +#ifndef SCM_THREADS_PLUGIN_H +#define SCM_THREADS_PLUGIN_H + +/* Copyright (C) 1996,1997,1998,2000,2001, 2002 Free Software Foundation, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + * + * As a special exception, the Free Software Foundation gives permission + * for additional uses of the text contained in its release of GUILE. + * + * The exception is that, if you link the GUILE library with other files + * to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the GUILE library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the + * Free Software Foundation under the name GUILE. If you copy + * code from other Free Software Foundation releases into a copy of + * GUILE, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for GUILE, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. */ + + +#include /* This file should *not* need to include pthread.h */ + +/* Size is checked in scm_init_threads_plugin */ +#define SCM_MUTEX_MAXSIZE (9 * sizeof (long)) +typedef struct { char _[SCM_MUTEX_MAXSIZE]; } scm_t_mutex; + +/*fixme* Should be defined similarly to scm_t_mutex. */ +#define scm_t_mutexattr pthread_mutexattr_t + +extern int scm_i_plugin_mutex_size; +typedef int (*scm_t_mutex_init) (scm_t_mutex *, const scm_t_mutexattr *); +typedef int (*scm_t_mutex_lock) (scm_t_mutex *); +typedef int (*scm_t_mutex_unlock) (scm_t_mutex *); +extern scm_t_mutex_init scm_i_plugin_mutex_init; +extern scm_t_mutex_lock scm_i_plugin_mutex_lock; +extern scm_t_mutex_unlock scm_i_plugin_mutex_unlock; + +/* Size is checked in scm_init_threads_plugin */ +#define SCM_REC_MUTEX_MAXSIZE (SCM_MUTEX_MAXSIZE + 3 * sizeof (long)) +typedef struct { char _[SCM_REC_MUTEX_MAXSIZE]; } scm_t_rec_mutex; + +extern int scm_i_plugin_rec_mutex_size; +typedef int (*scm_t_rec_mutex_init) (scm_t_rec_mutex *, + const scm_t_mutexattr *); +typedef int (*scm_t_rec_mutex_destroy) (scm_t_rec_mutex *); +typedef int (*scm_t_rec_mutex_lock) (scm_t_rec_mutex *); +typedef int (*scm_t_rec_mutex_trylock) (scm_t_rec_mutex *); +typedef int (*scm_t_rec_mutex_unlock) (scm_t_rec_mutex *); +extern scm_t_rec_mutex_init scm_i_plugin_rec_mutex_init; +extern scm_t_rec_mutex_destroy scm_i_plugin_rec_mutex_destroy; +extern scm_t_rec_mutex_lock scm_i_plugin_rec_mutex_lock; +extern scm_t_rec_mutex_trylock scm_i_plugin_rec_mutex_trylock; +extern scm_t_rec_mutex_unlock scm_i_plugin_rec_mutex_unlock; + +/*fixme*/ +#define scm_t_cond pthread_cond_t + +typedef int (*scm_t_cond_wait) (scm_t_cond *, scm_t_mutex *); +typedef int (*scm_t_cond_timedwait) (scm_t_cond *, + scm_t_mutex *, + const struct timespec *); +extern scm_t_cond_wait scm_i_plugin_cond_wait; +extern scm_t_cond_timedwait scm_i_plugin_cond_timedwait; + +extern void scm_init_threads_plugin (void); + +#endif /* SCM_THREADS_PLUGIN_H */ + +/* + Local Variables: + c-file-style: "gnu" + End: +*/ diff --git a/libguile/threads.c b/libguile/threads.c index 0d277c125..a717b13a0 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -460,6 +460,15 @@ SCM_DEFINE (scm_call_with_new_thread, "call-with-new-thread", 2, 0, 0, } #undef FUNC_NAME +SCM_DEFINE (scm_yield, "yield", 0, 0, 0, + (), +"Move the calling thread to the end of the scheduling queue.") +#define FUNC_NAME s_scm_yield +{ + return SCM_BOOL (scm_thread_yield); +} +#undef FUNC_NAME + SCM_DEFINE (scm_join_thread, "join-thread", 1, 0, 0, (SCM thread), "Suspend execution of the calling thread until the target @var{thread} " @@ -1312,6 +1321,7 @@ scm_t_rec_mutex scm_i_defer_mutex; #ifdef USE_PTHREAD_THREADS #include "libguile/pthread-threads.c" #endif +#include "libguile/threads-plugin.c" /*** Initialization */ diff --git a/libguile/threads.h b/libguile/threads.h index 63b70428d..14c0bdefe 100644 --- a/libguile/threads.h +++ b/libguile/threads.h @@ -51,6 +51,7 @@ #include "libguile/throw.h" #include "libguile/root.h" #include "libguile/iselect.h" +#include "libguile/threads-plugin.h" /* smob tags for the thread datatypes */ @@ -126,6 +127,7 @@ SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data, #define scm_thread_join scm_i_plugin_thread_join #define scm_thread_detach scm_i_plugin_thread_detach #define scm_thread_self scm_i_plugin_thread_self +#define scm_thread_yield scm_i_plugin_thread_yield #define scm_mutex_init scm_i_plugin_mutex_init #define scm_mutex_destroy scm_i_plugin_mutex_destroy @@ -235,6 +237,7 @@ do { \ /* The C versions of the Scheme-visible thread functions. */ SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler); +SCM_API SCM scm_yield (void); SCM_API SCM scm_join_thread (SCM t); SCM_API SCM scm_i_make_future (SCM thunk); SCM_API SCM scm_future_ref (SCM future); -- 2.20.1