+#else /* !BUILDING_VARIANT ] [ */
+extern int __unix_conforming;
+extern pthread_lock_t _pthread_list_lock;
+extern void _pthread_testcancel(pthread_t thread, int isconforming);
+extern int _pthread_reap_thread(pthread_t th, mach_port_t kernel_thread, void **value_ptr);
+
+#endif /* !BUILDING_VARIANT ] */
+
+#if __DARWIN_UNIX03
+
+static void __posix_join_cleanup(void *arg)
+{
+ pthread_t thread = (pthread_t)arg;
+ int already_exited, res;
+ void * dummy;
+ semaphore_t death;
+
+ LOCK(thread->lock);
+ death = thread->death;
+ already_exited = (thread->detached & _PTHREAD_EXITED);
+
+ if (!already_exited){
+ thread->joiner = (struct _pthread *)NULL;
+ UNLOCK(thread->lock);
+ restore_sem_to_pool(death);
+ } else {
+ UNLOCK(thread->lock);
+ while ((res = _pthread_reap_thread(thread,
+ thread->kernel_thread,
+ &dummy)) == EAGAIN)
+ {
+ sched_yield();
+ }
+ restore_sem_to_pool(death);
+
+ }
+}
+
+#endif /* __DARWIN_UNIX03 */
+
+
+/*
+ * Wait for a thread to terminate and obtain its exit value.
+ */
+int
+pthread_join(pthread_t thread,
+ void **value_ptr)
+{
+ kern_return_t kern_res;
+ int res = ESUCCESS;
+
+#if __DARWIN_UNIX03
+ if (__unix_conforming == 0)
+ __unix_conforming = 1;
+#endif /* __DARWIN_UNIX03 */
+
+ if (thread->sig == _PTHREAD_SIG)
+ {
+ semaphore_t death = new_sem_from_pool(); /* in case we need it */
+
+ LOCK(thread->lock);
+ if ((thread->detached & PTHREAD_CREATE_JOINABLE) &&
+ thread->death == SEMAPHORE_NULL)
+ {
+ pthread_t self = pthread_self();
+
+ assert(thread->joiner == NULL);
+ if (thread != self && (self == NULL || self->joiner != thread))
+ {
+ int already_exited = (thread->detached & _PTHREAD_EXITED);
+
+ thread->death = death;
+ thread->joiner = self;
+ UNLOCK(thread->lock);
+
+ if (!already_exited)
+ {
+#if __DARWIN_UNIX03
+ /* Wait for it to signal... */
+ pthread_cleanup_push(__posix_join_cleanup, (void *)thread);
+ do {
+ res = __semwait_signal(death, 0, 0, 0, 0, 0);
+ } while ((res < 0) && (errno == EINTR));
+ pthread_cleanup_pop(0);
+
+#else /* __DARWIN_UNIX03 */
+ /* Wait for it to signal... */
+ do {
+ PTHREAD_MACH_CALL(semaphore_wait(death), kern_res);
+ } while (kern_res != KERN_SUCCESS);
+#endif /* __DARWIN_UNIX03 */
+ }
+#if __DARWIN_UNIX03
+ else {
+ if ((thread->cancel_state & (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING)) == (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING))
+ res = PTHREAD_CANCELED;
+ }
+#endif /* __DARWIN_UNIX03 */
+
+ LOCK(_pthread_list_lock);
+ LIST_REMOVE(thread, plist);
+ UNLOCK(_pthread_list_lock);
+ /* ... and wait for it to really be dead */
+ while ((res = _pthread_reap_thread(thread,
+ thread->kernel_thread,
+ value_ptr)) == EAGAIN)
+ {
+ sched_yield();
+ }
+ } else {
+ UNLOCK(thread->lock);
+ res = EDEADLK;
+ }
+ } else {
+ UNLOCK(thread->lock);
+ res = EINVAL;
+ }
+ restore_sem_to_pool(death);
+ return res;
+ }
+ return ESRCH;
+}
+
+/*
+ * Cancel a thread
+ */
+int
+pthread_cancel(pthread_t thread)
+{
+#if __DARWIN_UNIX03
+ if (__unix_conforming == 0)
+ __unix_conforming = 1;
+#endif /* __DARWIN_UNIX03 */
+
+ if (thread->sig == _PTHREAD_SIG)
+ {
+#if __DARWIN_UNIX03
+ int state;
+ LOCK(thread->lock);
+ state = thread->cancel_state |= _PTHREAD_CANCEL_PENDING;
+ UNLOCK(thread->lock);
+ if (state & PTHREAD_CANCEL_ENABLE)
+ __pthread_markcancel(thread->kernel_thread);
+#else /* __DARWIN_UNIX03 */
+ thread->cancel_state |= _PTHREAD_CANCEL_PENDING;
+#endif /* __DARWIN_UNIX03 */
+ return (ESUCCESS);
+ } else
+ {
+ return (ESRCH);
+ }
+}
+
+void
+pthread_testcancel(void)
+{
+ pthread_t self = pthread_self();
+
+#if __DARWIN_UNIX03
+ if (__unix_conforming == 0)
+ __unix_conforming = 1;
+ _pthread_testcancel(self, 1);
+#else /* __DARWIN_UNIX03 */
+ _pthread_testcancel(self, 0);
+#endif /* __DARWIN_UNIX03 */
+
+}
+/*
+ * Query/update the cancelability 'state' of a thread
+ */
+int
+pthread_setcancelstate(int state, int *oldstate)
+{
+ pthread_t self = pthread_self();
+
+#if __DARWIN_UNIX03
+ if (__unix_conforming == 0)
+ __unix_conforming = 1;
+#endif /* __DARWIN_UNIX03 */
+
+ switch (state) {
+ case PTHREAD_CANCEL_ENABLE:
+#if __DARWIN_UNIX03
+ __pthread_canceled(1);
+#endif /* __DARWIN_UNIX03 */
+ break;
+ case PTHREAD_CANCEL_DISABLE:
+#if __DARWIN_UNIX03
+ __pthread_canceled(2);
+#endif /* __DARWIN_UNIX03 */
+ break;
+ default:
+ return EINVAL;
+ }
+
+ self = pthread_self();
+ LOCK(self->lock);
+ if (oldstate)
+ *oldstate = self->cancel_state & _PTHREAD_CANCEL_STATE_MASK;
+ self->cancel_state &= ~_PTHREAD_CANCEL_STATE_MASK;
+ self->cancel_state |= state;
+ UNLOCK(self->lock);
+#if !__DARWIN_UNIX03
+ _pthread_testcancel(self, 0); /* See if we need to 'die' now... */
+#endif /* __DARWIN_UNIX03 */
+ return (0);
+}
+
+/*
+ * Query/update the cancelability 'type' of a thread
+ */
+int
+pthread_setcanceltype(int type, int *oldtype)
+{
+ pthread_t self = pthread_self();
+
+#if __DARWIN_UNIX03
+ if (__unix_conforming == 0)
+ __unix_conforming = 1;
+#endif /* __DARWIN_UNIX03 */
+
+ if ((type != PTHREAD_CANCEL_DEFERRED) &&
+ (type != PTHREAD_CANCEL_ASYNCHRONOUS))
+ return EINVAL;
+ self = pthread_self();
+ LOCK(self->lock);
+ if (oldtype)
+ *oldtype = self->cancel_state & _PTHREAD_CANCEL_TYPE_MASK;
+ self->cancel_state &= ~_PTHREAD_CANCEL_TYPE_MASK;
+ self->cancel_state |= type;
+ UNLOCK(self->lock);
+#if !__DARWIN_UNIX03
+ _pthread_testcancel(self, 0); /* See if we need to 'die' now... */
+#endif /* __DARWIN_UNIX03 */
+ return (0);
+}
+