2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
17 * @defgroup mutex mutex
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
;
39 /** allow recursive locking of the mutex */
47 /** default condvar */
52 * Type of read-write lock.
55 /** default condvar */
60 * Mutex wrapper implements simple, portable and advanced mutex functions.
65 * Acquire the lock to the mutex.
67 void (*lock
)(mutex_t
*this);
70 * Release the lock on the mutex.
72 void (*unlock
)(mutex_t
*this);
75 * Destroy a mutex instance.
77 void (*destroy
)(mutex_t
*this);
81 * Condvar wrapper to use in conjunction with mutex_t.
86 * Wait on a condvar until it gets signalized.
88 * @param mutex mutex to release while waiting
90 void (*wait
)(condvar_t
*this, mutex_t
*mutex
);
93 * Wait on a condvar until it gets signalized, or times out.
95 * @param mutex mutex to release while waiting
96 * @param timeout timeout im ms
97 * @return TRUE if timed out, FALSE otherwise
99 bool (*timed_wait
)(condvar_t
*this, mutex_t
*mutex
, u_int timeout
);
102 * Wake up a single thread in a condvar.
104 void (*signal
)(condvar_t
*this);
107 * Wake up all threads in a condvar.
109 void (*broadcast
)(condvar_t
*this);
112 * Destroy a condvar and free its resources.
114 void (*destroy
)(condvar_t
*this);
118 * Read-Write lock wrapper.
123 * Acquire the read lock.
125 void (*read_lock
)(rwlock_t
*this);
128 * Acquire the write lock.
130 void (*write_lock
)(rwlock_t
*this);
133 * Try to acquire the write lock.
135 * Never blocks, but returns FALSE if the lock was already occupied.
137 * @return TRUE if lock acquired
139 bool (*try_write_lock
)(rwlock_t
*this);
142 * Release any acquired lock.
144 void (*unlock
)(rwlock_t
*this);
147 * Destroy the read-write lock.
149 void (*destroy
)(rwlock_t
*this);
153 * Create a mutex instance.
155 * @param type type of mutex to create
156 * @return unlocked mutex instance
158 mutex_t
*mutex_create(mutex_type_t type
);
161 * Create a condvar instance.
163 * @param type type of condvar to create
164 * @return condvar instance
166 condvar_t
*condvar_create(condvar_type_t type
);
169 * Create a read-write lock instance.
171 * @param type type of rwlock to create
172 * @return unlocked rwlock instance
174 rwlock_t
*rwlock_create(rwlock_type_t type
);
176 #endif /* MUTEX_H_ @}*/