use monotonic time source in convar->timed_wait, and in the scheduler using it
[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 #ifdef __APPLE__
35 /* on Mac OS X 10.5 several system calls we use are no cancellation points.
36 * fortunately, select isn't one of them, so we wrap some of the others with
37 * calls to select(2).
38 */
39 #include <sys/socket.h>
40 #include <sys/select.h>
41
42 #define WRAP_WITH_SELECT(func, socket, ...)\
43 fd_set rfds; FD_ZERO(&rfds); FD_SET(socket, &rfds);\
44 if (select(socket + 1, &rfds, NULL, NULL, NULL) <= 0) { return -1; }\
45 return func(socket, __VA_ARGS__)
46
47 static inline int cancellable_accept(int socket, struct sockaddr *address,
48 socklen_t *address_len)
49 {
50 WRAP_WITH_SELECT(accept, socket, address, address_len);
51 }
52 #define accept cancellable_accept
53 static inline int cancellable_recvfrom(int socket, void *buffer, size_t length,
54 int flags, struct sockaddr *address, socklen_t *address_len)
55 {
56 WRAP_WITH_SELECT(recvfrom, socket, buffer, length, flags, address, address_len);
57 }
58 #define recvfrom cancellable_recvfrom
59 #endif /* __APPLE__ */
60
61 /**
62 * Type of mutex.
63 */
64 enum mutex_type_t {
65 /** default mutex */
66 MUTEX_TYPE_DEFAULT = 0,
67 /** allow recursive locking of the mutex */
68 MUTEX_TYPE_RECURSIVE = 1,
69 };
70
71 /**
72 * Type of condvar.
73 */
74 enum condvar_type_t {
75 /** default condvar */
76 CONDVAR_TYPE_DEFAULT = 0,
77 };
78
79 /**
80 * Type of read-write lock.
81 */
82 enum rwlock_type_t {
83 /** default condvar */
84 RWLOCK_TYPE_DEFAULT = 0,
85 };
86
87 /**
88 * Mutex wrapper implements simple, portable and advanced mutex functions.
89 */
90 struct mutex_t {
91
92 /**
93 * Acquire the lock to the mutex.
94 */
95 void (*lock)(mutex_t *this);
96
97 /**
98 * Release the lock on the mutex.
99 */
100 void (*unlock)(mutex_t *this);
101
102 /**
103 * Destroy a mutex instance.
104 */
105 void (*destroy)(mutex_t *this);
106 };
107
108 /**
109 * Condvar wrapper to use in conjunction with mutex_t.
110 */
111 struct condvar_t {
112
113 /**
114 * Wait on a condvar until it gets signalized.
115 *
116 * @param mutex mutex to release while waiting
117 */
118 void (*wait)(condvar_t *this, mutex_t *mutex);
119
120 /**
121 * Wait on a condvar until it gets signalized, or times out.
122 *
123 * @param mutex mutex to release while waiting
124 * @param timeout timeout im ms
125 * @return TRUE if timed out, FALSE otherwise
126 */
127 bool (*timed_wait)(condvar_t *this, mutex_t *mutex, u_int timeout);
128
129 /**
130 * Wait on a condvar until it gets signalized, or times out.
131 *
132 * The passed timeval should be calculated based on the time_monotonic()
133 * function.
134 *
135 * @param mutex mutex to release while waiting
136 * @param tv absolute time until timeout
137 * @return TRUE if timed out, FALSE otherwise
138 */
139 bool (*timed_wait_abs)(condvar_t *this, mutex_t *mutex, timeval_t tv);
140
141 /**
142 * Wake up a single thread in a condvar.
143 */
144 void (*signal)(condvar_t *this);
145
146 /**
147 * Wake up all threads in a condvar.
148 */
149 void (*broadcast)(condvar_t *this);
150
151 /**
152 * Destroy a condvar and free its resources.
153 */
154 void (*destroy)(condvar_t *this);
155 };
156
157 /**
158 * Read-Write lock wrapper.
159 */
160 struct rwlock_t {
161
162 /**
163 * Acquire the read lock.
164 */
165 void (*read_lock)(rwlock_t *this);
166
167 /**
168 * Acquire the write lock.
169 */
170 void (*write_lock)(rwlock_t *this);
171
172 /**
173 * Try to acquire the write lock.
174 *
175 * Never blocks, but returns FALSE if the lock was already occupied.
176 *
177 * @return TRUE if lock acquired
178 */
179 bool (*try_write_lock)(rwlock_t *this);
180
181 /**
182 * Release any acquired lock.
183 */
184 void (*unlock)(rwlock_t *this);
185
186 /**
187 * Destroy the read-write lock.
188 */
189 void (*destroy)(rwlock_t *this);
190 };
191
192 /**
193 * Create a mutex instance.
194 *
195 * @param type type of mutex to create
196 * @return unlocked mutex instance
197 */
198 mutex_t *mutex_create(mutex_type_t type);
199
200 /**
201 * Create a condvar instance.
202 *
203 * @param type type of condvar to create
204 * @return condvar instance
205 */
206 condvar_t *condvar_create(condvar_type_t type);
207
208 /**
209 * Create a read-write lock instance.
210 *
211 * @param type type of rwlock to create
212 * @return unlocked rwlock instance
213 */
214 rwlock_t *rwlock_create(rwlock_type_t type);
215
216 #endif /** MUTEX_H_ @}*/