a0a198024a0ab2f8ae088abcfa4bbd9ea9bc75e3
[strongswan.git] / src / libstrongswan / utils / mutex.h
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup mutex mutex
18 * @{ @ingroup utils
19 */
20
21 #ifndef MUTEX_H_
22 #define MUTEX_H_
23
24 typedef struct mutex_t mutex_t;
25 typedef struct condvar_t condvar_t;
26 typedef struct rwlock_t rwlock_t;
27 typedef enum mutex_type_t mutex_type_t;
28 typedef enum condvar_type_t condvar_type_t;
29 typedef enum rwlock_type_t rwlock_type_t;
30
31 #include <library.h>
32
33 /**
34 * Type of mutex.
35 */
36 enum mutex_type_t {
37 /** default mutex */
38 MUTEX_DEFAULT = 0,
39 /** allow recursive locking of the mutex */
40 MUTEX_RECURSIVE = 1,
41 };
42
43 /**
44 * Type of condvar.
45 */
46 enum condvar_type_t {
47 /** default condvar */
48 CONDVAR_DEFAULT = 0,
49 };
50
51 /**
52 * Type of read-write lock.
53 */
54 enum rwlock_type_t {
55 /** default condvar */
56 RWLOCK_DEFAULT = 0,
57 };
58
59 /**
60 * Mutex wrapper implements simple, portable and advanced mutex functions.
61 */
62 struct mutex_t {
63
64 /**
65 * Acquire the lock to the mutex.
66 */
67 void (*lock)(mutex_t *this);
68
69 /**
70 * Release the lock on the mutex.
71 */
72 void (*unlock)(mutex_t *this);
73
74 /**
75 * Destroy a mutex instance.
76 */
77 void (*destroy)(mutex_t *this);
78 };
79
80 /**
81 * Condvar wrapper to use in conjunction with mutex_t.
82 */
83 struct condvar_t {
84
85 /**
86 * Wait on a condvar until it gets signalized.
87 *
88 * @param mutex mutex to release while waiting
89 */
90 void (*wait)(condvar_t *this, mutex_t *mutex);
91
92 /**
93 * Wait on a condvar until it gets signalized, or times out.
94 *
95 * @param mutex mutex to release while waiting
96 * @param timeout timeout im ms
97 * @return TRUE if timed out, FALSE otherwise
98 */
99 bool (*timed_wait)(condvar_t *this, mutex_t *mutex, u_int timeout);
100
101 /**
102 * Wake up a single thread in a condvar.
103 */
104 void (*signal)(condvar_t *this);
105
106 /**
107 * Wake up all threads in a condvar.
108 */
109 void (*broadcast)(condvar_t *this);
110
111 /**
112 * Destroy a condvar and free its resources.
113 */
114 void (*destroy)(condvar_t *this);
115 };
116
117 /**
118 * Read-Write lock wrapper.
119 */
120 struct rwlock_t {
121
122 /**
123 * Acquire the read lock.
124 */
125 void (*read_lock)(rwlock_t *this);
126
127 /**
128 * Acquire the write lock.
129 */
130 void (*write_lock)(rwlock_t *this);
131
132 /**
133 * Try to acquire the write lock.
134 *
135 * Never blocks, but returns FALSE if the lock was already occupied.
136 *
137 * @return TRUE if lock acquired
138 */
139 bool (*try_write_lock)(rwlock_t *this);
140
141 /**
142 * Release any acquired lock.
143 */
144 void (*unlock)(rwlock_t *this);
145
146 /**
147 * Destroy the read-write lock.
148 */
149 void (*destroy)(rwlock_t *this);
150 };
151
152 /**
153 * Create a mutex instance.
154 *
155 * @param type type of mutex to create
156 * @return unlocked mutex instance
157 */
158 mutex_t *mutex_create(mutex_type_t type);
159
160 /**
161 * Create a condvar instance.
162 *
163 * @param type type of condvar to create
164 * @return condvar instance
165 */
166 condvar_t *condvar_create(condvar_type_t type);
167
168 /**
169 * Create a read-write lock instance.
170 *
171 * @param type type of rwlock to create
172 * @return unlocked rwlock instance
173 */
174 rwlock_t *rwlock_create(rwlock_type_t type);
175
176 #endif /* MUTEX_H_ @}*/