2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de.
4 * Distributed under the terms of the MIT License.
6 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
7 * Distributed under the terms of the NewOS License.
9 #ifndef _FSSH_KERNEL_LOCK_H
10 #define _FSSH_KERNEL_LOCK_H
13 #include <fssh_auto_locker.h>
14 #include <fssh_errors.h>
18 typedef struct fssh_mutex
{
20 fssh_thread_id holder
;
23 #define FSSH_MUTEX_FLAG_CLONE_NAME 0x1
26 typedef struct fssh_recursive_lock
{
28 fssh_thread_id holder
;
30 } fssh_recursive_lock
;
33 typedef struct fssh_rw_lock
{
35 fssh_thread_id holder
;
39 #define FSSH_RW_LOCK_FLAG_CLONE_NAME 0x1
41 #define FSSH_ASSERT_LOCKED_RECURSIVE(r)
42 #define FSSH_ASSERT_LOCKED_MUTEX(m)
43 #define FSSH_ASSERT_WRITE_LOCKED_RW_LOCK(l)
44 #define FSSH_ASSERT_READ_LOCKED_RW_LOCK(l)
46 // static initializers
47 #define FSSH_MUTEX_INITIALIZER(name) { name, NULL, 0, 0 }
48 #define FSSH_RECURSIVE_LOCK_INITIALIZER(name) { FSSH_MUTEX_INITIALIZER(name), -1, 0 }
49 #define FSSH_RW_LOCK_INITIALIZER(name) { name, NULL, -1, 0, 0, 0 }
55 extern void fssh_recursive_lock_init(fssh_recursive_lock
*lock
, const char *name
);
56 // name is *not* cloned nor freed in recursive_lock_destroy()
57 extern void fssh_recursive_lock_init_etc(fssh_recursive_lock
*lock
, const char *name
,
59 extern void fssh_recursive_lock_destroy(fssh_recursive_lock
*lock
);
60 extern fssh_status_t
fssh_recursive_lock_lock(fssh_recursive_lock
*lock
);
61 extern fssh_status_t
fssh_recursive_lock_trylock(fssh_recursive_lock
*lock
);
62 extern void fssh_recursive_lock_unlock(fssh_recursive_lock
*lock
);
63 extern int32_t fssh_recursive_lock_get_recursion(fssh_recursive_lock
*lock
);
64 extern void fssh_recursive_lock_transfer_lock(fssh_recursive_lock
*lock
, fssh_thread_id thread
);
66 extern void fssh_rw_lock_init(fssh_rw_lock
* lock
, const char* name
);
67 // name is *not* cloned nor freed in rw_lock_destroy()
68 extern void fssh_rw_lock_init_etc(fssh_rw_lock
* lock
, const char* name
, uint32_t flags
);
69 extern void fssh_rw_lock_destroy(fssh_rw_lock
* lock
);
70 extern fssh_status_t
fssh_rw_lock_read_lock(fssh_rw_lock
* lock
);
71 extern fssh_status_t
fssh_rw_lock_read_unlock(fssh_rw_lock
* lock
);
72 extern fssh_status_t
fssh_rw_lock_write_lock(fssh_rw_lock
* lock
);
73 extern fssh_status_t
fssh_rw_lock_write_unlock(fssh_rw_lock
* lock
);
75 extern void fssh_mutex_init(fssh_mutex
* lock
, const char* name
);
76 // name is *not* cloned nor freed in mutex_destroy()
77 extern void fssh_mutex_init_etc(fssh_mutex
* lock
, const char* name
, uint32_t flags
);
78 extern void fssh_mutex_destroy(fssh_mutex
* lock
);
79 extern fssh_status_t
fssh_mutex_lock(fssh_mutex
* lock
);
80 extern fssh_status_t
fssh_mutex_trylock(fssh_mutex
* lock
);
81 extern void fssh_mutex_unlock(fssh_mutex
* lock
);
82 extern void fssh_mutex_transfer_lock(fssh_mutex
* lock
, fssh_thread_id thread
);
92 inline bool Lock(fssh_mutex
*lockable
)
94 return fssh_mutex_lock(lockable
) == FSSH_B_OK
;
97 inline void Unlock(fssh_mutex
*lockable
)
99 fssh_mutex_unlock(lockable
);
104 typedef AutoLocker
<fssh_mutex
, MutexLocking
> MutexLocker
;
106 // RecursiveLockLocking
107 class RecursiveLockLocking
{
109 inline bool Lock(fssh_recursive_lock
*lockable
)
111 return fssh_recursive_lock_lock(lockable
) == FSSH_B_OK
;
114 inline void Unlock(fssh_recursive_lock
*lockable
)
116 fssh_recursive_lock_unlock(lockable
);
121 typedef AutoLocker
<fssh_recursive_lock
, RecursiveLockLocking
> RecursiveLocker
;
123 class ReadWriteLockReadLocking
{
125 inline bool Lock(fssh_rw_lock
*lockable
)
127 return fssh_rw_lock_read_lock(lockable
) == FSSH_B_OK
;
130 inline void Unlock(fssh_rw_lock
*lockable
)
132 fssh_rw_lock_read_unlock(lockable
);
136 class ReadWriteLockWriteLocking
{
138 inline bool Lock(fssh_rw_lock
*lockable
)
140 return fssh_rw_lock_write_lock(lockable
) == FSSH_B_OK
;
143 inline void Unlock(fssh_rw_lock
*lockable
)
145 fssh_rw_lock_write_unlock(lockable
);
149 typedef AutoLocker
<fssh_rw_lock
, ReadWriteLockReadLocking
> ReadLocker
;
150 typedef AutoLocker
<fssh_rw_lock
, ReadWriteLockWriteLocking
> WriteLocker
;
152 } // namespace FSShell
154 using FSShell::AutoLocker
;
155 using FSShell::MutexLocker
;
156 using FSShell::RecursiveLocker
;
157 using FSShell::ReadLocker
;
158 using FSShell::WriteLocker
;
160 #endif // __cplusplus
162 #endif /* _FSSH_KERNEL_LOCK_H */