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-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
#define REC_MUTEX 2
typedef struct mutex {
-#ifdef SCM_DEBUG_THREADS
int kind;
-#endif
pthread_mutex_t mutex;
scm_thread *owner;
int count;
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
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
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 ();
+ }
}
/*
#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
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;
#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 */
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 *
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)
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))