summaryrefslogtreecommitdiffstats
path: root/lib/libthr/thread/thr_stack.c
diff options
context:
space:
mode:
authormtm <mtm@FreeBSD.org>2003-05-25 08:31:33 +0000
committermtm <mtm@FreeBSD.org>2003-05-25 08:31:33 +0000
commit9b84ee274a05a58268c2f9fcd1bac437c00cbbe3 (patch)
tree8d4a439b258befa57524829fe1e00a861d962282 /lib/libthr/thread/thr_stack.c
parent33c8b02fd8ae4e20728d11cbf06a9e18d546af6b (diff)
downloadFreeBSD-src-9b84ee274a05a58268c2f9fcd1bac437c00cbbe3.zip
FreeBSD-src-9b84ee274a05a58268c2f9fcd1bac437c00cbbe3.tar.gz
Start locking up the active and dead threads lists. The active threads
list is protected by a spinlock_t, but the dead list uses a pthread_mutex because it is necessary to synchronize other threads with the garbage collector thread. Lock/Unlock macros are used so it's easier to make changes to the locks in the future. The 'dead thread list' lock is intended to replace the gc mutex. This doesn't have any practical ramifications. It simply makes it clearer what the purpose of the lock is. The gc will use this lock, instead of the gc mutex, to synchronize access to the dead list with other threads. Modify _pthread_exit() to use these two new locks instead of GIANT_LOCK, and also to properly lock and protect thread state changes, especially with respect to a joining thread. The gc thread was also re-arranged to be more organized and less nested. _pthread_join() was also modified to use the thread list locks. However, locking and unlocking here needs special care because a thread could find itself in a position where it's joining an exiting thread that is waiting on the dead list lock, which this thread (joiner) holds. If the joiner doesn't take care to lock *and* unlock in the same order they (the joiner and the joinee) could deadlock against each other. Approved by: re/blanket libthr
Diffstat (limited to 'lib/libthr/thread/thr_stack.c')
-rw-r--r--lib/libthr/thread/thr_stack.c14
1 files changed, 5 insertions, 9 deletions
diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c
index c75d6ee..28b396b 100644
--- a/lib/libthr/thread/thr_stack.c
+++ b/lib/libthr/thread/thr_stack.c
@@ -144,8 +144,7 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize)
* Use the garbage collector mutex for synchronization of the
* spare stack list.
*/
- if (pthread_mutex_lock(&_gc_mutex) != 0)
- PANIC("Cannot lock gc mutex");
+ DEAD_LIST_LOCK;
if ((spare_stack = LIST_FIRST(&_dstackq)) != NULL) {
/* Use the spare stack. */
@@ -154,8 +153,7 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize)
}
/* Unlock the garbage collector mutex. */
- if (pthread_mutex_unlock(&_gc_mutex) != 0)
- PANIC("Cannot unlock gc mutex");
+ DEAD_LIST_UNLOCK;
}
/*
* The user specified a non-default stack and/or guard size, so try to
@@ -167,8 +165,7 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize)
* Use the garbage collector mutex for synchronization of the
* spare stack list.
*/
- if (pthread_mutex_lock(&_gc_mutex) != 0)
- PANIC("Cannot lock gc mutex");
+ DEAD_LIST_LOCK;
LIST_FOREACH(spare_stack, &_mstackq, qe) {
if (spare_stack->stacksize == stack_size &&
@@ -180,8 +177,7 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize)
}
/* Unlock the garbage collector mutex. */
- if (pthread_mutex_unlock(&_gc_mutex) != 0)
- PANIC("Cannot unlock gc mutex");
+ DEAD_LIST_UNLOCK;
}
/* Check if a stack was not allocated from a stack cache: */
@@ -212,7 +208,7 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize)
return (stack);
}
-/* This function must be called with _gc_mutex held. */
+/* This function must be called with the 'dead thread list' lock held. */
void
_thread_stack_free(void *stack, size_t stacksize, size_t guardsize)
{
OpenPOWER on IntegriCloud