Make accept(2) and recvfrom(2) cancellation points on Mac OS X.
[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_DEFAULT = 0,
67 /** allow recursive locking of the mutex */
68 MUTEX_RECURSIVE = 1,
69 };
70
71 /**
72 * Type of condvar.
73 */
74 enum condvar_type_t {
75 /** default condvar */
76 CONDVAR_DEFAULT = 0,
77 };
78
79 /**
80 * Type of read-write lock.
81 */
82 enum rwlock_type_t {
83 /** default condvar */
84 RWLOCK_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 * @param mutex mutex to release while waiting
133 * @param time absolute time until timeout
134 * @return TRUE if timed out, FALSE otherwise
135 */
136 bool (*timed_wait_abs)(condvar_t *this, mutex_t *mutex, timeval_t timeout);
137
138 /**
139 * Wake up a single thread in a condvar.
140 */
141 void (*signal)(condvar_t *this);
142
143 /**
144 * Wake up all threads in a condvar.
145 */
146 void (*broadcast)(condvar_t *this);
147
148 /**
149 * Destroy a condvar and free its resources.
150 */
151 void (*destroy)(condvar_t *this);
152 };
153
154 /**
155 * Read-Write lock wrapper.
156 */
157 struct rwlock_t {
158
159 /**
160 * Acquire the read lock.
161 */
162 void (*read_lock)(rwlock_t *this);
163
164 /**
165 * Acquire the write lock.
166 */
167 void (*write_lock)(rwlock_t *this);
168
169 /**
170 * Try to acquire the write lock.
171 *
172 * Never blocks, but returns FALSE if the lock was already occupied.
173 *
174 * @return TRUE if lock acquired
175 */
176 bool (*try_write_lock)(rwlock_t *this);
177
178 /**
179 * Release any acquired lock.
180 */
181 void (*unlock)(rwlock_t *this);
182
183 /**
184 * Destroy the read-write lock.
185 */
186 void (*destroy)(rwlock_t *this);
187 };
188
189 /**
190 * Create a mutex instance.
191 *
192 * @param type type of mutex to create
193 * @return unlocked mutex instance
194 */
195 mutex_t *mutex_create(mutex_type_t type);
196
197 /**
198 * Create a condvar instance.
199 *
200 * @param type type of condvar to create
201 * @return condvar instance
202 */
203 condvar_t *condvar_create(condvar_type_t type);
204
205 /**
206 * Create a read-write lock instance.
207 *
208 * @param type type of rwlock to create
209 * @return unlocked rwlock instance
210 */
211 rwlock_t *rwlock_create(rwlock_type_t type);
212
213 #endif /** MUTEX_H_ @}*/