2004-12-14 01:22:45 +03:00
|
|
|
/*
|
2008-05-02 02:07:36 +04:00
|
|
|
* Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
|
2008-02-02 02:05:26 +03:00
|
|
|
* Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de.
|
2004-12-14 01:22:45 +03: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-09 16:24:59 +04:00
|
|
|
#ifndef _KERNEL_LOCK_H
|
|
|
|
#define _KERNEL_LOCK_H
|
|
|
|
|
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
|
|
|
|
2008-05-29 03:12:36 +04:00
|
|
|
|
|
|
|
struct mutex_waiter;
|
|
|
|
|
|
|
|
typedef struct mutex {
|
|
|
|
const char* name;
|
|
|
|
struct mutex_waiter* waiters;
|
|
|
|
#ifdef KDEBUG
|
|
|
|
thread_id holder;
|
|
|
|
#else
|
|
|
|
int32 count;
|
|
|
|
#endif
|
|
|
|
uint8 flags;
|
|
|
|
} mutex;
|
|
|
|
|
|
|
|
#define MUTEX_FLAG_CLONE_NAME 0x1
|
|
|
|
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
typedef struct recursive_lock {
|
2008-05-29 03:12:36 +04:00
|
|
|
mutex lock;
|
|
|
|
#ifndef KDEBUG
|
2003-06-27 07:16:53 +04:00
|
|
|
thread_id holder;
|
2008-05-29 03:12:36 +04:00
|
|
|
#endif
|
2003-06-27 07:16:53 +04:00
|
|
|
int recursion;
|
2002-07-09 16:24:59 +04:00
|
|
|
} recursive_lock;
|
|
|
|
|
2008-05-29 03:12:36 +04:00
|
|
|
|
2008-05-29 04:32:06 +04:00
|
|
|
struct rw_lock_waiter;
|
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
typedef struct rw_lock {
|
2008-05-29 04:32:06 +04:00
|
|
|
const char* name;
|
|
|
|
struct rw_lock_waiter* waiters;
|
|
|
|
thread_id holder;
|
|
|
|
int32 reader_count;
|
|
|
|
int32 writer_count;
|
2008-07-08 11:56:49 +04:00
|
|
|
int32 owner_count;
|
2008-05-29 04:32:06 +04:00
|
|
|
uint32 flags;
|
2003-06-27 07:16:53 +04:00
|
|
|
} rw_lock;
|
|
|
|
|
2008-05-29 04:32:06 +04:00
|
|
|
#define RW_LOCK_FLAG_CLONE_NAME 0x1
|
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
|
2007-06-21 09:37:46 +04:00
|
|
|
#if 0 && KDEBUG // XXX disable this for now, it causes problems when including thread.h here
|
2004-12-14 01:22:45 +03:00
|
|
|
# include <thread.h>
|
|
|
|
#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); }
|
2007-06-21 09:37:46 +04:00
|
|
|
#else
|
|
|
|
#define ASSERT_LOCKED_RECURSIVE(r)
|
|
|
|
#define ASSERT_LOCKED_MUTEX(m)
|
|
|
|
#endif
|
2004-12-14 01:22:45 +03:00
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
|
2008-05-29 06:10:10 +04:00
|
|
|
// static initializers
|
|
|
|
#ifdef KDEBUG
|
|
|
|
# define MUTEX_INITIALIZER(name) { name, NULL, -1, 0 }
|
|
|
|
# define RECURSIVE_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), 0 }
|
|
|
|
#else
|
|
|
|
# define MUTEX_INITIALIZER(name) { name, NULL, 0, 0 }
|
|
|
|
# define RECURSIVE_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), -1, 0 }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define RW_LOCK_INITIALIZER(name) { name, NULL, -1, 0, 0, 0 }
|
|
|
|
|
|
|
|
|
2003-06-27 07:16:53 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2008-05-29 03:12:36 +04:00
|
|
|
extern void recursive_lock_init(recursive_lock *lock, const char *name);
|
|
|
|
// name is *not* cloned nor freed in recursive_lock_destroy()
|
|
|
|
extern void recursive_lock_init_etc(recursive_lock *lock, const char *name,
|
|
|
|
uint32 flags);
|
2003-06-27 07:16:53 +04:00
|
|
|
extern void recursive_lock_destroy(recursive_lock *lock);
|
2007-02-07 17:07:31 +03:00
|
|
|
extern status_t recursive_lock_lock(recursive_lock *lock);
|
2008-07-07 20:17:34 +04:00
|
|
|
extern status_t recursive_lock_trylock(recursive_lock *lock);
|
2007-02-07 17:07:31 +03:00
|
|
|
extern void recursive_lock_unlock(recursive_lock *lock);
|
|
|
|
extern int32 recursive_lock_get_recursion(recursive_lock *lock);
|
2003-06-27 07:16:53 +04:00
|
|
|
|
2008-05-29 04:32:06 +04:00
|
|
|
extern void rw_lock_init(rw_lock* lock, const char* name);
|
|
|
|
// name is *not* cloned nor freed in rw_lock_destroy()
|
|
|
|
extern void rw_lock_init_etc(rw_lock* lock, const char* name, uint32 flags);
|
|
|
|
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
|
|
|
|
2008-05-29 04:32:06 +04:00
|
|
|
extern void mutex_init(mutex* lock, const char* name);
|
2008-05-02 02:07:36 +04:00
|
|
|
// name is *not* cloned nor freed in mutex_destroy()
|
2008-05-29 04:32:06 +04:00
|
|
|
extern void mutex_init_etc(mutex* lock, const char* name, uint32 flags);
|
2008-05-02 02:07:36 +04:00
|
|
|
extern void mutex_destroy(mutex* lock);
|
2008-05-01 05:53:07 +04:00
|
|
|
|
|
|
|
// implementation private:
|
2008-05-02 02:07:36 +04:00
|
|
|
extern status_t _mutex_lock(mutex* lock, bool threadsLocked);
|
|
|
|
extern void _mutex_unlock(mutex* lock);
|
|
|
|
extern status_t _mutex_trylock(mutex* lock);
|
|
|
|
|
|
|
|
|
|
|
|
static inline status_t
|
|
|
|
mutex_lock(mutex* lock)
|
|
|
|
{
|
|
|
|
#ifdef KDEBUG
|
|
|
|
return _mutex_lock(lock, false);
|
|
|
|
#else
|
|
|
|
if (atomic_add(&lock->count, -1) < 0)
|
|
|
|
return _mutex_lock(lock, false);
|
|
|
|
return B_OK;
|
|
|
|
#endif
|
|
|
|
}
|
2008-05-01 05:53:07 +04:00
|
|
|
|
|
|
|
|
2008-05-01 22:06:09 +04:00
|
|
|
static inline status_t
|
2008-05-02 02:07:36 +04:00
|
|
|
mutex_lock_threads_locked(mutex* lock)
|
2008-05-01 05:53:07 +04:00
|
|
|
{
|
|
|
|
#ifdef KDEBUG
|
2008-05-02 02:07:36 +04:00
|
|
|
return _mutex_lock(lock, true);
|
2008-05-01 05:53:07 +04:00
|
|
|
#else
|
|
|
|
if (atomic_add(&lock->count, -1) < 0)
|
2008-05-02 02:07:36 +04:00
|
|
|
return _mutex_lock(lock, true);
|
2008-05-01 22:06:09 +04:00
|
|
|
return B_OK;
|
2008-05-01 05:53:07 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline status_t
|
2008-05-02 02:07:36 +04:00
|
|
|
mutex_trylock(mutex* lock)
|
2008-05-01 05:53:07 +04:00
|
|
|
{
|
|
|
|
#ifdef KDEBUG
|
2008-05-02 02:07:36 +04:00
|
|
|
return _mutex_trylock(lock);
|
2008-05-01 05:53:07 +04:00
|
|
|
#else
|
|
|
|
if (atomic_test_and_set(&lock->count, -1, 0) != 0)
|
|
|
|
return B_WOULD_BLOCK;
|
2008-05-01 22:06:09 +04:00
|
|
|
return B_OK;
|
2008-05-01 05:53:07 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2008-05-02 02:07:36 +04:00
|
|
|
mutex_unlock(mutex* lock)
|
2008-05-01 05:53:07 +04:00
|
|
|
{
|
|
|
|
#ifdef KDEBUG
|
2008-05-02 02:07:36 +04:00
|
|
|
_mutex_unlock(lock);
|
2008-05-01 05:53:07 +04:00
|
|
|
#else
|
|
|
|
if (atomic_add(&lock->count, 1) < -1)
|
2008-05-02 02:07:36 +04:00
|
|
|
_mutex_unlock(lock);
|
2008-05-01 05:53:07 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-08 11:56:49 +04:00
|
|
|
static inline void
|
|
|
|
mutex_transfer_lock(mutex* lock, thread_id thread)
|
|
|
|
{
|
|
|
|
#ifdef KDEBUG
|
|
|
|
lock->holder = thread;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-01 05:53:07 +04:00
|
|
|
extern void lock_debug_init();
|
|
|
|
|
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 */
|