summaryrefslogtreecommitdiff
path: root/linuxthreads/mutex.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>1998-06-25 19:36:00 +0000
committerUlrich Drepper <drepper@redhat.com>1998-06-25 19:36:00 +0000
commit3387a425e65b839b68bd2973f6bc5ab22315cc5d (patch)
tree375713a0b865b10b9eddd9c9877ad68cf0bdc851 /linuxthreads/mutex.c
parentd47aac39992cb1dd705d8c584f4d3979d7ce4602 (diff)
Finish user stack support. Change locking code to be safe in situations with different priorities.
1998-06-25 19:27 Ulrich Drepper <drepper@cygnus.com> * attr.c: Finish user stack support. Change locking code to be safe in situations with different priorities. * cancel.c: Likewise. * condvar.c: Likewise. * internals.h: Likewise. * join.c: Likewise. * manager.c: Likewise. * mutex.c: Likewise. * pthread.c: Likewise. * ptlongjmp.c: Likewise. * queue.h: Likewise. * rwlock.c: Likewise. * semaphore.c: Likewise. * semaphore.h: Likewise. * signals.c: Likewise. * spinlock.c: Likewise. * spinlock.h: Likewise. Patches by Xavier leroy. 1998-06-25 Ulrich Drepper <drepper@cygnus.com> * sysdeps/pthread/pthread.h: Make [sg]et_stacksize and [sg]et_stackaddr prototypes always available. * sysdeps/unix/sysv/linux/bits/posix_opt.h: Define _POSIX_THREAD_ATTR_STACKSIZE and _POSIX_THREAD_ATTR_STACKADDR.
Diffstat (limited to 'linuxthreads/mutex.c')
-rw-r--r--linuxthreads/mutex.c122
1 files changed, 39 insertions, 83 deletions
diff --git a/linuxthreads/mutex.c b/linuxthreads/mutex.c
index 3b40ac04c5..54504bfbc3 100644
--- a/linuxthreads/mutex.c
+++ b/linuxthreads/mutex.c
@@ -26,23 +26,18 @@
int __pthread_mutex_init(pthread_mutex_t * mutex,
const pthread_mutexattr_t * mutex_attr)
{
- mutex->m_spinlock = 0;
- mutex->m_count = 0;
- mutex->m_owner = NULL;
+ __pthread_init_lock(&mutex->m_lock);
mutex->m_kind =
mutex_attr == NULL ? PTHREAD_MUTEX_FAST_NP : mutex_attr->mutexkind;
- queue_init(&mutex->m_waiting);
+ mutex->m_count = 0;
+ mutex->m_owner = NULL;
return 0;
}
weak_alias (__pthread_mutex_init, pthread_mutex_init)
int __pthread_mutex_destroy(pthread_mutex_t * mutex)
{
- int count;
- acquire(&mutex->m_spinlock);
- count = mutex->m_count;
- release(&mutex->m_spinlock);
- if (count > 0) return EBUSY;
+ if (mutex->m_lock.status != 0) return EBUSY;
return 0;
}
weak_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
@@ -50,40 +45,33 @@ weak_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
int __pthread_mutex_trylock(pthread_mutex_t * mutex)
{
pthread_descr self;
+ int retcode;
- acquire(&mutex->m_spinlock);
switch(mutex->m_kind) {
case PTHREAD_MUTEX_FAST_NP:
- if (mutex->m_count == 0) {
- mutex->m_count = 1;
- release(&mutex->m_spinlock);
- return 0;
- }
- break;
+ retcode = __pthread_trylock(&mutex->m_lock);
+ return retcode;
case PTHREAD_MUTEX_RECURSIVE_NP:
self = thread_self();
- if (mutex->m_count == 0 || mutex->m_owner == self) {
+ if (mutex->m_owner == self) {
mutex->m_count++;
- mutex->m_owner = self;
- release(&mutex->m_spinlock);
return 0;
}
- break;
- case PTHREAD_MUTEX_ERRORCHECK_NP:
- self = thread_self();
- if (mutex->m_count == 0) {
- mutex->m_count = 1;
+ retcode = __pthread_trylock(&mutex->m_lock);
+ if (retcode == 0) {
mutex->m_owner = self;
- release(&mutex->m_spinlock);
- return 0;
+ mutex->m_count = 0;
+ }
+ return retcode;
+ case PTHREAD_MUTEX_ERRORCHECK_NP:
+ retcode = __pthread_trylock(&mutex->m_lock);
+ if (retcode == 0) {
+ mutex->m_owner = thread_self();
}
- break;
+ return retcode;
default:
- release(&mutex->m_spinlock);
return EINVAL;
}
- release(&mutex->m_spinlock);
- return EBUSY;
}
weak_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
@@ -91,87 +79,55 @@ int __pthread_mutex_lock(pthread_mutex_t * mutex)
{
pthread_descr self;
- acquire(&mutex->m_spinlock);
switch(mutex->m_kind) {
case PTHREAD_MUTEX_FAST_NP:
- if (mutex->m_count == 0) {
- mutex->m_count = 1;
- release(&mutex->m_spinlock);
- return 0;
- }
- self = thread_self();
- break;
+ __pthread_lock(&mutex->m_lock);
+ return 0;
case PTHREAD_MUTEX_RECURSIVE_NP:
self = thread_self();
- if (mutex->m_count == 0 || mutex->m_owner == self) {
+ if (mutex->m_owner == self) {
mutex->m_count++;
- mutex->m_owner = self;
- release(&mutex->m_spinlock);
return 0;
}
- break;
+ __pthread_lock(&mutex->m_lock);
+ mutex->m_owner = self;
+ mutex->m_count = 0;
+ return 0;
case PTHREAD_MUTEX_ERRORCHECK_NP:
self = thread_self();
- if (mutex->m_count == 0) {
- mutex->m_count = 1;
- mutex->m_owner = self;
- release(&mutex->m_spinlock);
- return 0;
- } else if (mutex->m_owner == self) {
- release(&mutex->m_spinlock);
- return EDEADLK;
- }
- break;
+ if (mutex->m_owner == self) return EDEADLK;
+ __pthread_lock(&mutex->m_lock);
+ mutex->m_owner = self;
+ return 0;
default:
- release(&mutex->m_spinlock);
return EINVAL;
}
- /* Suspend ourselves */
- enqueue(&mutex->m_waiting, self);
- release(&mutex->m_spinlock);
- suspend(self); /* This is not a cancellation point */
- /* Now we own the mutex */
- ASSERT(mutex->m_count == 1);
- mutex->m_owner = self; /* for recursive and errorcheck mutexes */
- return 0;
}
weak_alias (__pthread_mutex_lock, pthread_mutex_lock)
int __pthread_mutex_unlock(pthread_mutex_t * mutex)
{
- pthread_descr th;
-
- acquire(&mutex->m_spinlock);
switch (mutex->m_kind) {
case PTHREAD_MUTEX_FAST_NP:
- break;
+ __pthread_unlock(&mutex->m_lock);
+ return 0;
case PTHREAD_MUTEX_RECURSIVE_NP:
- if (mutex->m_count >= 2) {
+ if (mutex->m_count > 0) {
mutex->m_count--;
- release(&mutex->m_spinlock);
return 0;
}
- break;
+ mutex->m_owner = NULL;
+ __pthread_unlock(&mutex->m_lock);
+ return 0;
case PTHREAD_MUTEX_ERRORCHECK_NP:
- if (mutex->m_count == 0 || mutex->m_owner != thread_self()) {
- release(&mutex->m_spinlock);
+ if (mutex->m_owner != thread_self() || mutex->m_lock.status == 0)
return EPERM;
- }
- break;
+ mutex->m_owner = NULL;
+ __pthread_unlock(&mutex->m_lock);
+ return 0;
default:
- release(&mutex->m_spinlock);
return EINVAL;
}
- th = dequeue(&mutex->m_waiting);
- /* If no waiters, unlock the mutex */
- if (th == NULL) mutex->m_count = 0;
- release(&mutex->m_spinlock);
- /* If there is a waiter, restart it with the mutex still locked */
- if (th != NULL) {
- mutex->m_owner = NULL; /* we no longer own the mutex */
- restart(th);
- }
- return 0;
}
weak_alias (__pthread_mutex_unlock, pthread_mutex_unlock)