Implemented geist's recent change to mutexes - they are now no longer

benaphores; benaphores aren't that beneficial in kernel land, the benaphores
are a way to reduce the number of kernel calls.
They can now only be released by the same thread who originally acquired it.

Included other changes geist (change 1499) did to a) fix some bugs, and
b) reflect the changes made to the mutexes.
Cleaned the files a bit up, all the commented dprintf()s in elf.c are now
deactivated through a macro.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@311 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2002-07-18 19:21:40 +00:00
parent 2b5d1dc418
commit 4bb2d8915b
5 changed files with 289 additions and 186 deletions
+66 -33
View File
@@ -1,8 +1,10 @@
/* Mutex and recursive_lock code */
/*
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <kernel.h>
#include <OS.h>
#include <lock.h>
@@ -12,44 +14,52 @@
#include <atomic.h>
#include <thread.h>
int recursive_lock_get_recursion(recursive_lock *lock)
int
recursive_lock_get_recursion(recursive_lock *lock)
{
thread_id thid = thread_get_current_thread_id();
if(lock->holder == thid) {
if (lock->holder == thid)
return lock->recursion;
} else {
return -1;
}
return -1;
}
int recursive_lock_create(recursive_lock *lock)
int
recursive_lock_create(recursive_lock *lock)
{
if(lock == NULL)
if (lock == NULL)
return EINVAL;
lock->holder = -1;
lock->recursion = 0;
lock->sem = create_sem(1, "recursive_lock_sem");
// if(lock->sem < 0)
// return -1;
return B_NO_ERROR;
}
void recursive_lock_destroy(recursive_lock *lock)
void
recursive_lock_destroy(recursive_lock *lock)
{
if(lock == NULL)
if (lock == NULL)
return;
if(lock->sem > 0)
if (lock->sem > 0)
delete_sem(lock->sem);
lock->sem = -1;
}
bool recursive_lock_lock(recursive_lock *lock)
bool
recursive_lock_lock(recursive_lock *lock)
{
thread_id thid = thread_get_current_thread_id();
bool retval = false;
if(thid != lock->holder) {
if (thid != lock->holder) {
acquire_sem(lock->sem);
lock->holder = thid;
@@ -59,15 +69,17 @@ bool recursive_lock_lock(recursive_lock *lock)
return retval;
}
bool recursive_lock_unlock(recursive_lock *lock)
bool
recursive_lock_unlock(recursive_lock *lock)
{
thread_id thid = thread_get_current_thread_id();
bool retval = false;
if(thid != lock->holder)
if (thid != lock->holder)
panic("recursive_lock %p unlocked by non-holder thread!\n", lock);
if(--lock->recursion == 0) {
if (--lock->recursion == 0) {
lock->holder = -1;
release_sem(lock->sem);
retval = true;
@@ -75,46 +87,67 @@ bool recursive_lock_unlock(recursive_lock *lock)
return retval;
}
int mutex_init(mutex *m, const char *in_name)
int
mutex_init(mutex *m, const char *in_name)
{
const char *name;
if(m == NULL)
if (m == NULL)
return EINVAL;
if(in_name == NULL)
if (in_name == NULL)
name = "mutex_sem";
else
name = in_name;
m->count = 0;
m->holder = -1;
m->sem = create_sem(0, name);
if(m->sem < 0)
m->sem = create_sem(1, name);
if (m->sem < 0)
return m->sem;
return 0;
}
void mutex_destroy(mutex *m)
void
mutex_destroy(mutex *mutex)
{
if(m == NULL)
if (mutex == NULL)
return;
if(m->sem >= 0) {
delete_sem(m->sem);
if (mutex->sem >= 0) {
delete_sem(mutex->sem);
mutex->sem = -1;
}
mutex->holder = -1;
}
void mutex_lock(mutex *m)
void
mutex_lock(mutex *mutex)
{
if(atomic_add(&m->count, 1) >= 1)
acquire_sem(m->sem);
thread_id me = thread_get_current_thread_id();
if (me == mutex->holder)
panic("mutex_lock failure: mutex %p acquired twice by thread 0x%x\n", mutex, me);
acquire_sem(mutex->sem);
mutex->holder = me;
}
void mutex_unlock(mutex *m)
void
mutex_unlock(mutex *mutex)
{
if(atomic_add(&m->count, -1) > 1)
release_sem(m->sem);
thread_id me = thread_get_current_thread_id();
if (me != mutex->holder)
panic("mutex_unlock failure: thread 0x%x is trying to release mutex %p (current holder 0x%x)\n",
me, mutex, mutex->holder);
mutex->holder = -1;
release_sem(mutex->sem);
}