Index: generic/src/proc/task.c
===================================================================
--- generic/src/proc/task.c	(revision 88636f6876ccf21580fba5255649d74b61377073)
+++ generic/src/proc/task.c	(revision 8abbcc9858b56f6fbc9af504791bd35090d5573d)
@@ -301,5 +301,5 @@
 
 	/*
-	 * Interrupt all threads except this one.
+	 * Interrupt all threads except ktaskclnp.
 	 */	
 	for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
@@ -468,5 +468,5 @@
 			thr = list_get_instance(cur, thread_t, th_link);
 			spinlock_lock(&thr->lock);
-			if (thr->state == Undead && thr->join_type == None) {
+			if (thr != t && thr->state == Undead && thr->join_type == None) {
 				thr->join_type = TaskGC;
 				spinlock_unlock(&thr->lock);
Index: generic/src/proc/thread.c
===================================================================
--- generic/src/proc/thread.c	(revision 88636f6876ccf21580fba5255649d74b61377073)
+++ generic/src/proc/thread.c	(revision 8abbcc9858b56f6fbc9af504791bd35090d5573d)
@@ -351,8 +351,10 @@
 	 * Attach to the containing task.
 	 */
+	ipl = interrupts_disable();	 
 	spinlock_lock(&task->lock);
 	if (!task->accept_new_threads) {
 		spinlock_unlock(&task->lock);
 		slab_free(thread_slab, t);
+		interrupts_restore(ipl);
 		return NULL;
 	}
@@ -365,5 +367,4 @@
 	 * Register this thread in the system-wide list.
 	 */
-	ipl = interrupts_disable();
 	spinlock_lock(&threads_lock);
 	btree_insert(&threads_btree, (btree_key_t) ((__address) t), (void *) t, NULL);
@@ -375,5 +376,5 @@
 }
 
-/** Make thread exiting
+/** Terminate thread.
  *
  * End current thread execution and switch it to the exiting
@@ -438,14 +439,9 @@
 	ipl = interrupts_disable();
 	spinlock_lock(&t->lock);
-
 	ASSERT(!t->detached);
-	
-	(void) waitq_sleep_prepare(&t->join_wq);
 	spinlock_unlock(&t->lock);
-	
-	rc = waitq_sleep_timeout_unsafe(&t->join_wq, usec, flags);
-	
-	waitq_sleep_finish(&t->join_wq, rc, ipl);
-	interrupts_restore(ipl);
+	interrupts_restore(ipl);
+	
+	rc = waitq_sleep_timeout(&t->join_wq, usec, flags);
 	
 	return rc;	
@@ -467,5 +463,4 @@
 	 * pointer to it must be still valid.
 	 */
-	
 	ipl = interrupts_disable();
 	spinlock_lock(&t->lock);
