2004-12-14 01:22:45 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de.
|
|
|
|
* 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-09 16:24:59 +04:00
|
|
|
#ifndef _KERNEL_LOCK_H
|
|
|
|
#define _KERNEL_LOCK_H
|
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
|
2002-10-05 05:17:28 +04:00
|
|
|
#include <OS.h>
|
2002-07-18 23:22:17 +04:00
|
|
|
#include <debug.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
typedef struct recursive_lock {
|
2003-06-27 07:16:53 +04:00
|
|
|
sem_id sem;
|
|
|
|
thread_id holder;
|
|
|
|
int recursion;
|
2002-07-09 16:24:59 +04:00
|
|
|
} recursive_lock;
|
|
|
|
|
|
|
|
typedef struct mutex {
|
2003-06-27 07:16:53 +04:00
|
|
|
sem_id sem;
|
|
|
|
thread_id holder;
|
2002-07-09 16:24:59 +04:00
|
|
|
} mutex;
|
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
typedef struct benaphore {
|
2002-07-09 16:24:59 +04:00
|
|
|
sem_id sem;
|
|
|
|
int32 count;
|
2003-06-27 07:16:53 +04:00
|
|
|
} benaphore;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
// Note: this is currently a trivial r/w lock implementation
|
|
|
|
// it will be replaced with something better later - this
|
|
|
|
// or a similar API will be made publically available at this point.
|
|
|
|
typedef struct rw_lock {
|
2002-07-09 16:24:59 +04:00
|
|
|
sem_id sem;
|
|
|
|
int32 count;
|
|
|
|
benaphore writeLock;
|
2003-06-27 07:16:53 +04:00
|
|
|
} rw_lock;
|
|
|
|
|
|
|
|
#define RW_MAX_READERS 1000000
|
|
|
|
|
2004-12-14 01:22:45 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
# include <thread.h>
|
|
|
|
#endif
|
|
|
|
#define ASSERT_LOCKED_RECURSIVE(r) { ASSERT(thread_get_current_thread_id() == (r)->holder); }
|
|
|
|
#define ASSERT_LOCKED_MUTEX(m) { ASSERT(thread_get_current_thread_id() == (m)->holder); }
|
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern status_t recursive_lock_init(recursive_lock *lock, const char *name);
|
|
|
|
extern void recursive_lock_destroy(recursive_lock *lock);
|
|
|
|
extern bool recursive_lock_lock(recursive_lock *lock);
|
|
|
|
extern bool recursive_lock_unlock(recursive_lock *lock);
|
|
|
|
extern int recursive_lock_get_recursion(recursive_lock *lock);
|
|
|
|
|
|
|
|
extern status_t mutex_init(mutex *m, const char *name);
|
|
|
|
extern void mutex_destroy(mutex *m);
|
|
|
|
extern void mutex_lock(mutex *m);
|
|
|
|
extern void mutex_unlock(mutex *m);
|
|
|
|
|
|
|
|
extern status_t benaphore_init(benaphore *ben, const char *name);
|
|
|
|
extern void benaphore_destroy(benaphore *ben);
|
|
|
|
|
2003-06-27 20:39:33 +04:00
|
|
|
static inline status_t
|
|
|
|
benaphore_lock_etc(benaphore *ben, uint32 flags, bigtime_t timeout)
|
|
|
|
{
|
|
|
|
if (atomic_add(&ben->count, -1) <= 0)
|
|
|
|
return acquire_sem_etc(ben->sem, 1, flags, timeout);
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
static inline status_t
|
|
|
|
benaphore_lock(benaphore *ben)
|
|
|
|
{
|
|
|
|
if (atomic_add(&ben->count, -1) <= 0)
|
|
|
|
return acquire_sem(ben->sem);
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
2003-08-20 02:29:51 +04:00
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
static inline status_t
|
|
|
|
benaphore_unlock(benaphore *ben)
|
|
|
|
{
|
|
|
|
if (atomic_add(&ben->count, 1) < 0)
|
|
|
|
return release_sem(ben->sem);
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern status_t rw_lock_init(rw_lock *lock, const char *name);
|
|
|
|
extern void rw_lock_destroy(rw_lock *lock);
|
|
|
|
extern status_t rw_lock_read_lock(rw_lock *lock);
|
|
|
|
extern status_t rw_lock_read_unlock(rw_lock *lock);
|
|
|
|
extern status_t rw_lock_write_lock(rw_lock *lock);
|
|
|
|
extern status_t rw_lock_write_unlock(rw_lock *lock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-18 23:22:17 +04:00
|
|
|
#endif /* _KERNEL_LOCK_H */
|