2
* OpenVPN -- An application to securely tunnel IP networks
3
* over a single UDP port, with support for SSL/TLS-based
4
* session authentication and key exchange,
5
* packet encryption, packet authentication, and
8
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License version 2
12
* as published by the Free Software Foundation.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program (see the file COPYING included with this
21
* distribution); if not, write to the Free Software Foundation, Inc.,
22
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
* OpenVPN static mutex locks, by mutex type
41
#define L_GETTIMEOFDAY 7
44
#define L_CREATE_TEMP 10
50
#define MAX_THREADS 50
52
#define CACHE_LINE_SIZE 128
55
* Improve SMP performance by making sure that each
56
* mutex resides in its own cache line.
60
pthread_mutex_t mutex;
61
uint8_t dummy [CACHE_LINE_SIZE - sizeof (pthread_mutex_t)];
64
typedef pthread_t openvpn_thread_t;
66
extern bool pthread_initialized;
68
extern struct sparse_mutex mutex_array[N_MUTEXES];
70
#define MUTEX_DEFINE(lock) pthread_mutex_t lock
71
#define MUTEX_PTR_DEFINE(lock) pthread_mutex_t *lock
74
openvpn_thread_enabled (void)
76
return pthread_initialized;
79
static inline openvpn_thread_t
80
openvpn_thread_self (void)
82
return pthread_initialized ? pthread_self() : 0;
86
mutex_init (pthread_mutex_t *mutex)
89
pthread_mutex_init (mutex, NULL);
93
mutex_destroy (pthread_mutex_t *mutex)
96
pthread_mutex_destroy (mutex);
100
mutex_lock (pthread_mutex_t *mutex)
102
if (pthread_initialized && mutex)
103
pthread_mutex_lock (mutex);
107
mutex_trylock (pthread_mutex_t *mutex)
109
if (pthread_initialized && mutex)
110
return pthread_mutex_trylock (mutex) == 0;
116
mutex_unlock (pthread_mutex_t *mutex)
118
if (pthread_initialized && mutex)
120
pthread_mutex_unlock (mutex);
121
#if 1 /* JYFIXME: if race conditions exist, make them more likely to occur */
128
mutex_cycle (pthread_mutex_t *mutex)
130
if (pthread_initialized && mutex)
132
pthread_mutex_unlock (mutex);
134
pthread_mutex_lock (mutex);
139
mutex_lock_static (int type)
141
mutex_lock (&mutex_array[type].mutex);
145
mutex_unlock_static (int type)
147
mutex_unlock (&mutex_array[type].mutex);
151
mutex_cycle_static (int type)
153
mutex_cycle (&mutex_array[type].mutex);
156
void openvpn_thread_init (void);
157
void openvpn_thread_cleanup (void);
159
openvpn_thread_t openvpn_thread_create (void *(*start_routine) (void *), void* arg);
160
void openvpn_thread_join (openvpn_thread_t id);
162
#else /* USE_PTHREAD */
164
typedef int openvpn_thread_t;
166
#if defined(_MSC_VER) || PEDANTIC
168
#define MUTEX_DEFINE(lock) int eat_semicolon
169
#define MUTEX_PTR_DEFINE(lock) int eat_semicolon
173
#define MUTEX_DEFINE(lock)
174
#define MUTEX_PTR_DEFINE(lock)
178
#define mutex_init(m)
179
#define mutex_destroy(m)
180
#define mutex_lock(m)
181
#define mutex_trylock(m) (true)
182
#define mutex_unlock(m)
183
#define mutex_cycle(m)
186
openvpn_thread_enabled (void)
191
static inline openvpn_thread_t
192
openvpn_thread_self (void)
198
openvpn_thread_init (void)
203
openvpn_thread_cleanup (void)
207
static inline openvpn_thread_t
208
openvpn_thread_create (void *(*start_routine) (void *), void* arg)
214
work_thread_join (openvpn_thread_t id)
219
mutex_lock_static (int type)
224
mutex_unlock_static (int type)
229
mutex_cycle_static (int type)
233
#endif /* USE_PTHREAD */
235
#endif /* THREAD_H */