~ubuntu-branches/ubuntu/oneiric/openvpn/oneiric

« back to all changes in this revision

Viewing changes to thread.h

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-06-16 18:33:37 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 46.
  • Revision ID: james.westby@ubuntu.com-20110616183337-etb3qgbwjkn8zniq
Tags: upstream-2.2.0
ImportĀ upstreamĀ versionĀ 2.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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
6
 
 *             packet compression.
7
 
 *
8
 
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9
 
 *
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.
13
 
 *
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.
18
 
 *
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
23
 
 */
24
 
 
25
 
#ifndef THREAD_H
26
 
#define THREAD_H
27
 
 
28
 
#include "basic.h"
29
 
#include "common.h"
30
 
 
31
 
/*
32
 
 * OpenVPN static mutex locks, by mutex type
33
 
 */
34
 
#define L_UNUSED       0
35
 
#define L_CTIME        1
36
 
#define L_INET_NTOA    2
37
 
#define L_MSG          3
38
 
#define L_STRERR       4
39
 
#define L_PUTENV       5
40
 
#define L_PRNG         6
41
 
#define L_GETTIMEOFDAY 7
42
 
#define L_ENV_SET      8
43
 
#define L_SYSTEM       9
44
 
#define L_CREATE_TEMP  10
45
 
#define L_PLUGIN       11
46
 
#define N_MUTEXES      12
47
 
 
48
 
#ifdef USE_PTHREAD
49
 
 
50
 
#define MAX_THREADS     50
51
 
 
52
 
#define CACHE_LINE_SIZE 128
53
 
 
54
 
/*
55
 
 * Improve SMP performance by making sure that each
56
 
 * mutex resides in its own cache line.
57
 
 */
58
 
struct sparse_mutex
59
 
{
60
 
  pthread_mutex_t mutex;
61
 
  uint8_t dummy [CACHE_LINE_SIZE - sizeof (pthread_mutex_t)];
62
 
};
63
 
 
64
 
typedef pthread_t openvpn_thread_t;
65
 
 
66
 
extern bool pthread_initialized;
67
 
 
68
 
extern struct sparse_mutex mutex_array[N_MUTEXES];
69
 
 
70
 
#define MUTEX_DEFINE(lock) pthread_mutex_t lock
71
 
#define MUTEX_PTR_DEFINE(lock) pthread_mutex_t *lock
72
 
 
73
 
static inline bool
74
 
openvpn_thread_enabled (void)
75
 
{
76
 
  return pthread_initialized;
77
 
}
78
 
 
79
 
static inline openvpn_thread_t
80
 
openvpn_thread_self (void)
81
 
{
82
 
  return pthread_initialized ? pthread_self() : 0;
83
 
}
84
 
 
85
 
static inline void
86
 
mutex_init (pthread_mutex_t *mutex)
87
 
{
88
 
  if (mutex)
89
 
    pthread_mutex_init (mutex, NULL);
90
 
}
91
 
 
92
 
static inline void
93
 
mutex_destroy (pthread_mutex_t *mutex)
94
 
{
95
 
  if (mutex)
96
 
    pthread_mutex_destroy (mutex);
97
 
}
98
 
 
99
 
static inline void
100
 
mutex_lock (pthread_mutex_t *mutex)
101
 
{
102
 
  if (pthread_initialized && mutex)
103
 
    pthread_mutex_lock (mutex);
104
 
}
105
 
 
106
 
static inline bool
107
 
mutex_trylock (pthread_mutex_t *mutex)
108
 
{
109
 
  if (pthread_initialized && mutex)
110
 
    return pthread_mutex_trylock (mutex) == 0;
111
 
  else
112
 
    return true;
113
 
}
114
 
 
115
 
static inline void
116
 
mutex_unlock (pthread_mutex_t *mutex)
117
 
{
118
 
  if (pthread_initialized && mutex)
119
 
    {
120
 
      pthread_mutex_unlock (mutex);
121
 
#if 1 /* JYFIXME: if race conditions exist, make them more likely to occur */
122
 
      sleep (0);
123
 
#endif
124
 
    }
125
 
}
126
 
 
127
 
static inline void
128
 
mutex_cycle (pthread_mutex_t *mutex)
129
 
{
130
 
  if (pthread_initialized && mutex)
131
 
    {
132
 
      pthread_mutex_unlock (mutex);
133
 
      sleep (0);
134
 
      pthread_mutex_lock (mutex);
135
 
    }
136
 
}
137
 
 
138
 
static inline void
139
 
mutex_lock_static (int type)
140
 
{
141
 
  mutex_lock (&mutex_array[type].mutex);
142
 
}
143
 
 
144
 
static inline void
145
 
mutex_unlock_static (int type)
146
 
{
147
 
  mutex_unlock (&mutex_array[type].mutex);
148
 
}
149
 
 
150
 
static inline void
151
 
mutex_cycle_static (int type)
152
 
{
153
 
  mutex_cycle (&mutex_array[type].mutex);
154
 
}
155
 
 
156
 
void openvpn_thread_init (void);
157
 
void openvpn_thread_cleanup (void);
158
 
 
159
 
openvpn_thread_t openvpn_thread_create (void *(*start_routine) (void *), void* arg);
160
 
void openvpn_thread_join (openvpn_thread_t id);
161
 
 
162
 
#else /* USE_PTHREAD */
163
 
 
164
 
typedef int openvpn_thread_t;
165
 
 
166
 
#if defined(_MSC_VER) || PEDANTIC
167
 
 
168
 
#define MUTEX_DEFINE(lock) int eat_semicolon
169
 
#define MUTEX_PTR_DEFINE(lock) int eat_semicolon
170
 
 
171
 
#else
172
 
 
173
 
#define MUTEX_DEFINE(lock)
174
 
#define MUTEX_PTR_DEFINE(lock)
175
 
 
176
 
#endif
177
 
 
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)
184
 
 
185
 
static inline bool
186
 
openvpn_thread_enabled (void)
187
 
{
188
 
  return false;
189
 
}
190
 
 
191
 
static inline openvpn_thread_t
192
 
openvpn_thread_self (void)
193
 
{
194
 
  return 0;
195
 
}
196
 
 
197
 
static inline void
198
 
openvpn_thread_init (void)
199
 
{
200
 
}
201
 
 
202
 
static inline void
203
 
openvpn_thread_cleanup (void)
204
 
{
205
 
}
206
 
 
207
 
static inline openvpn_thread_t
208
 
openvpn_thread_create (void *(*start_routine) (void *), void* arg)
209
 
{
210
 
  return 0;
211
 
}
212
 
 
213
 
static inline void
214
 
work_thread_join (openvpn_thread_t id)
215
 
{
216
 
}
217
 
 
218
 
static inline void
219
 
mutex_lock_static (int type)
220
 
{
221
 
}
222
 
 
223
 
static inline void
224
 
mutex_unlock_static (int type)
225
 
{
226
 
}
227
 
 
228
 
static inline void
229
 
mutex_cycle_static (int type)
230
 
{
231
 
}
232
 
 
233
 
#endif /* USE_PTHREAD */
234
 
 
235
 
#endif /* THREAD_H */