xref: /haiku/headers/private/fs_shell/fssh_lock.h (revision 589f1a9133081a871738156ebc2c95e757581aac)
1 /*
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.
5  *
6  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
7  * Distributed under the terms of the NewOS License.
8  */
9 #ifndef _FSSH_KERNEL_LOCK_H
10 #define _FSSH_KERNEL_LOCK_H
11 
12 
13 #include <fssh_auto_locker.h>
14 #include <fssh_errors.h>
15 #include <fssh_os.h>
16 
17 
18 typedef struct fssh_mutex {
19 	fssh_sem_id		sem;
20 	fssh_thread_id	holder;
21 } fssh_mutex;
22 
23 #define FSSH_MUTEX_FLAG_CLONE_NAME	0x1
24 
25 
26 typedef struct fssh_recursive_lock {
27 	fssh_sem_id		sem;
28 	fssh_thread_id	holder;
29 	int				recursion;
30 } fssh_recursive_lock;
31 
32 
33 typedef struct fssh_rw_lock {
34 	fssh_sem_id		sem;
35 	int32_t			count;
36 } fssh_rw_lock;
37 
38 #define FSSH_RW_LOCK_FLAG_CLONE_NAME	0x1
39 
40 #define FSSH_ASSERT_LOCKED_RECURSIVE(r)
41 #define FSSH_ASSERT_LOCKED_MUTEX(m)
42 
43 // static initializers
44 #define FSSH_MUTEX_INITIALIZER(name)			{ name, NULL, 0, 0 }
45 #define FSSH_RECURSIVE_LOCK_INITIALIZER(name)	{ FSSH_MUTEX_INITIALIZER(name), -1, 0 }
46 #define FSSH_RW_LOCK_INITIALIZER(name)			{ name, NULL, -1, 0, 0, 0 }
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 extern void	fssh_recursive_lock_init(fssh_recursive_lock *lock, const char *name);
53 	// name is *not* cloned nor freed in recursive_lock_destroy()
54 extern void fssh_recursive_lock_init_etc(fssh_recursive_lock *lock, const char *name,
55 	uint32_t flags);
56 extern void fssh_recursive_lock_destroy(fssh_recursive_lock *lock);
57 extern fssh_status_t fssh_recursive_lock_lock(fssh_recursive_lock *lock);
58 extern void fssh_recursive_lock_unlock(fssh_recursive_lock *lock);
59 extern int32_t fssh_recursive_lock_get_recursion(fssh_recursive_lock *lock);
60 
61 extern void fssh_rw_lock_init(fssh_rw_lock* lock, const char* name);
62 	// name is *not* cloned nor freed in rw_lock_destroy()
63 extern void fssh_rw_lock_init_etc(fssh_rw_lock* lock, const char* name, uint32_t flags);
64 extern void fssh_rw_lock_destroy(fssh_rw_lock* lock);
65 extern fssh_status_t fssh_rw_lock_read_lock(fssh_rw_lock* lock);
66 extern fssh_status_t fssh_rw_lock_read_unlock(fssh_rw_lock* lock);
67 extern fssh_status_t fssh_rw_lock_write_lock(fssh_rw_lock* lock);
68 extern fssh_status_t fssh_rw_lock_write_unlock(fssh_rw_lock* lock);
69 
70 extern void fssh_mutex_init(fssh_mutex* lock, const char* name);
71 	// name is *not* cloned nor freed in mutex_destroy()
72 extern void fssh_mutex_init_etc(fssh_mutex* lock, const char* name, uint32_t flags);
73 extern void fssh_mutex_destroy(fssh_mutex* lock);
74 extern fssh_status_t fssh_mutex_lock(fssh_mutex* lock);
75 extern fssh_status_t fssh_mutex_trylock(fssh_mutex* lock);
76 extern void fssh_mutex_unlock(fssh_mutex* lock);
77 
78 #ifdef __cplusplus
79 }
80 
81 namespace FSShell {
82 
83 // MutexLocking
84 class MutexLocking {
85 public:
86 	inline bool Lock(fssh_mutex *lockable)
87 	{
88 		return fssh_mutex_lock(lockable) == FSSH_B_OK;
89 	}
90 
91 	inline void Unlock(fssh_mutex *lockable)
92 	{
93 		fssh_mutex_unlock(lockable);
94 	}
95 };
96 
97 // MutexLocker
98 typedef AutoLocker<fssh_mutex, MutexLocking> MutexLocker;
99 
100 // RecursiveLockLocking
101 class RecursiveLockLocking {
102 public:
103 	inline bool Lock(fssh_recursive_lock *lockable)
104 	{
105 		return fssh_recursive_lock_lock(lockable) == FSSH_B_OK;
106 	}
107 
108 	inline void Unlock(fssh_recursive_lock *lockable)
109 	{
110 		fssh_recursive_lock_unlock(lockable);
111 	}
112 };
113 
114 // RecursiveLocker
115 typedef AutoLocker<fssh_recursive_lock, RecursiveLockLocking> RecursiveLocker;
116 
117 }	// namespace FSShell
118 
119 using FSShell::AutoLocker;
120 using FSShell::MutexLocker;
121 using FSShell::RecursiveLocker;
122 
123 #endif	// __cplusplus
124 
125 #endif	/* _FSSH_KERNEL_LOCK_H */
126