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