- return (_pthread_mutex_init(mutex, attr));
-}
-
-/*
- * Lock a mutex.
- * TODO: Priority inheritance stuff
- */
-int
-pthread_mutex_lock(pthread_mutex_t *mutex)
-{
- kern_return_t kern_res;
- pthread_t self;
- int sig = mutex->sig;
-
- /* To provide backwards compat for apps using mutex incorrectly */
- if ((sig != _PTHREAD_MUTEX_SIG) && (sig != _PTHREAD_MUTEX_SIG_init)) {
- PLOCKSTAT_MUTEX_ERROR(mutex, EINVAL);
- return(EINVAL);
- }
-
- LOCK(mutex->lock);
- if (mutex->sig != _PTHREAD_MUTEX_SIG)
- {
- if (mutex->sig != _PTHREAD_MUTEX_SIG_init)
- {
- UNLOCK(mutex->lock);
- PLOCKSTAT_MUTEX_ERROR(mutex, EINVAL);
- return (EINVAL);
- }
- _pthread_mutex_init(mutex, NULL);
- self = _PTHREAD_MUTEX_OWNER_SELF;
- }
-#if defined(__i386__) || defined(__x86_64__)
- else if(mutex->mtxopts.options.pshared == PTHREAD_PROCESS_SHARED){
- UNLOCK(mutex->lock);
- return(_new_pthread_mutex_lock(mutex));
- }
-#endif /* __i386__ || __x86_64__ */
- else if (mutex->mtxopts.options.type != PTHREAD_MUTEX_NORMAL)
- {
- self = pthread_self();
- if (mutex->owner == self)
- {
- int res;
-
- if (mutex->mtxopts.options.type == PTHREAD_MUTEX_RECURSIVE)
- {
- if (mutex->mtxopts.options.lock_count < USHRT_MAX)
- {
- mutex->mtxopts.options.lock_count++;
- PLOCKSTAT_MUTEX_ACQUIRE(mutex, 1, 0);
- res = 0;
- } else {
- res = EAGAIN;
- PLOCKSTAT_MUTEX_ERROR(mutex, res);
- }
- } else { /* PTHREAD_MUTEX_ERRORCHECK */
- res = EDEADLK;
- PLOCKSTAT_MUTEX_ERROR(mutex, res);
- }
- UNLOCK(mutex->lock);
- return (res);
- }
- } else
- self = _PTHREAD_MUTEX_OWNER_SELF;
-
- if (mutex->owner != (pthread_t)NULL) {
- if (mutex->waiters || mutex->owner != _PTHREAD_MUTEX_OWNER_SWITCHING)
- {
- semaphore_t sem, order;
-
- if (++mutex->waiters == 1)
- {
- mutex->sem = sem = new_sem_from_pool();
- mutex->order = order = new_sem_from_pool();
- }
- else
- {
- sem = mutex->sem;
- order = mutex->order;
- do {
- PTHREAD_MACH_CALL(semaphore_wait(order), kern_res);
- } while (kern_res == KERN_ABORTED);
- }
- UNLOCK(mutex->lock);
-
- PLOCKSTAT_MUTEX_BLOCK(mutex);
- PTHREAD_MACH_CALL(semaphore_wait_signal(sem, order), kern_res);
- while (kern_res == KERN_ABORTED)
- {
- PTHREAD_MACH_CALL(semaphore_wait(sem), kern_res);
- }
-
- PLOCKSTAT_MUTEX_BLOCKED(mutex, BLOCK_SUCCESS_PLOCKSTAT);
-
- LOCK(mutex->lock);
- if (--mutex->waiters == 0)
- {
- PTHREAD_MACH_CALL(semaphore_wait(order), kern_res);
- mutex->sem = mutex->order = SEMAPHORE_NULL;
- restore_sem_to_pool(order);
- restore_sem_to_pool(sem);
- }
- }
- else if (mutex->owner == _PTHREAD_MUTEX_OWNER_SWITCHING)
- {
- semaphore_t sem = mutex->sem;
- do {
- PTHREAD_MACH_CALL(semaphore_wait(sem), kern_res);
- } while (kern_res == KERN_ABORTED);
- mutex->sem = SEMAPHORE_NULL;
- restore_sem_to_pool(sem);
- }
- }
-
- mutex->mtxopts.options.lock_count = 1;
- mutex->owner = self;
- UNLOCK(mutex->lock);
- PLOCKSTAT_MUTEX_ACQUIRE(mutex, 0, 0);
- return (0);
-}
-
-/*
- * Attempt to lock a mutex, but don't block if this isn't possible.
- */
-int
-pthread_mutex_trylock(pthread_mutex_t *mutex)
-{
- kern_return_t kern_res;
- pthread_t self;
-
- LOCK(mutex->lock);
- if (mutex->sig != _PTHREAD_MUTEX_SIG)
- {
- if (mutex->sig != _PTHREAD_MUTEX_SIG_init)
- {
- PLOCKSTAT_MUTEX_ERROR(mutex, EINVAL);
- UNLOCK(mutex->lock);
- return (EINVAL);
- }
- _pthread_mutex_init(mutex, NULL);
- self = _PTHREAD_MUTEX_OWNER_SELF;
- }
-#if defined(__i386__) || defined(__x86_64__)
- else if(mutex->mtxopts.options.pshared == PTHREAD_PROCESS_SHARED){
- UNLOCK(mutex->lock);
- return(_new_pthread_mutex_trylock(mutex));
- }
-#endif /* __i386__ || __x86_64__ */
- else if (mutex->mtxopts.options.type != PTHREAD_MUTEX_NORMAL)
- {
- self = pthread_self();
- if (mutex->mtxopts.options.type == PTHREAD_MUTEX_RECURSIVE)
- {
- if (mutex->owner == self)
- {
- int res;
-
- if (mutex->mtxopts.options.lock_count < USHRT_MAX)
- {
- mutex->mtxopts.options.lock_count++;
- PLOCKSTAT_MUTEX_ACQUIRE(mutex, 1, 0);
- res = 0;
- } else {
- res = EAGAIN;
- PLOCKSTAT_MUTEX_ERROR(mutex, res);
- }
- UNLOCK(mutex->lock);
- return (res);
- }
- }
- } else
- self = _PTHREAD_MUTEX_OWNER_SELF;
-
- if (mutex->owner != (pthread_t)NULL)
- {
- if (mutex->waiters || mutex->owner != _PTHREAD_MUTEX_OWNER_SWITCHING)
- {
- PLOCKSTAT_MUTEX_ERROR(mutex, EBUSY);
- UNLOCK(mutex->lock);
- return (EBUSY);
- }
- else if (mutex->owner == _PTHREAD_MUTEX_OWNER_SWITCHING)
- {
- semaphore_t sem = mutex->sem;
-
- do {
- PTHREAD_MACH_CALL(semaphore_wait(sem), kern_res);
- } while (kern_res == KERN_ABORTED);
- restore_sem_to_pool(sem);
- mutex->sem = SEMAPHORE_NULL;
- }
- }
-
- mutex->mtxopts.options.lock_count = 1;
- mutex->owner = self;
- UNLOCK(mutex->lock);
- PLOCKSTAT_MUTEX_ACQUIRE(mutex, 0, 0);
- return (0);
-}
-
-/*
- * Unlock a mutex.
- * TODO: Priority inheritance stuff
- */
-int
-pthread_mutex_unlock(pthread_mutex_t *mutex)
-{
- kern_return_t kern_res;
- int waiters;
- int sig = mutex->sig;
-
-
- /* To provide backwards compat for apps using mutex incorrectly */
-
- if ((sig != _PTHREAD_MUTEX_SIG) && (sig != _PTHREAD_MUTEX_SIG_init)) {
- PLOCKSTAT_MUTEX_ERROR(mutex, EINVAL);
- return(EINVAL);
- }
- LOCK(mutex->lock);
- if (mutex->sig != _PTHREAD_MUTEX_SIG)
- {
- if (mutex->sig != _PTHREAD_MUTEX_SIG_init)
- {
- PLOCKSTAT_MUTEX_ERROR(mutex, EINVAL);
- UNLOCK(mutex->lock);
- return (EINVAL);
- }
- _pthread_mutex_init(mutex, NULL);
- }
-#if defined(__i386__) || defined(__x86_64__)
- else if(mutex->mtxopts.options.pshared == PTHREAD_PROCESS_SHARED){
- UNLOCK(mutex->lock);
- return(_new_pthread_mutex_unlock(mutex));
- }
-#endif /* __i386__ || __x86_64__ */
- else if (mutex->mtxopts.options.type != PTHREAD_MUTEX_NORMAL)
- {
- pthread_t self = pthread_self();
- if (mutex->owner != self)
- {
- PLOCKSTAT_MUTEX_ERROR(mutex, EPERM);
- UNLOCK(mutex->lock);
- return EPERM;
- } else if (mutex->mtxopts.options.type == PTHREAD_MUTEX_RECURSIVE &&
- --mutex->mtxopts.options.lock_count)
- {
- PLOCKSTAT_MUTEX_RELEASE(mutex, 1);
- UNLOCK(mutex->lock);
- return(0);
- }
- }
-
- mutex->mtxopts.options.lock_count = 0;
-
- waiters = mutex->waiters;
- if (waiters)
- {
- mutex->owner = _PTHREAD_MUTEX_OWNER_SWITCHING;
- PLOCKSTAT_MUTEX_RELEASE(mutex, 0);
- UNLOCK(mutex->lock);
- PTHREAD_MACH_CALL(semaphore_signal(mutex->sem), kern_res);
- }
- else
- {
- mutex->owner = (pthread_t)NULL;
- PLOCKSTAT_MUTEX_RELEASE(mutex, 0);
- UNLOCK(mutex->lock);
- }
- return (0);