haiku/headers/private/fs_shell/fssh_lock.h
Axel Dörfler 78b32dd026 * The rw_lock is now able to nest read/write locks when you have a write lock.
It follows the semantics of the BFS R/W lock, though, that is, if you unlock
  your write lock before the read locks, the read locks effectively become
  write locks, too.
* Added a mutex_transfer_lock() function that will allow you to unlock a mutex
  in a different thread than the one which locked it (only matters if KDEBUG
  is enabled, though).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26316 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-07-08 07:56:49 +00:00

160 lines
4.3 KiB
C++

/*
* Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2002-2008, 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.
*/
#ifndef _FSSH_KERNEL_LOCK_H
#define _FSSH_KERNEL_LOCK_H
#include <fssh_auto_locker.h>
#include <fssh_errors.h>
#include <fssh_os.h>
typedef struct fssh_mutex {
fssh_sem_id sem;
fssh_thread_id holder;
} fssh_mutex;
#define FSSH_MUTEX_FLAG_CLONE_NAME 0x1
typedef struct fssh_recursive_lock {
fssh_sem_id sem;
fssh_thread_id holder;
int recursion;
} fssh_recursive_lock;
typedef struct fssh_rw_lock {
fssh_sem_id sem;
fssh_thread_id holder;
int32_t count;
} fssh_rw_lock;
#define FSSH_RW_LOCK_FLAG_CLONE_NAME 0x1
#define FSSH_ASSERT_LOCKED_RECURSIVE(r)
#define FSSH_ASSERT_LOCKED_MUTEX(m)
// static initializers
#define FSSH_MUTEX_INITIALIZER(name) { name, NULL, 0, 0 }
#define FSSH_RECURSIVE_LOCK_INITIALIZER(name) { FSSH_MUTEX_INITIALIZER(name), -1, 0 }
#define FSSH_RW_LOCK_INITIALIZER(name) { name, NULL, -1, 0, 0, 0 }
#ifdef __cplusplus
extern "C" {
#endif
extern void fssh_recursive_lock_init(fssh_recursive_lock *lock, const char *name);
// name is *not* cloned nor freed in recursive_lock_destroy()
extern void fssh_recursive_lock_init_etc(fssh_recursive_lock *lock, const char *name,
uint32_t flags);
extern void fssh_recursive_lock_destroy(fssh_recursive_lock *lock);
extern fssh_status_t fssh_recursive_lock_lock(fssh_recursive_lock *lock);
extern fssh_status_t fssh_recursive_lock_trylock(fssh_recursive_lock *lock);
extern void fssh_recursive_lock_unlock(fssh_recursive_lock *lock);
extern int32_t fssh_recursive_lock_get_recursion(fssh_recursive_lock *lock);
extern void fssh_rw_lock_init(fssh_rw_lock* lock, const char* name);
// name is *not* cloned nor freed in rw_lock_destroy()
extern void fssh_rw_lock_init_etc(fssh_rw_lock* lock, const char* name, uint32_t flags);
extern void fssh_rw_lock_destroy(fssh_rw_lock* lock);
extern fssh_status_t fssh_rw_lock_read_lock(fssh_rw_lock* lock);
extern fssh_status_t fssh_rw_lock_read_unlock(fssh_rw_lock* lock);
extern fssh_status_t fssh_rw_lock_write_lock(fssh_rw_lock* lock);
extern fssh_status_t fssh_rw_lock_write_unlock(fssh_rw_lock* lock);
extern void fssh_mutex_init(fssh_mutex* lock, const char* name);
// name is *not* cloned nor freed in mutex_destroy()
extern void fssh_mutex_init_etc(fssh_mutex* lock, const char* name, uint32_t flags);
extern void fssh_mutex_destroy(fssh_mutex* lock);
extern fssh_status_t fssh_mutex_lock(fssh_mutex* lock);
extern fssh_status_t fssh_mutex_trylock(fssh_mutex* lock);
extern void fssh_mutex_unlock(fssh_mutex* lock);
extern void fssh_mutex_transfer_lock(fssh_mutex* lock, fssh_thread_id thread);
#ifdef __cplusplus
}
namespace FSShell {
// MutexLocking
class MutexLocking {
public:
inline bool Lock(fssh_mutex *lockable)
{
return fssh_mutex_lock(lockable) == FSSH_B_OK;
}
inline void Unlock(fssh_mutex *lockable)
{
fssh_mutex_unlock(lockable);
}
};
// MutexLocker
typedef AutoLocker<fssh_mutex, MutexLocking> MutexLocker;
// RecursiveLockLocking
class RecursiveLockLocking {
public:
inline bool Lock(fssh_recursive_lock *lockable)
{
return fssh_recursive_lock_lock(lockable) == FSSH_B_OK;
}
inline void Unlock(fssh_recursive_lock *lockable)
{
fssh_recursive_lock_unlock(lockable);
}
};
// RecursiveLocker
typedef AutoLocker<fssh_recursive_lock, RecursiveLockLocking> RecursiveLocker;
class ReadWriteLockReadLocking {
public:
inline bool Lock(fssh_rw_lock *lockable)
{
return fssh_rw_lock_read_lock(lockable) == FSSH_B_OK;
}
inline void Unlock(fssh_rw_lock *lockable)
{
fssh_rw_lock_read_unlock(lockable);
}
};
class ReadWriteLockWriteLocking {
public:
inline bool Lock(fssh_rw_lock *lockable)
{
return fssh_rw_lock_write_lock(lockable) == FSSH_B_OK;
}
inline void Unlock(fssh_rw_lock *lockable)
{
fssh_rw_lock_write_unlock(lockable);
}
};
typedef AutoLocker<fssh_rw_lock, ReadWriteLockReadLocking> ReadLocker;
typedef AutoLocker<fssh_rw_lock, ReadWriteLockWriteLocking> WriteLocker;
} // namespace FSShell
using FSShell::AutoLocker;
using FSShell::MutexLocker;
using FSShell::RecursiveLocker;
using FSShell::ReadLocker;
using FSShell::WriteLocker;
#endif // __cplusplus
#endif /* _FSSH_KERNEL_LOCK_H */