2002-07-09 16:24:59 +04:00
|
|
|
/* Mutex and recursive_lock code */
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
/*
|
|
|
|
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <OS.h>
|
|
|
|
#include <lock.h>
|
2002-11-28 05:25:04 +03:00
|
|
|
#include <int.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <debug.h>
|
|
|
|
#include <arch/cpu.h>
|
|
|
|
#include <Errors.h>
|
|
|
|
#include <atomic.h>
|
|
|
|
#include <thread.h>
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
recursive_lock_get_recursion(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
thread_id thid = thread_get_current_thread_id();
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (lock->holder == thid)
|
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
|
|
|
|
|
|
|
int
|
|
|
|
recursive_lock_create(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (lock == NULL)
|
2002-07-12 02:21:56 +04:00
|
|
|
return EINVAL;
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
lock->holder = -1;
|
|
|
|
lock->recursion = 0;
|
|
|
|
lock->sem = create_sem(1, "recursive_lock_sem");
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (lock->sem > 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
delete_sem(lock->sem);
|
|
|
|
lock->sem = -1;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
recursive_lock_lock(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
thread_id thid = thread_get_current_thread_id();
|
|
|
|
bool retval = false;
|
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);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (thid != lock->holder) {
|
2002-07-09 16:24:59 +04:00
|
|
|
acquire_sem(lock->sem);
|
|
|
|
|
|
|
|
lock->holder = thid;
|
|
|
|
retval = true;
|
|
|
|
}
|
|
|
|
lock->recursion++;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
recursive_lock_unlock(recursive_lock *lock)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
thread_id thid = thread_get_current_thread_id();
|
|
|
|
bool retval = false;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (thid != lock->holder)
|
2002-07-09 16:24:59 +04:00
|
|
|
panic("recursive_lock %p unlocked by non-holder thread!\n", lock);
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (--lock->recursion == 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
lock->holder = -1;
|
|
|
|
release_sem(lock->sem);
|
|
|
|
retval = true;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
mutex_init(mutex *m, const char *in_name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (in_name == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
name = "mutex_sem";
|
|
|
|
else
|
|
|
|
name = in_name;
|
|
|
|
|
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);
|
|
|
|
if (m->sem < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return m->sem;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
void
|
|
|
|
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();
|
|
|
|
|
2002-11-28 05:25:04 +03:00
|
|
|
if (!kernel_startup && !are_interrupts_enabled())
|
|
|
|
panic("mutex_lock: called with interrupts disabled for mutex %p, sem %#lx\n", mutex, mutex->sem);
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (me == mutex->holder)
|
2002-09-23 06:41:52 +04:00
|
|
|
panic("mutex_lock failure: mutex %p acquired twice by thread 0x%lx\n", mutex, me);
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
acquire_sem(mutex->sem);
|
|
|
|
mutex->holder = me;
|
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();
|
|
|
|
|
|
|
|
if (me != mutex->holder)
|
2002-09-23 06:41:52 +04:00
|
|
|
panic("mutex_unlock failure: thread 0x%lx is trying to release mutex %p (current holder 0x%lx)\n",
|
2002-07-18 23:21:40 +04:00
|
|
|
me, mutex, mutex->holder);
|
|
|
|
|
|
|
|
mutex->holder = -1;
|
|
|
|
release_sem(mutex->sem);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|