pthread_cancelable.c [plain text]
#include "internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/queue.h>
#include <machine/vmparam.h>
#include <mach/vm_statistics.h>
extern int __unix_conforming;
extern int _pthread_setcancelstate_internal(int state, int *oldstate, int conforming);
extern void _pthread_testcancel(pthread_t thread, int isconforming);
extern int _pthread_cond_wait(pthread_cond_t *cond,
pthread_mutex_t *mutex,
const struct timespec *abstime,
int isRelative,
int isconforming);
extern int __sigwait(const sigset_t *set, int *sig);
extern int __pthread_sigmask(int, const sigset_t *, sigset_t *);
extern int __pthread_markcancel(int);
#ifdef VARIANT_CANCELABLE
extern int __semwait_signal(int cond_sem, int mutex_sem, int timeout, int relative, __int64_t tv_sec, __int32_t tv_nsec);
#else
extern int __semwait_signal(int cond_sem, int mutex_sem, int timeout, int relative, __int64_t tv_sec, __int32_t tv_nsec) __asm__("___semwait_signal_nocancel");
#endif
#ifndef VARIANT_CANCELABLE
int
pthread_cancel(pthread_t thread)
{
#if __DARWIN_UNIX03
if (__unix_conforming == 0)
__unix_conforming = 1;
#endif
if (_pthread_lookup_thread(thread, NULL, 0) != 0)
return(ESRCH);
if (thread->wqthread != 0) {
return(ENOTSUP);
}
#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(_pthread_kernel_thread(thread));
#else
thread->cancel_state |= _PTHREAD_CANCEL_PENDING;
#endif
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
_pthread_testcancel(self, 0);
#endif
}
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
return (_pthread_setcancelstate_internal(state, oldstate, 0));
#endif
}
int
pthread_setcanceltype(int type, int *oldtype)
{
pthread_t self;
#if __DARWIN_UNIX03
if (__unix_conforming == 0)
__unix_conforming = 1;
#endif
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);
#endif
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
return(__pthread_sigmask(how, set, oset));
#endif
}
#endif
#if __DARWIN_UNIX03
static void
__posix_join_cleanup(void *arg)
{
pthread_t thread = (pthread_t)arg;
LOCK(thread->lock);
thread->joiner = (struct _pthread *)NULL;
UNLOCK(thread->lock);
}
#endif
int
pthread_join(pthread_t thread,
void **value_ptr)
{
int res = 0;
pthread_t self = pthread_self();
mach_port_t kthport;
int conforming = 0;
#if !__DARWIN_UNIX03
kern_return_t kern_res;
#endif
#if __DARWIN_UNIX03
if (__unix_conforming == 0)
__unix_conforming = 1;
#ifdef VARIANT_CANCELABLE
_pthread_testcancel(self, 1);
#endif
#endif
if ((res = _pthread_lookup_thread(thread, &kthport, 1)) != 0)
return(res);
if (thread->sig == _PTHREAD_SIG) {
semaphore_t death = SEMAPHORE_NULL;
semaphore_t joinsem = SEMAPHORE_NULL;
if (thread->joiner_notify == SEMAPHORE_NULL) {
death = (semaphore_t)os_get_cached_semaphore();
}
LOCK(thread->lock);
if ((thread->detached & PTHREAD_CREATE_JOINABLE) &&
(thread->joiner == NULL)) {
PTHREAD_ASSERT(_pthread_kernel_thread(thread) == kthport);
if (thread != self && (self == NULL || self->joiner != thread)) {
if (thread->joiner_notify == SEMAPHORE_NULL) {
thread->joiner_notify = death;
death = SEMAPHORE_NULL;
}
joinsem = thread->joiner_notify;
thread->joiner = self;
UNLOCK(thread->lock);
if (death != SEMAPHORE_NULL) {
os_put_cached_semaphore((os_semaphore_t)death);
death = SEMAPHORE_NULL;
}
#if __DARWIN_UNIX03
pthread_cleanup_push(__posix_join_cleanup, (void *)thread);
do {
res = __semwait_signal(joinsem, 0, 0, 0, (int64_t)0, (int32_t)0);
} while ((res < 0) && (errno == EINTR));
pthread_cleanup_pop(0);
#else
do {
kern_res = semaphore_wait(joinsem);
} while (kern_res != KERN_SUCCESS);
#endif
os_put_cached_semaphore((os_semaphore_t)joinsem);
res = _pthread_join_cleanup(thread, value_ptr, conforming);
} else {
UNLOCK(thread->lock);
res = EDEADLK;
}
} else {
UNLOCK(thread->lock);
res = EINVAL;
}
if (death != SEMAPHORE_NULL) {
os_put_cached_semaphore((os_semaphore_t)death);
}
return res;
}
return ESRCH;
}
int
pthread_cond_wait(pthread_cond_t *cond,
pthread_mutex_t *mutex)
{
int conforming;
#if __DARWIN_UNIX03
if (__unix_conforming == 0)
__unix_conforming = 1;
#ifdef VARIANT_CANCELABLE
conforming = 1;
#else
conforming = -1;
#endif
#else
conforming = 0;
#endif
return (_pthread_cond_wait(cond, mutex, (struct timespec *)NULL, 0, conforming));
}
int
pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex,
const struct timespec *abstime)
{
int conforming;
#if __DARWIN_UNIX03
if (__unix_conforming == 0)
__unix_conforming = 1;
#ifdef VARIANT_CANCELABLE
conforming = 1;
#else
conforming = -1;
#endif
#else
conforming = 0;
#endif
return (_pthread_cond_wait(cond, mutex, abstime, 0, conforming));
}
int
sigwait(const sigset_t * set, int * sig)
{
#if __DARWIN_UNIX03
int err = 0;
if (__unix_conforming == 0)
__unix_conforming = 1;
#ifdef VARIANT_CANCELABLE
_pthread_testcancel(pthread_self(), 1);
#endif
if (__sigwait(set, sig) == -1) {
err = errno;
#ifdef VARIANT_CANCELABLE
_pthread_testcancel(pthread_self(), 1);
#endif
if (err == EINTR) {
err = 0;
}
}
return(err);
#else
if (__sigwait(set, sig) == -1) {
if (errno != EINTR) {
return -1;
}
}
return 0;
#endif
}