summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikael Djurfeldt <djurfeldt@nada.kth.se>2002-12-18 10:53:23 +0000
committerMikael Djurfeldt <djurfeldt@nada.kth.se>2002-12-18 10:53:23 +0000
commite29e0b0963a6b659f70d78271d2c40e9e2de28e7 (patch)
tree83b0413167341f34109e1b9f2df2674003027a53
parentdad981029a145d7a8854124a558d66a68675ce1f (diff)
downloadguile-e29e0b0963a6b659f70d78271d2c40e9e2de28e7.tar.gz
* threads.c (really_launch): Detach before unlocking
thread_admin_mutex in order not to risk being joined. (scm_i_thread_put_to_sleep, scm_i_thread_wake_up): Keep thread_admin_mutex locked during GC. * pthread-threads.c, pthread-threads.h: Improvements to debugging functions.
-rw-r--r--libguile/ChangeLog10
-rw-r--r--libguile/pthread-threads.c59
-rw-r--r--libguile/pthread-threads.h16
-rw-r--r--libguile/threads.c9
4 files changed, 62 insertions, 32 deletions
diff --git a/libguile/ChangeLog b/libguile/ChangeLog
index 631ef1c4b..79cc105eb 100644
--- a/libguile/ChangeLog
+++ b/libguile/ChangeLog
@@ -1,3 +1,13 @@
+2002-12-18 Mikael Djurfeldt <mdj@kvast.blakulla.net>
+
+ * threads.c (really_launch): Detach before unlocking
+ thread_admin_mutex in order not to risk being joined.
+ (scm_i_thread_put_to_sleep, scm_i_thread_wake_up): Keep
+ thread_admin_mutex locked during GC.
+
+ * pthread-threads.c, pthread-threads.h: Improvements to debugging
+ functions.
+
2002-12-16 Mikael Djurfeldt <mdj@kvast.blakulla.net>
* pthread-threads.c, pthread-threads.h (SCM_DEBUG_THREADS): Added
diff --git a/libguile/pthread-threads.c b/libguile/pthread-threads.c
index 1e456e788..0fce9ecbf 100644
--- a/libguile/pthread-threads.c
+++ b/libguile/pthread-threads.c
@@ -68,9 +68,7 @@ static scm_t_mutex rec_mutex_mutex;
#define REC_MUTEX 2
typedef struct mutex {
-#ifdef SCM_DEBUG_THREADS
int kind;
-#endif
pthread_mutex_t mutex;
scm_thread *owner;
int count;
@@ -147,7 +145,21 @@ int
scm_i_plugin_cond_wait (scm_t_cond *c, scm_t_mutex *mx)
{
mutex *m = (mutex *) mx;
- return pthread_cond_wait ((pthread_cond_t *) c, &m->mutex);
+ 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
@@ -159,6 +171,24 @@ scm_i_plugin_cond_timedwait (scm_t_cond *c,
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
@@ -270,33 +300,34 @@ int pthread_mutexattr_settype (pthread_mutexattr_t *, int);
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)
- if (sizeof (pthread_mutex_t) > SCM_REC_MUTEX_MAXSIZE)
- {
- fprintf (stderr, "Internal error: Need to upgrade mutex size\n");
- abort ();
- }
- pthread_mutexattr_init (&scm_i_plugin_rec_mutex);
+ 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
*/
- if (sizeof (rec_mutex) > SCM_REC_MUTEX_MAXSIZE)
- {
- fprintf (stderr, "Internal error: Need to upgrade mutex size\n");
- abort ();
- }
+ 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 ();
+ }
}
/*
diff --git a/libguile/pthread-threads.h b/libguile/pthread-threads.h
index 2ab3d16a0..2af178ae6 100644
--- a/libguile/pthread-threads.h
+++ b/libguile/pthread-threads.h
@@ -70,11 +70,7 @@
#define scm_i_plugin_thread_yield sched_yield
/* Size is checked in scm_init_pthread_threads */
-#ifdef SCM_DEBUG_THREADS
#define SCM_MUTEX_MAXSIZE (9 * sizeof (long))
-#else
-#define SCM_MUTEX_MAXSIZE (6 * sizeof (long))
-#endif
typedef struct { char _[SCM_MUTEX_MAXSIZE]; } scm_t_mutex;
#define scm_t_mutexattr pthread_mutexattr_t
@@ -100,15 +96,7 @@ int scm_i_plugin_mutex_unlock (scm_t_mutex *);
pthread_mutex_trylock ((pthread_mutex_t *) (m))
/* Size is checked in scm_init_pthread_threads */
-#ifdef SCM_DEBUG_THREADS
#define SCM_REC_MUTEX_MAXSIZE (SCM_MUTEX_MAXSIZE + 3 * sizeof (long))
-#else
-#ifdef SCM_MUTEX_RECURSIVE
-#define SCM_REC_MUTEX_MAXSIZE SCM_MUTEX_MAXSIZE
-#else
-#define SCM_REC_MUTEX_MAXSIZE (SCM_MUTEX_MAXSIZE + 2 * sizeof (long))
-#endif
-#endif
typedef struct { char _[SCM_REC_MUTEX_MAXSIZE]; } scm_t_rec_mutex;
extern scm_t_mutexattr scm_i_plugin_rec_mutex;
@@ -160,6 +148,10 @@ int scm_i_plugin_cond_timedwait (scm_t_cond *,
#define scm_i_plugin_select select
+#ifdef SCM_DEBUG_THREADS
+void scm_i_assert_heap_locked (void);
+#endif
+
void scm_init_pthread_threads (void);
#endif /* SCM_THREADS_PTHREADS_H */
diff --git a/libguile/threads.c b/libguile/threads.c
index dee62c501..0d277c125 100644
--- a/libguile/threads.c
+++ b/libguile/threads.c
@@ -346,9 +346,9 @@ really_launch (SCM_STACKITEM *base, launch_data *data)
all_threads = scm_delq_x (thread, all_threads);
t->exited = 1;
thread_count--;
- scm_i_plugin_mutex_unlock (&thread_admin_mutex);
-
+ /* detach before unlocking in order to not become joined when detached */
scm_thread_detach (t->thread);
+ scm_i_plugin_mutex_unlock (&thread_admin_mutex);
}
static void *
@@ -1262,14 +1262,13 @@ scm_i_thread_put_to_sleep ()
scm_i_plugin_mutex_lock (&t->heap_mutex);
}
scm_i_thread_go_to_sleep = 0;
- scm_i_plugin_mutex_unlock (&thread_admin_mutex);
}
}
void
scm_i_thread_invalidate_freelists ()
{
- /* Don't need to lock thread_admin_mutex here since we are sinle threaded */
+ /* Don't need to lock thread_admin_mutex here since we are single threaded */
SCM threads = all_threads;
for (; !SCM_NULLP (threads); threads = SCM_CDR (threads))
if (SCM_CAR (threads) != cur_thread)
@@ -1285,8 +1284,6 @@ scm_i_thread_wake_up ()
if (threads_initialized_p && !--gc_section_count)
{
SCM threads;
- /* Need to lock since woken threads can die and be deleted from list */
- scm_i_plugin_mutex_lock (&thread_admin_mutex);
threads = all_threads;
scm_i_plugin_cond_broadcast (&wake_up_cond);
for (; !SCM_NULLP (threads); threads = SCM_CDR (threads))