2005-04-12 10:09:13 +04:00
|
|
|
/*
|
2008-02-02 02:05:26 +03:00
|
|
|
* Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
2005-04-12 10:09:13 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2005-04-12 10:09:13 +04:00
|
|
|
/* Mutex and recursive_lock code */
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <OS.h>
|
2005-04-12 10:09:13 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <lock.h>
|
2005-04-12 10:09:13 +04:00
|
|
|
#include <kernel.h>
|
2002-11-28 05:25:04 +03:00
|
|
|
#include <int.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <debug.h>
|
|
|
|
#include <thread.h>
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
int32
|
2002-07-18 23:21:40 +04:00
|
|
|
recursive_lock_get_recursion(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2007-02-07 17:07:31 +03:00
|
|
|
if (lock->holder == thread_get_current_thread_id())
|
2002-07-09 16:24:59 +04:00
|
|
|
return lock->recursion;
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
return -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
status_t
|
|
|
|
recursive_lock_init(recursive_lock *lock, const char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (lock == NULL)
|
2003-06-27 07:22:57 +04:00
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
name = "recursive lock";
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
lock->holder = -1;
|
|
|
|
lock->recursion = 0;
|
2003-06-27 07:22:57 +04:00
|
|
|
lock->sem = create_sem(1, name);
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-11-01 03:52:27 +03:00
|
|
|
if (lock->sem >= B_OK)
|
2003-06-27 07:22:57 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
return lock->sem;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
recursive_lock_destroy(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (lock == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
return;
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
delete_sem(lock->sem);
|
2002-07-09 16:24:59 +04:00
|
|
|
lock->sem = -1;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
status_t
|
2002-07-18 23:21:40 +04:00
|
|
|
recursive_lock_lock(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2007-02-07 17:07:31 +03:00
|
|
|
thread_id thread = thread_get_current_thread_id();
|
2002-11-28 05:25:04 +03:00
|
|
|
|
|
|
|
if (!kernel_startup && !are_interrupts_enabled())
|
|
|
|
panic("recursive_lock_lock: called with interrupts disabled for lock %p, sem %#lx\n", lock, lock->sem);
|
2003-06-27 07:22:57 +04:00
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
if (thread != lock->holder) {
|
|
|
|
status_t status = acquire_sem(lock->sem);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
lock->holder = thread;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
lock->recursion++;
|
2007-02-07 17:07:31 +03:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
void
|
2002-07-18 23:21:40 +04:00
|
|
|
recursive_lock_unlock(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2007-02-07 17:07:31 +03:00
|
|
|
if (thread_get_current_thread_id() != lock->holder)
|
2002-07-09 16:24:59 +04:00
|
|
|
panic("recursive_lock %p unlocked by non-holder thread!\n", lock);
|
2003-06-27 07:22:57 +04:00
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (--lock->recursion == 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
lock->holder = -1;
|
2007-09-25 20:41:52 +04:00
|
|
|
release_sem_etc(lock->sem, 1, 0/*B_DO_NOT_RESCHEDULE*/);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
// #pragma mark -
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
mutex_init(mutex *m, const char *name)
|
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (m == NULL)
|
2002-07-12 02:21:56 +04:00
|
|
|
return EINVAL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
if (name == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
name = "mutex_sem";
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
m->holder = -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
m->sem = create_sem(1, name);
|
2004-11-01 03:52:27 +03:00
|
|
|
if (m->sem >= B_OK)
|
2003-06-27 07:22:57 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
return m->sem;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
mutex_destroy(mutex *mutex)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (mutex == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
return;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (mutex->sem >= 0) {
|
|
|
|
delete_sem(mutex->sem);
|
|
|
|
mutex->sem = -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
mutex->holder = -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2007-09-25 20:41:52 +04:00
|
|
|
status_t
|
|
|
|
mutex_trylock(mutex *mutex)
|
|
|
|
{
|
|
|
|
thread_id me = thread_get_current_thread_id();
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
if (kernel_startup)
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
status = acquire_sem_etc(mutex->sem, 1, B_RELATIVE_TIMEOUT, 0);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (me == mutex->holder) {
|
|
|
|
panic("mutex_trylock failure: mutex %p (sem = 0x%lx) acquired twice by"
|
|
|
|
" thread 0x%lx\n", mutex, mutex->sem, me);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex->holder = me;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
status_t
|
2002-07-18 23:21:40 +04:00
|
|
|
mutex_lock(mutex *mutex)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
thread_id me = thread_get_current_thread_id();
|
2007-02-07 17:07:31 +03:00
|
|
|
status_t status;
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
if (kernel_startup)
|
|
|
|
return B_OK;
|
2002-11-28 05:25:04 +03:00
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
status = acquire_sem(mutex->sem);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
2007-07-18 03:40:41 +04:00
|
|
|
if (me == mutex->holder) {
|
|
|
|
panic("mutex_lock failure: mutex %p (sem = 0x%lx) acquired twice by"
|
|
|
|
" thread 0x%lx\n", mutex, mutex->sem, me);
|
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
mutex->holder = me;
|
2007-02-07 17:07:31 +03:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
mutex_unlock(mutex *mutex)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
thread_id me = thread_get_current_thread_id();
|
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
if (kernel_startup)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (me != mutex->holder) {
|
2007-07-18 03:40:41 +04:00
|
|
|
panic("mutex_unlock failure: thread 0x%lx is trying to release mutex %p"
|
|
|
|
" (current holder 0x%lx)\n", me, mutex, mutex->holder);
|
2007-02-07 17:07:31 +03:00
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
mutex->holder = -1;
|
2007-09-25 20:41:52 +04:00
|
|
|
release_sem_etc(mutex->sem, 1, 0/*B_DO_NOT_RESCHEDULE*/);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2003-06-27 07:22:57 +04:00
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
benaphore_init(benaphore *ben, const char *name)
|
|
|
|
{
|
|
|
|
if (ben == NULL || name == NULL)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
ben->count = 1;
|
2008-02-02 02:05:26 +03:00
|
|
|
#ifdef KDEBUG
|
|
|
|
ben->sem = create_sem(1, name);
|
|
|
|
#else
|
2003-06-27 07:22:57 +04:00
|
|
|
ben->sem = create_sem(0, name);
|
2008-02-02 02:05:26 +03:00
|
|
|
#endif
|
2004-11-01 03:52:27 +03:00
|
|
|
if (ben->sem >= B_OK)
|
2003-06-27 07:22:57 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
return ben->sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
benaphore_destroy(benaphore *ben)
|
|
|
|
{
|
|
|
|
delete_sem(ben->sem);
|
|
|
|
ben->sem = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
rw_lock_init(rw_lock *lock, const char *name)
|
|
|
|
{
|
|
|
|
if (lock == NULL)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
name = "r/w lock";
|
|
|
|
|
|
|
|
lock->sem = create_sem(RW_MAX_READERS, name);
|
2004-11-01 03:52:27 +03:00
|
|
|
if (lock->sem >= B_OK)
|
2003-06-27 07:22:57 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
return lock->sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
rw_lock_destroy(rw_lock *lock)
|
|
|
|
{
|
|
|
|
if (lock == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
delete_sem(lock->sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
rw_lock_read_lock(rw_lock *lock)
|
|
|
|
{
|
|
|
|
return acquire_sem(lock->sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
rw_lock_read_unlock(rw_lock *lock)
|
|
|
|
{
|
2007-09-25 20:41:52 +04:00
|
|
|
return release_sem_etc(lock->sem, 1, 0/*B_DO_NOT_RESCHEDULE*/);
|
2003-06-27 07:22:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
rw_lock_write_lock(rw_lock *lock)
|
|
|
|
{
|
|
|
|
return acquire_sem_etc(lock->sem, RW_MAX_READERS, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
rw_lock_write_unlock(rw_lock *lock)
|
|
|
|
{
|
2003-09-13 00:44:45 +04:00
|
|
|
return release_sem_etc(lock->sem, RW_MAX_READERS, 0);
|
2003-06-27 07:22:57 +04:00
|
|
|
}
|
|
|
|
|