Implemented a read-write lock using only mutex_t and condvar_t (in case the pthread_r...
[strongswan.git] / src / libstrongswan / threading / rwlock.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 #ifndef THREADING_RWLOCK_H_
18 #define THREADING_RWLOCK_H_
19
20 #include "lock_profiler.h"
21
22 typedef struct private_rwlock_t private_rwlock_t;
23
24 /**
25 * private data of rwlock
26 */
27 struct private_rwlock_t {
28
29 /**
30 * public functions
31 */
32 rwlock_t public;
33
34 #ifdef HAVE_PTHREAD_RWLOCK_INIT
35
36 /**
37 * wrapped pthread rwlock
38 */
39 pthread_rwlock_t rwlock;
40
41 #else
42
43 /**
44 * mutex to emulate a native rwlock
45 */
46 mutex_t *mutex;
47
48 /**
49 * condvar to handle writers
50 */
51 condvar_t *writers;
52
53 /**
54 * condvar to handle readers
55 */
56 condvar_t *readers;
57
58 /**
59 * number of waiting writers
60 */
61 u_int waiting_writers;
62
63 /**
64 * number of readers holding the lock
65 */
66 u_int reader_count;
67
68 /**
69 * current writer thread, if any
70 */
71 pthread_t writer;
72
73 #endif /* HAVE_PTHREAD_RWLOCK_INIT */
74
75 /**
76 * profiling info, if enabled
77 */
78 lock_profile_t profile;
79 };
80
81 #endif /* THREADING_THREADING_H_ */
82