+/*
+ * Query/update the cancelability 'state' of a thread
+ */
+int
+_pthread_setcancelstate_internal(int state, int *oldstate, int conforming)
+{
+ pthread_t self = pthread_self();
+
+
+ switch (state) {
+ case PTHREAD_CANCEL_ENABLE:
+ if (conforming)
+ __pthread_canceled(1);
+ break;
+ case PTHREAD_CANCEL_DISABLE:
+ if (conforming)
+ __pthread_canceled(2);
+ 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 (!conforming)
+ _pthread_testcancel(self, 0); /* See if we need to 'die' now... */
+ return (0);
+}
+
+/* When a thread exits set the cancellation state to DISABLE and DEFERRED */
+static void
+_pthread_setcancelstate_exit(pthread_t self, void * value_ptr, int conforming)
+{
+ LOCK(self->lock);
+ self->cancel_state &= ~(_PTHREAD_CANCEL_STATE_MASK | _PTHREAD_CANCEL_TYPE_MASK);
+ self->cancel_state |= (PTHREAD_CANCEL_DISABLE | PTHREAD_CANCEL_DEFERRED);
+ if ((value_ptr == PTHREAD_CANCELED)) {
+// 4597450: begin
+ self->detached |= _PTHREAD_WASCANCEL;
+// 4597450: end
+ }
+ UNLOCK(self->lock);
+}
+
+int
+_pthread_join_cleanup(pthread_t thread, void ** value_ptr, int conforming)
+{
+ kern_return_t res;
+ int detached = 0, ret;
+
+#if WQ_TRACE
+ __kdebug_trace(0x9000028, thread, 0, 0, 1, 0);
+#endif
+ /* The scenario where the joiner was waiting for the thread and
+ * the pthread detach happened on that thread. Then the semaphore
+ * will trigger but by the time joiner runs, the target thread could be
+ * freed. So we need to make sure that the thread is still in the list
+ * and is joinable before we continue with the join.
+ */
+ LOCK(_pthread_list_lock);
+ if ((ret = _pthread_find_thread(thread)) != 0) {
+ UNLOCK(_pthread_list_lock);
+ /* returns ESRCH */
+ return(ret);
+ }
+ if ((thread->detached & PTHREAD_CREATE_JOINABLE) == 0) {
+ /* the thread might be a detached thread */
+ UNLOCK(_pthread_list_lock);
+ return(ESRCH);
+
+ }
+ /* It is still a joinable thread and needs to be reaped */
+ TAILQ_REMOVE(&__pthread_head, thread, plist);
+#if WQ_TRACE
+ __kdebug_trace(0x9000010, thread, 0, 0, 3, 0);
+#endif
+ UNLOCK(_pthread_list_lock);
+
+ if (value_ptr)
+ *value_ptr = thread->exit_value;
+ if (conforming) {
+ if ((thread->cancel_state & (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING)) ==
+ (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING)) {
+ *value_ptr = PTHREAD_CANCELED;
+ }
+ }
+ if (thread->reply_port != MACH_PORT_NULL) {
+ res = mach_port_mod_refs(mach_task_self(), thread->reply_port, MACH_PORT_RIGHT_RECEIVE, -1);
+ if (res != KERN_SUCCESS)
+ fprintf(stderr,"mach_port_mod_refs(reply_port) failed: %s\n",mach_error_string(res));
+ thread->reply_port = MACH_PORT_NULL;
+ }
+ if (thread->freeStackOnExit) {
+ thread->sig = _PTHREAD_NO_SIG;
+#if WQ_TRACE
+ __kdebug_trace(0x9000028, thread, 0, 0, 2, 0);
+#endif
+ vm_deallocate(mach_task_self(), thread, pthreadsize);
+ } else {
+ thread->sig = _PTHREAD_NO_SIG;
+#if WQ_TRACE
+ __kdebug_trace(0x9000028, thread, 0, 0, 3, 0);
+#endif
+ free(thread);
+ }
+ return(0);
+}
+
+/* ALWAYS called with list lock and return with list lock */
+int
+_pthread_find_thread(pthread_t thread)
+{
+ pthread_t p;
+
+loop:
+ TAILQ_FOREACH(p, &__pthread_head, plist) {
+ if (p == thread) {
+ if (thread->kernel_thread == MACH_PORT_NULL) {
+ UNLOCK(_pthread_list_lock);
+ sched_yield();
+ LOCK(_pthread_list_lock);
+ goto loop;
+ }
+ return(0);
+ }
+ }
+ return(ESRCH);
+}
+
+int
+_pthread_lookup_thread(pthread_t thread, mach_port_t * portp, int only_joinable)
+{
+ mach_port_t kport;
+ int ret = 0;
+
+ if (thread == NULL)
+ return(ESRCH);
+
+ LOCK(_pthread_list_lock);
+
+ if ((ret = _pthread_find_thread(thread)) != 0) {
+ UNLOCK(_pthread_list_lock);
+ return(ret);
+ }
+ if ((only_joinable != 0) && ((thread->detached & PTHREAD_CREATE_DETACHED) != 0)) {
+ UNLOCK(_pthread_list_lock);
+ return(EINVAL);
+ }
+ kport = thread->kernel_thread;
+ UNLOCK(_pthread_list_lock);
+ if (portp != NULL)
+ *portp = kport;
+ return(0);
+}
+
+/* XXXXXXXXXXXXX Pthread Workqueue Attributes XXXXXXXXXXXXXXXXXX */
+int
+pthread_workqueue_attr_init_np(pthread_workqueue_attr_t * attrp)
+{
+ attrp->stacksize = DEFAULT_STACK_SIZE;
+ attrp->istimeshare = 1;
+ attrp->importance = 0;
+ attrp->affinity = 0;
+ attrp->queueprio = WORK_QUEUE_NORMALIZER;
+ attrp->sig = PTHEAD_WRKQUEUE_ATTR_SIG;
+ return(0);
+}
+
+int
+pthread_workqueue_attr_destroy_np(pthread_workqueue_attr_t * attr)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG)
+ {
+ return (0);
+ } else
+ {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+#ifdef NOTYET /* [ */
+int
+pthread_workqueue_attr_getstacksize_np(const pthread_workqueue_attr_t * attr, size_t * stacksizep)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) {
+ *stacksizep = attr->stacksize;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+int
+pthread_workqueue_attr_setstacksize_np(pthread_workqueue_attr_t * attr, size_t stacksize)
+{
+ if ((attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) && ((stacksize % vm_page_size) == 0) && (stacksize >= PTHREAD_STACK_MIN)) {
+ attr->stacksize = stacksize;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+
+int
+pthread_workqueue_attr_getthreadtimeshare_np(const pthread_workqueue_attr_t * attr, int * istimesahrep)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) {
+ *istimesahrep = attr->istimeshare;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+int
+pthread_workqueue_attr_settthreadtimeshare_np(pthread_workqueue_attr_t * attr, int istimeshare)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) {
+ if (istimeshare != 0)
+ attr->istimeshare = istimeshare;
+ else
+ attr->istimeshare = 0;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+int
+pthread_workqueue_attr_getthreadimportance_np(const pthread_workqueue_attr_t * attr, int * importancep)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) {
+ *importancep = attr->importance;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+int
+pthread_workqueue_attr_settthreadimportance_np(pthread_workqueue_attr_t * attr, int importance)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG){
+ attr->importance = importance;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+int
+pthread_workqueue_attr_getthreadaffinity_np(const pthread_workqueue_attr_t * attr, int * affinityp)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) {
+ *affinityp = attr->affinity;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+int
+pthread_workqueue_attr_settthreadaffinity_np(pthread_workqueue_attr_t * attr, int affinity)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG){
+ attr->affinity = affinity;
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+#endif /* NOTYET ] */
+
+int
+pthread_workqueue_attr_getqueuepriority_np(const pthread_workqueue_attr_t * attr, int * qpriop)
+{
+ if (attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) {
+ *qpriop = (attr->queueprio - WORK_QUEUE_NORMALIZER);
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+int
+pthread_workqueue_attr_setqueuepriority_np(pthread_workqueue_attr_t * attr, int qprio)
+{
+ /* only -2 to +2 is valid */
+ if ((attr->sig == PTHEAD_WRKQUEUE_ATTR_SIG) && (qprio <= 2) && (qprio >= -2)) {
+ attr->queueprio = (qprio + WORK_QUEUE_NORMALIZER);
+ return (0);
+ } else {
+ return (EINVAL); /* Not an attribute structure! */
+ }
+}
+
+/* XXXXXXXXXXXXX Pthread Workqueue support routines XXXXXXXXXXXXXXXXXX */
+
+static void
+workqueue_list_lock()
+{
+ OSSpinLockLock(&__workqueue_list_lock);
+}
+
+static void
+workqueue_list_unlock()
+{
+ OSSpinLockUnlock(&__workqueue_list_lock);
+}
+
+int
+pthread_workqueue_init_np()
+{
+ int ret;
+
+ workqueue_list_lock();
+ ret =_pthread_work_internal_init();
+ workqueue_list_unlock();
+
+ return(ret);
+}
+
+static int
+_pthread_work_internal_init(void)
+{
+ int i, error;
+ pthread_workqueue_head_t headp;
+ pthread_workitem_t witemp;
+ pthread_workqueue_t wq;
+
+ if (kernel_workq_setup == 0) {
+#if defined(__i386__) || defined(__x86_64__)
+ __bsdthread_register(thread_start, start_wqthread, round_page(sizeof(struct _pthread)));
+#else
+ __bsdthread_register(_pthread_start, _pthread_wqthread, round_page(sizeof(struct _pthread)));
+#endif
+
+ _pthread_wq_attr_default.stacksize = DEFAULT_STACK_SIZE;
+ _pthread_wq_attr_default.istimeshare = 1;
+ _pthread_wq_attr_default.importance = 0;
+ _pthread_wq_attr_default.affinity = 0;
+ _pthread_wq_attr_default.queueprio = WORK_QUEUE_NORMALIZER;
+ _pthread_wq_attr_default.sig = PTHEAD_WRKQUEUE_ATTR_SIG;
+
+ for( i = 0; i< WQ_NUM_PRIO_QS; i++) {
+ headp = __pthread_wq_head_tbl[i];
+ TAILQ_INIT(&headp->wqhead);
+ headp->next_workq = 0;
+ }
+
+ /* create work item and workqueue pools */
+ witemp = (struct _pthread_workitem *)malloc(sizeof(struct _pthread_workitem) * WORKITEM_POOL_SIZE);
+ bzero(witemp, (sizeof(struct _pthread_workitem) * WORKITEM_POOL_SIZE));
+ for (i = 0; i < WORKITEM_POOL_SIZE; i++) {
+ TAILQ_INSERT_TAIL(&__pthread_workitem_pool_head, &witemp[i], item_entry);
+ }
+ wq = (struct _pthread_workqueue *)malloc(sizeof(struct _pthread_workqueue) * WORKQUEUE_POOL_SIZE);
+ bzero(wq, (sizeof(struct _pthread_workqueue) * WORKQUEUE_POOL_SIZE));
+ for (i = 0; i < WORKQUEUE_POOL_SIZE; i++) {
+ TAILQ_INSERT_TAIL(&__pthread_workqueue_pool_head, &wq[i], wq_list);
+ }
+
+ if (error = __workq_open()) {
+ TAILQ_INIT(&__pthread_workitem_pool_head);
+ TAILQ_INIT(&__pthread_workqueue_pool_head);
+ free(witemp);
+ free(wq);
+ return(ENOMEM);
+ }
+ kernel_workq_setup = 1;
+ }
+ return(0);
+}
+
+
+/* This routine is called with list lock held */
+static pthread_workitem_t
+alloc_workitem(void)
+{
+ pthread_workitem_t witem;
+
+ if (TAILQ_EMPTY(&__pthread_workitem_pool_head)) {
+ workqueue_list_unlock();
+ witem = malloc(sizeof(struct _pthread_workitem));
+ workqueue_list_lock();
+ } else {
+ witem = TAILQ_FIRST(&__pthread_workitem_pool_head);
+ TAILQ_REMOVE(&__pthread_workitem_pool_head, witem, item_entry);
+ }
+ return(witem);
+}
+
+/* This routine is called with list lock held */
+static void
+free_workitem(pthread_workitem_t witem)
+{
+ TAILQ_INSERT_TAIL(&__pthread_workitem_pool_head, witem, item_entry);
+}
+
+/* This routine is called with list lock held */
+static pthread_workqueue_t
+alloc_workqueue(void)
+{
+ pthread_workqueue_t wq;
+
+ if (TAILQ_EMPTY(&__pthread_workqueue_pool_head)) {
+ workqueue_list_unlock();
+ wq = malloc(sizeof(struct _pthread_workqueue));
+ workqueue_list_lock();
+ } else {
+ wq = TAILQ_FIRST(&__pthread_workqueue_pool_head);
+ TAILQ_REMOVE(&__pthread_workqueue_pool_head, wq, wq_list);
+ }
+ user_workq_count++;
+ return(wq);
+}
+
+/* This routine is called with list lock held */
+static void
+free_workqueue(pthread_workqueue_t wq)
+{
+ user_workq_count--;
+ TAILQ_INSERT_TAIL(&__pthread_workqueue_pool_head, wq, wq_list);
+}
+
+static void
+_pthread_workq_init(pthread_workqueue_t wq, const pthread_workqueue_attr_t * attr)
+{
+ bzero(wq, sizeof(struct _pthread_workqueue));
+ if (attr != NULL) {
+ wq->stacksize = attr->stacksize;
+ wq->istimeshare = attr->istimeshare;
+ wq->importance = attr->importance;
+ wq->affinity = attr->affinity;
+ wq->queueprio = attr->queueprio;
+ } else {
+ wq->stacksize = DEFAULT_STACK_SIZE;
+ wq->istimeshare = 1;
+ wq->importance = 0;
+ wq->affinity = 0;
+ wq->queueprio = WORK_QUEUE_NORMALIZER;
+ }
+ LOCK_INIT(wq->lock);
+ wq->flags = 0;
+ TAILQ_INIT(&wq->item_listhead);
+ TAILQ_INIT(&wq->item_kernhead);
+ wq->wq_list.tqe_next = 0;
+ wq->wq_list.tqe_prev = 0;
+ wq->sig = PTHEAD_WRKQUEUE_SIG;
+ wq->headp = __pthread_wq_head_tbl[wq->queueprio];
+}
+
+int
+valid_workq(pthread_workqueue_t workq)
+{
+ if (workq->sig == PTHEAD_WRKQUEUE_SIG)
+ return(1);
+ else
+ return(0);
+}
+
+
+/* called with list lock */
+static void
+pick_nextworkqueue_droplock()
+{
+ int i, curwqprio, val, found;
+ pthread_workqueue_head_t headp;
+ pthread_workqueue_t workq;
+ pthread_workqueue_t nworkq = NULL;
+
+loop:
+ while (kernel_workq_count < KERNEL_WORKQ_ELEM_MAX) {
+ found = 0;
+ for (i = 0; i < WQ_NUM_PRIO_QS; i++) {
+ wqreadyprio = i; /* because there is nothing else higher to run */
+ headp = __pthread_wq_head_tbl[i];
+
+ if (TAILQ_EMPTY(&headp->wqhead))
+ continue;
+ workq = headp->next_workq;
+ if (workq == NULL)
+ workq = TAILQ_FIRST(&headp->wqhead);
+ curwqprio = workq->queueprio;
+ nworkq = workq; /* starting pt */
+ while (kernel_workq_count < KERNEL_WORKQ_ELEM_MAX) {
+ headp->next_workq = TAILQ_NEXT(workq, wq_list);
+ if (headp->next_workq == NULL)
+ headp->next_workq = TAILQ_FIRST(&headp->wqhead);
+ val = post_nextworkitem(workq);
+
+ if (val != 0) {
+ /* things could have changed so reasses */
+ /* If kernel queue is full , skip */
+ if (kernel_workq_count >= KERNEL_WORKQ_ELEM_MAX)
+ break;
+ /* If anything with higher prio arrived, then reevaluate */
+ if (wqreadyprio < curwqprio)
+ goto loop; /* we need re evaluate again */
+ /* we can post some more work items */
+ found = 1;
+ }
+
+ /* cannot use workq here as it could be freed */
+ if (TAILQ_EMPTY(&headp->wqhead))
+ break;
+ /* if we found nothing to run and only one workqueue in the list, skip */
+ if ((val == 0) && (workq == headp->next_workq))
+ break;
+ workq = headp->next_workq;
+ if (workq == NULL)
+ workq = TAILQ_FIRST(&headp->wqhead);
+ if (val != 0)
+ nworkq = workq;
+ /* if we found nothing to run and back to workq where we started */
+ if ((val == 0) && (workq == nworkq))
+ break;
+ }
+ if (kernel_workq_count >= KERNEL_WORKQ_ELEM_MAX)
+ break;
+ }
+ /* nothing found to run? */
+ if (found == 0)
+ break;
+ }
+ workqueue_list_unlock();
+}
+
+static int
+post_nextworkitem(pthread_workqueue_t workq)
+{
+ int error;
+ pthread_workitem_t witem;
+ pthread_workqueue_head_t headp;
+ void (*func)(pthread_workqueue_t, void *);
+
+ if ((workq->flags & PTHREAD_WORKQ_SUSPEND) == PTHREAD_WORKQ_SUSPEND) {
+ return(0);
+ }
+ if (TAILQ_EMPTY(&workq->item_listhead)) {
+ return(0);
+ }
+ witem = TAILQ_FIRST(&workq->item_listhead);
+ headp = workq->headp;
+ if ((witem->flags & PTH_WQITEM_BARRIER) == PTH_WQITEM_BARRIER) {
+
+ if ((witem->flags & PTH_WQITEM_APPLIED) != 0) {
+ return(0);
+ }
+ /* Also barrier when nothing is there needs to be handled */
+ /* Nothing to wait for */
+ if (workq->kq_count != 0) {
+ witem->flags |= PTH_WQITEM_APPLIED;
+ workq->flags |= PTHREAD_WORKQ_BARRIER_ON;
+ workq->barrier_count = workq->kq_count;
+#if WQ_TRACE
+ __kdebug_trace(0x9000064, 1, workq->barrier_count, 0, 0, 0);
+#endif
+ return(1);
+ } else {
+#if WQ_TRACE
+ __kdebug_trace(0x9000064, 2, workq->barrier_count, 0, 0, 0);
+#endif
+ if (witem->func != NULL) {
+ workqueue_list_unlock();
+ func = witem->func;
+ (*func)(workq, witem->func_arg);
+ workqueue_list_lock();
+ }
+ TAILQ_REMOVE(&workq->item_listhead, witem, item_entry);
+ witem->flags = 0;
+ free_workitem(witem);
+ return(1);
+ }
+ } else if ((witem->flags & PTH_WQITEM_DESTROY) == PTH_WQITEM_DESTROY) {
+#if WQ_TRACE
+ __kdebug_trace(0x9000068, 1, workq->kq_count, 0, 0, 0);
+#endif
+ if ((witem->flags & PTH_WQITEM_APPLIED) != 0) {
+ return(0);
+ }
+ witem->flags |= PTH_WQITEM_APPLIED;
+ workq->flags |= (PTHREAD_WORKQ_BARRIER_ON | PTHREAD_WORKQ_TERM_ON);
+ workq->barrier_count = workq->kq_count;
+ workq->term_callback = witem->func;
+ workq->term_callarg = witem->func_arg;
+ TAILQ_REMOVE(&workq->item_listhead, witem, item_entry);
+ if ((TAILQ_EMPTY(&workq->item_listhead)) && (workq->kq_count == 0)) {
+ if (!(TAILQ_EMPTY(&workq->item_kernhead))) {
+#if WQ_TRACE
+ __kdebug_trace(0x900006c, workq, workq->kq_count, 0, 0xff, 0);
+#endif
+ }
+ witem->flags = 0;
+ free_workitem(witem);
+ workq->flags |= PTHREAD_WORKQ_DESTROYED;
+#if WQ_TRACE
+ __kdebug_trace(0x900006c, workq, workq->kq_count, 0, 1, 0);
+#endif
+ headp = __pthread_wq_head_tbl[workq->queueprio];
+ if (headp->next_workq == workq) {
+ headp->next_workq = TAILQ_NEXT(workq, wq_list);
+ if (headp->next_workq == NULL) {
+ headp->next_workq = TAILQ_FIRST(&headp->wqhead);
+ if (headp->next_workq == workq)
+ headp->next_workq = NULL;
+ }
+ }
+ workq->sig = 0;
+ TAILQ_REMOVE(&headp->wqhead, workq, wq_list);
+ if (workq->term_callback != NULL) {
+ workqueue_list_unlock();
+ (*workq->term_callback)(workq, workq->term_callarg);
+ workqueue_list_lock();
+ }
+ free_workqueue(workq);
+ return(1);
+ } else
+ TAILQ_INSERT_HEAD(&workq->item_listhead, witem, item_entry);
+#if WQ_TRACE
+ __kdebug_trace(0x9000068, 2, workq->barrier_count, 0, 0, 0);
+#endif
+ return(1);
+ } else {
+#if WQ_TRACE
+ __kdebug_trace(0x9000060, witem, workq, witem->func_arg, 0xfff, 0);
+#endif
+ TAILQ_REMOVE(&workq->item_listhead, witem, item_entry);
+ TAILQ_INSERT_TAIL(&workq->item_kernhead, witem, item_entry);
+ if ((witem->flags & PTH_WQITEM_KERN_COUNT) == 0) {
+ workq->kq_count++;
+ witem->flags |= PTH_WQITEM_KERN_COUNT;
+ }
+ OSAtomicIncrement32(&kernel_workq_count);
+ workqueue_list_unlock();
+ if (( error =__workq_ops(WQOPS_QUEUE_ADD, witem, 0)) == -1) {
+ OSAtomicDecrement32(&kernel_workq_count);
+ workqueue_list_lock();
+#if WQ_TRACE
+ __kdebug_trace(0x900007c, witem, workq, witem->func_arg, workq->kq_count, 0);
+#endif
+ TAILQ_REMOVE(&workq->item_kernhead, witem, item_entry);
+ TAILQ_INSERT_HEAD(&workq->item_listhead, witem, item_entry);
+ if ((workq->flags & (PTHREAD_WORKQ_BARRIER_ON | PTHREAD_WORKQ_TERM_ON)) != 0)
+ workq->flags |= PTHREAD_WORKQ_REQUEUED;
+ } else
+ workqueue_list_lock();
+#if WQ_TRACE
+ __kdebug_trace(0x9000060, witem, workq, witem->func_arg, workq->kq_count, 0);
+#endif
+ return(1);
+ }
+ /* noone should come here */
+#if 1
+ printf("error in logic for next workitem\n");
+ abort();
+#endif
+ return(0);
+}
+
+void
+_pthread_wqthread(pthread_t self, mach_port_t kport, void * stackaddr, pthread_workitem_t item, int reuse)
+{
+ int ret;
+ pthread_attr_t *attrs = &_pthread_attr_default;
+ pthread_workqueue_t workq;
+ pthread_t pself;
+
+
+ workq = item->workq;
+ if (reuse == 0) {
+ /* reuse is set to 0, when a thread is newly created to run a workitem */
+ _pthread_struct_init(self, attrs, stackaddr, DEFAULT_STACK_SIZE, 1, 1);
+ self->wqthread = 1;
+ self->parentcheck = 1;
+
+ /* These are not joinable threads */
+ self->detached &= ~PTHREAD_CREATE_JOINABLE;
+ self->detached |= PTHREAD_CREATE_DETACHED;
+#if defined(__i386__) || defined(__x86_64__)
+ _pthread_set_self(self);
+#endif
+#if WQ_TRACE
+ __kdebug_trace(0x9000050, self, item, item->func_arg, 0, 0);
+#endif
+ self->kernel_thread = kport;
+ self->fun = item->func;
+ self->arg = item->func_arg;
+ /* Add to the pthread list */
+ LOCK(_pthread_list_lock);
+ TAILQ_INSERT_TAIL(&__pthread_head, self, plist);
+#if WQ_TRACE
+ __kdebug_trace(0x900000c, self, 0, 0, 10, 0);
+#endif
+ _pthread_count++;
+ UNLOCK(_pthread_list_lock);
+ } else {
+ /* reuse is set to 1, when a thread is resued to run another work item */
+#if WQ_TRACE
+ __kdebug_trace(0x9000054, self, item, item->func_arg, 0, 0);
+#endif
+ /* reset all tsd from 1 to KEYS_MAX */
+ _pthread_tsd_reinit(self);
+
+ self->fun = item->func;
+ self->arg = item->func_arg;
+ }
+
+#if WQ_DEBUG
+ if (reuse == 0) {
+ pself = pthread_self();
+ if (self != pself) {
+#if WQ_TRACE
+ __kdebug_trace(0x9000078, self, pself, item->func_arg, 0, 0);
+#endif
+ printf("pthread_self not set: pself %p, passed in %p\n", pself, self);
+ _pthread_set_self(self);
+ pself = pthread_self();
+ if (self != pself)
+ printf("(2)pthread_self not set: pself %p, passed in %p\n", pself, self);
+ pself = self;
+ }
+ } else {
+ pself = pthread_self();
+ if (self != pself) {
+ printf("(3)pthread_self not set in reuse: pself %p, passed in %p\n", pself, self);
+ abort();
+ }
+ }
+#endif /* WQ_DEBUG */
+
+ self->cur_workq = workq;
+ self->cur_workitem = item;
+ OSAtomicDecrement32(&kernel_workq_count);
+
+ ret = (*self->fun)(self->arg);
+
+ workqueue_exit(self, workq, item);
+
+}
+
+static void
+workqueue_exit(pthread_t self, pthread_workqueue_t workq, pthread_workitem_t item)
+{
+ pthread_attr_t *attrs = &_pthread_attr_default;
+ pthread_workitem_t baritem;
+ pthread_workqueue_head_t headp;
+ void (*func)(pthread_workqueue_t, void *);
+
+ workqueue_list_lock();
+
+ TAILQ_REMOVE(&workq->item_kernhead, item, item_entry);
+ workq->kq_count--;
+#if WQ_TRACE
+ __kdebug_trace(0x9000070, self, 1, item->func_arg, workq->kq_count, 0);
+#endif
+ item->flags = 0;
+ free_workitem(item);
+
+ if ((workq->flags & PTHREAD_WORKQ_BARRIER_ON) == PTHREAD_WORKQ_BARRIER_ON) {
+ workq->barrier_count--;
+#if WQ_TRACE
+ __kdebug_trace(0x9000084, self, workq->barrier_count, workq->kq_count, 1, 0);
+#endif
+ if (workq->barrier_count <= 0 ) {
+ /* Need to remove barrier item from the list */
+ baritem = TAILQ_FIRST(&workq->item_listhead);
+#if WQ_DEBUG
+ if ((baritem->flags & (PTH_WQITEM_BARRIER | PTH_WQITEM_DESTROY| PTH_WQITEM_APPLIED)) == 0)
+ printf("Incorect bar item being removed in barrier processing\n");
+#endif /* WQ_DEBUG */
+ /* if the front item is a barrier and call back is registered, run that */
+ if (((baritem->flags & PTH_WQITEM_BARRIER) == PTH_WQITEM_BARRIER) && (baritem->func != NULL)) {
+ workqueue_list_unlock();
+ func = baritem->func;
+ (*func)(workq, baritem->func_arg);
+ workqueue_list_lock();
+ }
+ TAILQ_REMOVE(&workq->item_listhead, baritem, item_entry);
+ baritem->flags = 0;
+ free_workitem(baritem);
+ workq->flags &= ~PTHREAD_WORKQ_BARRIER_ON;
+#if WQ_TRACE
+ __kdebug_trace(0x9000058, self, item, item->func_arg, 0, 0);
+#endif
+ if ((workq->flags & PTHREAD_WORKQ_TERM_ON) != 0) {
+ headp = __pthread_wq_head_tbl[workq->queueprio];
+ workq->flags |= PTHREAD_WORKQ_DESTROYED;
+#if WQ_TRACE
+ __kdebug_trace(0x900006c, workq, workq->kq_count, 0, 2, 0);
+#endif
+ if (headp->next_workq == workq) {
+ headp->next_workq = TAILQ_NEXT(workq, wq_list);
+ if (headp->next_workq == NULL) {
+ headp->next_workq = TAILQ_FIRST(&headp->wqhead);
+ if (headp->next_workq == workq)
+ headp->next_workq = NULL;
+ }
+ }
+ TAILQ_REMOVE(&headp->wqhead, workq, wq_list);
+ workq->sig = 0;
+ if (workq->term_callback != NULL) {
+ workqueue_list_unlock();
+ (*workq->term_callback)(workq, workq->term_callarg);
+ workqueue_list_lock();
+ }
+ free_workqueue(workq);
+ } else {
+ /* if there are higher prio schedulabel item reset to wqreadyprio */
+ if ((workq->queueprio < wqreadyprio) && (!(TAILQ_EMPTY(&workq->item_listhead))))
+ wqreadyprio = workq->queueprio;
+ }
+ }
+ }
+#if WQ_TRACE
+ else {
+ __kdebug_trace(0x9000070, self, 2, item->func_arg, workq->barrier_count, 0);
+ }
+
+ __kdebug_trace(0x900005c, self, item, 0, 0, 0);
+#endif
+ pick_nextworkqueue_droplock();
+ _pthread_workq_return(self);
+}
+
+static void
+_pthread_workq_return(pthread_t self)
+{
+ struct __darwin_pthread_handler_rec *handler;
+ int value = 0;
+ int * value_ptr=&value;
+
+ /* set cancel state to disable and type to deferred */
+ _pthread_setcancelstate_exit(self, value_ptr, __unix_conforming);
+
+ /* Make this thread not to receive any signals */
+ __disable_threadsignal(1);
+
+ while ((handler = self->__cleanup_stack) != 0)
+ {
+ (handler->__routine)(handler->__arg);
+ self->__cleanup_stack = handler->__next;
+ }
+ _pthread_tsd_cleanup(self);
+
+ __workq_ops(WQOPS_THREAD_RETURN, NULL, 0);
+
+ /* This is the way to terminate the thread */
+ _pthread_exit(self, NULL);
+}
+
+
+/* returns 0 if it handles it, otherwise 1 */
+static int
+handle_removeitem(pthread_workqueue_t workq, pthread_workitem_t item)
+{
+ pthread_workitem_t baritem;
+ pthread_workqueue_head_t headp;
+ void (*func)(pthread_workqueue_t, void *);
+
+ if ((workq->flags & PTHREAD_WORKQ_BARRIER_ON) == PTHREAD_WORKQ_BARRIER_ON) {
+ workq->barrier_count--;
+ if (workq->barrier_count <= 0 ) {
+ /* Need to remove barrier item from the list */
+ baritem = TAILQ_FIRST(&workq->item_listhead);
+#if WQ_DEBUG
+ if ((baritem->flags & (PTH_WQITEM_BARRIER | PTH_WQITEM_DESTROY| PTH_WQITEM_APPLIED)) == 0)
+ printf("Incorect bar item being removed in barrier processing\n");
+#endif /* WQ_DEBUG */
+ /* if the front item is a barrier and call back is registered, run that */
+ if (((baritem->flags & PTH_WQITEM_BARRIER) == PTH_WQITEM_BARRIER)
+ && (baritem->func != NULL)) {
+ workqueue_list_unlock();
+ func = baritem->func;
+ (*func)(workq, baritem->func_arg);
+ workqueue_list_lock();
+ }
+ TAILQ_REMOVE(&workq->item_listhead, baritem, item_entry);
+ baritem->flags = 0;
+ free_workitem(baritem);
+ item->flags = 0;
+ free_workitem(item);
+ workq->flags &= ~PTHREAD_WORKQ_BARRIER_ON;
+#if WQ_TRACE
+ __kdebug_trace(0x9000058, pthread_self(), item, item->func_arg, 0, 0);
+#endif
+ if ((workq->flags & PTHREAD_WORKQ_TERM_ON) != 0) {
+ headp = __pthread_wq_head_tbl[workq->queueprio];
+ workq->flags |= PTHREAD_WORKQ_DESTROYED;
+#if WQ_TRACE
+ __kdebug_trace(0x900006c, workq, workq->kq_count, 0, 2, 0);
+#endif
+ if (headp->next_workq == workq) {
+ headp->next_workq = TAILQ_NEXT(workq, wq_list);
+ if (headp->next_workq == NULL) {
+ headp->next_workq = TAILQ_FIRST(&headp->wqhead);
+ if (headp->next_workq == workq)
+ headp->next_workq = NULL;
+ }
+ }
+ TAILQ_REMOVE(&headp->wqhead, workq, wq_list);
+ workq->sig = 0;
+ if (workq->term_callback != NULL) {
+ workqueue_list_unlock();
+ (*workq->term_callback)(workq, workq->term_callarg);
+ workqueue_list_lock();
+ }
+ free_workqueue(workq);
+ pick_nextworkqueue_droplock();
+ return(0);
+ } else {
+ /* if there are higher prio schedulabel item reset to wqreadyprio */
+ if ((workq->queueprio < wqreadyprio) && (!(TAILQ_EMPTY(&workq->item_listhead))))
+ wqreadyprio = workq->queueprio;
+ free_workitem(item);
+ pick_nextworkqueue_droplock();
+ return(0);
+ }
+ }
+ }
+ return(1);
+}
+/* XXXXXXXXXXXXX Pthread Workqueue functions XXXXXXXXXXXXXXXXXX */
+
+int
+pthread_workqueue_create_np(pthread_workqueue_t * workqp, const pthread_workqueue_attr_t * attr)
+{
+ pthread_workqueue_t wq;
+ pthread_workqueue_head_t headp;
+
+ if ((attr != NULL) && (attr->sig != PTHEAD_WRKQUEUE_ATTR_SIG)) {
+ return(EINVAL);
+ }
+
+ if (__is_threaded == 0)
+ __is_threaded = 1;
+
+ workqueue_list_lock();
+ if (kernel_workq_setup == 0) {
+ int ret = _pthread_work_internal_init();
+ if (ret != 0) {
+ workqueue_list_unlock();
+ return(ret);
+ }
+ }
+
+ wq = alloc_workqueue();
+
+ _pthread_workq_init(wq, attr);
+
+ headp = __pthread_wq_head_tbl[wq->queueprio];
+ TAILQ_INSERT_TAIL(&headp->wqhead, wq, wq_list);
+ if (headp->next_workq == NULL) {
+ headp->next_workq = TAILQ_FIRST(&headp->wqhead);
+ }
+
+ workqueue_list_unlock();
+
+ *workqp = wq;
+
+ return(0);
+}
+
+int
+pthread_workqueue_destroy_np(pthread_workqueue_t workq, void (* callback_func)(pthread_workqueue_t, void *), void * callback_arg)
+{
+ pthread_workitem_t witem;
+ pthread_workqueue_head_t headp;
+
+ if (valid_workq(workq) == 0) {
+ return(EINVAL);
+ }
+
+ workqueue_list_lock();
+
+ /*
+ * Allocate the workitem here as it can drop the lock.
+ * Also we can evaluate the workqueue state only once.
+ */
+ witem = alloc_workitem();
+ witem->item_entry.tqe_next = 0;
+ witem->item_entry.tqe_prev = 0;
+ witem->func = callback_func;
+ witem->func_arg = callback_arg;
+ witem->flags = PTH_WQITEM_DESTROY;
+
+ if ((workq->flags & (PTHREAD_WORKQ_IN_TERMINATE | PTHREAD_WORKQ_TERM_ON | PTHREAD_WORKQ_DESTROYED)) == 0) {
+ workq->flags |= PTHREAD_WORKQ_IN_TERMINATE;
+ /* If nothing queued or running, destroy now */
+ if ((TAILQ_EMPTY(&workq->item_listhead)) && (TAILQ_EMPTY(&workq->item_kernhead))) {
+ workq->flags |= (PTHREAD_WORKQ_TERM_ON | PTHREAD_WORKQ_DESTROYED);
+ headp = __pthread_wq_head_tbl[workq->queueprio];
+ workq->term_callback = callback_func;
+ workq->term_callarg = callback_arg;
+ if (headp->next_workq == workq) {
+ headp->next_workq = TAILQ_NEXT(workq, wq_list);
+ if (headp->next_workq == NULL) {
+ headp->next_workq = TAILQ_FIRST(&headp->wqhead);
+ if (headp->next_workq == workq)
+ headp->next_workq = NULL;
+ }
+ }
+ TAILQ_REMOVE(&headp->wqhead, workq, wq_list);
+ workq->sig = 0;
+ free_workitem(witem);
+ if (workq->term_callback != NULL) {
+ workqueue_list_unlock();
+ (*workq->term_callback)(workq, workq->term_callarg);
+ workqueue_list_lock();
+ }
+#if WQ_TRACE
+ __kdebug_trace(0x900006c, workq, workq->kq_count, 0, 3, 0);
+#endif
+ free_workqueue(workq);
+ workqueue_list_unlock();
+ return(0);
+ }
+ TAILQ_INSERT_TAIL(&workq->item_listhead, witem, item_entry);
+ } else {
+ free_workitem(witem);
+ workqueue_list_unlock();
+ return(EINPROGRESS);
+ }
+ workqueue_list_unlock();
+ return(0);
+}
+
+
+int
+pthread_workqueue_additem_np(pthread_workqueue_t workq, void ( *workitem_func)(void *), void * workitem_arg, pthread_workitem_handle_t * itemhandlep)
+{
+ pthread_workitem_t witem;
+
+ if (valid_workq(workq) == 0) {
+ return(EINVAL);
+ }
+
+ workqueue_list_lock();
+
+ /*
+ * Allocate the workitem here as it can drop the lock.
+ * Also we can evaluate the workqueue state only once.
+ */
+ witem = alloc_workitem();
+ witem->func = workitem_func;
+ witem->func_arg = workitem_arg;
+ witem->flags = 0;
+ witem->workq = workq;
+ witem->item_entry.tqe_next = 0;
+ witem->item_entry.tqe_prev = 0;
+
+ /* alloc workitem can drop the lock, check the state */
+ if ((workq->flags & (PTHREAD_WORKQ_IN_TERMINATE | PTHREAD_WORKQ_DESTROYED)) != 0) {
+ free_workitem(witem);
+ workqueue_list_unlock();
+ *itemhandlep = 0;
+ return(ESRCH);
+ }
+
+ if (itemhandlep != NULL)
+ *itemhandlep = (pthread_workitem_handle_t *)witem;
+ TAILQ_INSERT_TAIL(&workq->item_listhead, witem, item_entry);
+ if (((workq->flags & PTHREAD_WORKQ_BARRIER_ON) == 0) && (workq->queueprio < wqreadyprio))
+ wqreadyprio = workq->queueprio;
+
+ pick_nextworkqueue_droplock();
+
+ return(0);
+}
+
+int
+pthread_workqueue_removeitem_np(pthread_workqueue_t workq, pthread_workitem_handle_t itemhandle)
+{
+ pthread_workitem_t item, baritem;
+ pthread_workqueue_head_t headp;
+ int error;
+
+ if (valid_workq(workq) == 0) {
+ return(EINVAL);
+ }
+
+ workqueue_list_lock();
+ if ((workq->flags & (PTHREAD_WORKQ_IN_TERMINATE | PTHREAD_WORKQ_DESTROYED)) != 0) {
+ workqueue_list_unlock();
+ return(ESRCH);
+ }
+
+ TAILQ_FOREACH(item, &workq->item_listhead, item_entry) {
+ if (item == (pthread_workitem_t)itemhandle) {
+ TAILQ_REMOVE(&workq->item_listhead, item, item_entry);
+ if ((item->flags & (PTH_WQITEM_BARRIER | PTH_WQITEM_APPLIED)) == (PTH_WQITEM_BARRIER | PTH_WQITEM_APPLIED)) {
+ workq->flags &= ~PTHREAD_WORKQ_BARRIER_ON;
+ workq->barrier_count = 0;
+ if ((workq->queueprio < wqreadyprio) && (!(TAILQ_EMPTY(&workq->item_listhead)))) {
+ wqreadyprio = workq->queueprio;
+ }
+ } else if ((item->flags & PTH_WQITEM_KERN_COUNT) == PTH_WQITEM_KERN_COUNT) {
+ workq->kq_count--;
+ item->flags |= PTH_WQITEM_REMOVED;
+ if (handle_removeitem(workq, item) == 0)
+ return(0);
+ }
+ item->flags |= PTH_WQITEM_NOTINLIST;
+ free_workitem(item);
+ workqueue_list_unlock();
+ return(0);
+ }
+ }
+
+ TAILQ_FOREACH(item, &workq->item_kernhead, item_entry) {
+ if (item == (pthread_workitem_t)itemhandle) {
+ workqueue_list_unlock();
+ if ((error = __workq_ops(WQOPS_QUEUE_REMOVE, item, 0)) == 0) {
+ workqueue_list_lock();
+ TAILQ_REMOVE(&workq->item_kernhead, item, item_entry);
+ OSAtomicDecrement32(&kernel_workq_count);
+ workq->kq_count--;
+ item->flags |= PTH_WQITEM_REMOVED;
+ if (handle_removeitem(workq, item) != 0) {
+ free_workitem(item);
+ pick_nextworkqueue_droplock();
+ }
+ return(0);
+ } else {
+ return(EBUSY);
+ }
+ }
+ }
+ workqueue_list_unlock();
+ return(EINVAL);
+}
+
+
+int
+pthread_workqueue_addbarrier_np(pthread_workqueue_t workq, void (* callback_func)(pthread_workqueue_t, void *), void * callback_arg, __unused int waitforcallback, pthread_workitem_handle_t *itemhandlep)
+{
+ pthread_workitem_t witem;
+
+ if (valid_workq(workq) == 0) {
+ return(EINVAL);
+ }
+
+ workqueue_list_lock();
+
+ /*
+ * Allocate the workitem here as it can drop the lock.
+ * Also we can evaluate the workqueue state only once.
+ */
+ witem = alloc_workitem();
+ witem->item_entry.tqe_next = 0;
+ witem->item_entry.tqe_prev = 0;
+ witem->func = callback_func;
+ witem->func_arg = callback_arg;
+ witem->flags = PTH_WQITEM_BARRIER;
+
+ /* alloc workitem can drop the lock, check the state */
+ if ((workq->flags & (PTHREAD_WORKQ_IN_TERMINATE | PTHREAD_WORKQ_DESTROYED)) != 0) {
+ free_workitem(witem);
+ workqueue_list_unlock();
+ return(ESRCH);
+ }
+
+ if (itemhandlep != NULL)
+ *itemhandlep = (pthread_workitem_handle_t *)witem;
+
+ TAILQ_INSERT_TAIL(&workq->item_listhead, witem, item_entry);
+ if (((workq->flags & PTHREAD_WORKQ_BARRIER_ON) == 0) && (workq->queueprio < wqreadyprio))
+ wqreadyprio = workq->queueprio;
+
+ pick_nextworkqueue_droplock();
+
+ return(0);
+}
+
+int
+pthread_workqueue_suspend_np(pthread_workqueue_t workq)
+{
+ if (valid_workq(workq) == 0) {
+ return(EINVAL);
+ }
+ workqueue_list_lock();
+ if ((workq->flags & (PTHREAD_WORKQ_IN_TERMINATE | PTHREAD_WORKQ_DESTROYED)) != 0) {
+ workqueue_list_unlock();
+ return(ESRCH);
+ }
+
+ workq->flags |= PTHREAD_WORKQ_SUSPEND;
+ workq->suspend_count++;
+ workqueue_list_unlock();
+ return(0);
+}
+
+int
+pthread_workqueue_resume_np(pthread_workqueue_t workq)
+{
+ if (valid_workq(workq) == 0) {
+ return(EINVAL);
+ }
+ workqueue_list_lock();
+ if ((workq->flags & (PTHREAD_WORKQ_IN_TERMINATE | PTHREAD_WORKQ_DESTROYED)) != 0) {
+ workqueue_list_unlock();
+ return(ESRCH);
+ }
+
+ workq->suspend_count--;
+ if (workq->suspend_count <= 0) {
+ workq->flags &= ~PTHREAD_WORKQ_SUSPEND;
+ if (((workq->flags & PTHREAD_WORKQ_BARRIER_ON) == 0) && (workq->queueprio < wqreadyprio))
+ wqreadyprio = workq->queueprio;
+
+ pick_nextworkqueue_droplock();
+ } else
+ workqueue_list_unlock();
+
+
+ return(0);
+}
+
+#else /* !BUILDING_VARIANT ] [ */
+extern int __unix_conforming;
+extern int _pthread_count;
+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, int conforming);
+
+#endif /* !BUILDING_VARIANT ] */
+
+#if __DARWIN_UNIX03
+
+__private_extern__ void
+__posix_join_cleanup(void *arg)
+{
+ pthread_t thread = (pthread_t)arg;
+ int already_exited, res;
+ void * dummy;
+ semaphore_t death;
+ mach_port_t joinport;
+ int newstyle = 0;
+
+ LOCK(thread->lock);
+ already_exited = (thread->detached & _PTHREAD_EXITED);
+
+ newstyle = thread->newstyle;
+
+#if WQ_TRACE
+ __kdebug_trace(0x900002c, thread, newstyle, 0, 0, 0);
+#endif
+ if (newstyle = 0) {
+ death = thread->death;
+ 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, 1)) == EAGAIN)
+ {
+ sched_yield();
+ }
+ restore_sem_to_pool(death);
+
+ }
+
+ } else {
+ /* leave another thread to join */
+ thread->joiner = (struct _pthread *)NULL;
+ UNLOCK(thread->lock);
+ }
+}
+
+#endif /* __DARWIN_UNIX03 */
+
+
+/*
+ * Wait for a thread to terminate and obtain its exit value.
+ */
+/*
+int
+pthread_join(pthread_t thread,
+ void **value_ptr)
+
+moved to pthread_cancelable.c */
+
+/*
+ * Cancel a thread
+ */
+int
+pthread_cancel(pthread_t thread)
+{
+#if __DARWIN_UNIX03
+ if (__unix_conforming == 0)
+ __unix_conforming = 1;
+#endif /* __DARWIN_UNIX03 */
+
+ if (_pthread_lookup_thread(thread, NULL, 0) != 0)
+ return(ESRCH);
+
+#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 (0);
+}
+
+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)
+{
+#if __DARWIN_UNIX03
+ if (__unix_conforming == 0) {
+ __unix_conforming = 1;
+ }
+ return (_pthread_setcancelstate_internal(state, oldstate, 1));
+#else /* __DARWIN_UNIX03 */
+ return (_pthread_setcancelstate_internal(state, oldstate, 0));
+#endif /* __DARWIN_UNIX03 */
+
+}
+
+
+
+/*
+ * 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);
+}
+
+int
+pthread_sigmask(int how, const sigset_t * set, sigset_t * oset)
+{
+#if __DARWIN_UNIX03
+ int err = 0;
+
+ if (__pthread_sigmask(how, set, oset) == -1) {
+ err = errno;
+ }
+ return(err);
+#else /* __DARWIN_UNIX03 */
+ return(__pthread_sigmask(how, set, oset));
+#endif /* __DARWIN_UNIX03 */
+}
+
+/*
+int
+sigwait(const sigset_t * set, int * sig)
+
+moved to pthread_cancelable.c */