* Introduced a new locking primitive I called "cutex" (sorry for the

name, couldn't resist :-P). It's semantically equivalent to a mutex,
  but doesn't need a semaphore (it uses thread blocking and a simple
  queue instead). Initialization can't fail. In fact it is ready to use
  without initialization when living in the bss segment, also in the
  early boot process. It's as fast as a benaphore in cases of low lock
  contention, and faster otherwise.  Only disadvantage is the higher
  immediate memory footprint of 16 bytes.
* Changed how the "thread" and "threads" debugger commands list the
  objects they are waiting for. Cutexes are also included.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25276 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2008-05-01 01:53:07 +00:00
parent 4e6b38af6c
commit 8562499f44
7 changed files with 347 additions and 29 deletions
+64
View File
@@ -39,13 +39,29 @@ typedef struct rw_lock {
#define RW_MAX_READERS 1000000
struct cutex_waiter;
typedef struct cutex {
const char* name;
struct cutex_waiter* waiters;
#ifdef KDEBUG
thread_id holder;
#else
int32 count;
#endif
int32 release_count;
} cutex;
#if 0 && KDEBUG // XXX disable this for now, it causes problems when including thread.h here
# 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); }
#define ASSERT_LOCKED_CUTEX(m) { ASSERT(thread_get_current_thread_id() == (m)->holder); }
#else
#define ASSERT_LOCKED_RECURSIVE(r)
#define ASSERT_LOCKED_MUTEX(m)
#define ASSERT_LOCKED_CUTEX(m)
#endif
@@ -103,6 +119,54 @@ 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);
extern void cutex_init(cutex* lock, const char *name);
// name is *not* cloned nor freed in cutex_destroy()
extern void cutex_destroy(cutex* lock);
// implementation private:
extern void _cutex_lock(cutex* lock);
extern void _cutex_unlock(cutex* lock);
extern status_t _cutex_trylock(cutex* lock);
static inline void
cutex_lock(cutex* lock)
{
#ifdef KDEBUG
_cutex_lock(lock);
#else
if (atomic_add(&lock->count, -1) < 0)
_cutex_lock(lock);
#endif
}
static inline status_t
cutex_trylock(cutex* lock)
{
#ifdef KDEBUG
return _cutex_trylock(lock);
#else
if (atomic_test_and_set(&lock->count, -1, 0) != 0)
return B_WOULD_BLOCK;
#endif
}
static inline void
cutex_unlock(cutex* lock)
{
#ifdef KDEBUG
_cutex_unlock(lock);
#else
if (atomic_add(&lock->count, 1) < -1)
_cutex_unlock(lock);
#endif
}
extern void lock_debug_init();
#ifdef __cplusplus
}
#endif
+1
View File
@@ -62,6 +62,7 @@ enum {
THREAD_BLOCK_TYPE_CONDITION_VARIABLE = 1,
THREAD_BLOCK_TYPE_SNOOZE = 2,
THREAD_BLOCK_TYPE_SIGNAL = 3,
THREAD_BLOCK_TYPE_CUTEX = 4,
THREAD_BLOCK_TYPE_OTHER = 9999,
THREAD_BLOCK_TYPE_USER_BASE = 10000
+19
View File
@@ -67,6 +67,24 @@ public:
// BenaphoreLocker
typedef AutoLocker<benaphore, BenaphoreLocking> BenaphoreLocker;
// CutexLocking
class CutexLocking {
public:
inline bool Lock(cutex *lockable)
{
cutex_lock(lockable);
return true;
}
inline void Unlock(cutex *lockable)
{
cutex_unlock(lockable);
}
};
// CutexLocker
typedef AutoLocker<cutex, CutexLocking> CutexLocker;
// InterruptsLocking
class InterruptsLocking {
public:
@@ -153,6 +171,7 @@ using BPrivate::AutoLocker;
using BPrivate::MutexLocker;
using BPrivate::RecursiveLocker;
using BPrivate::BenaphoreLocker;
using BPrivate::CutexLocker;
using BPrivate::InterruptsLocker;
using BPrivate::SpinLocker;
using BPrivate::InterruptsSpinLocker;