~vkolesnikov/pbxt/pbxt-preload-test-bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 *
 * PrimeBase XT
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * 2006-03-22	Paul McCullagh
 *
 * H&G2JCtL
 *
 * This file contains windows specific code.
 */

#ifndef __win_xt_h__
#define __win_xt_h__

#ifdef XT_WIN
#include <windef.h>
#include <my_pthread.h>
#else
#include <pthread.h>
#endif

#include "locklist_xt.h"

#ifdef DEBUG
//#define DEBUG_LOCKING
#endif

#define xt_cond_struct			_opaque_pthread_cond_t
#define xt_cond_type			pthread_cond_t

#define xt_cond_wait			pthread_cond_wait
#define xt_cond_wakeall			pthread_cond_broadcast

#ifdef	__cplusplus
extern "C" {
#endif
void	xt_p_init_threading(void);
int		xt_p_set_normal_priority(pthread_t thr);
int		xt_p_set_low_priority(pthread_t thr);
int		xt_p_set_high_priority(pthread_t thr);
#ifdef	__cplusplus
}
#endif

#ifdef XT_WIN

#ifdef	__cplusplus
extern "C" {
#endif

typedef LPVOID pthread_key_t;

typedef struct xt_mutex_struct {
	CRITICAL_SECTION	mt_cs;
#ifdef XT_THREAD_LOCK_INFO
	const char		   *mt_name;
	XTThreadLockInfoRec mt_lock_info;
#endif
} xt_mutex_type;

typedef struct xt_rwlock_struct {
  xt_mutex_type			rw_ex_lock;
  xt_mutex_type			rw_sh_lock;
  pthread_cond_t		rw_sh_cond;
  int					rw_sh_count;
  int					rw_ex_count;
  int					rw_sh_complete_count;
  int					rw_magic;
#ifdef XT_THREAD_LOCK_INFO
	const char		   *rw_name;
	XTThreadLockInfoRec rw_lock_info;
#endif
} xt_rwlock_type;

#ifdef XT_THREAD_LOCK_INFO
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *name);
#else
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr);
#endif
int xt_p_mutex_destroy(xt_mutex_type *mutex);
int xt_p_mutex_lock(xt_mutex_type *mx);
int xt_p_mutex_unlock(xt_mutex_type *mx);
int xt_p_mutex_trylock(xt_mutex_type *mutex);

#ifdef XT_THREAD_LOCK_INFO
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_condattr_t *attr, const char *name);
#else
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_condattr_t *attr);
#endif
int		xt_p_rwlock_destroy(xt_rwlock_type *rwlock);
int		xt_p_rwlock_rdlock(xt_rwlock_type *mx);
int		xt_p_rwlock_wrlock(xt_rwlock_type *mx);
xtBool	xt_p_rwlock_try_wrlock(xt_rwlock_type *rwl);
int		xt_p_rwlock_unlock(xt_rwlock_type *mx);

int		xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex);
int		xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mutex, struct timespec *abstime);

int xt_p_join(pthread_t thread, void **value);

#ifdef	__cplusplus
}
#endif

#ifdef XT_THREAD_LOCK_INFO
#define xt_p_rwlock_init_with_name(a,b,c)   xt_p_rwlock_init(a,b,c)
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))
#else
#define xt_p_rwlock_init_with_name(a,b,c)   xt_p_rwlock_init(a,b,c)
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init(a,b)
#endif

#define xt_slock_rwlock_ns		xt_p_rwlock_rdlock
#define xt_xlock_rwlock_ns		xt_p_rwlock_wrlock
#define xt_xlock_try_rwlock_ns	xt_p_rwlock_try_wrlock
#define xt_unlock_rwlock_ns		xt_p_rwlock_unlock

#ifdef XT_THREAD_LOCK_INFO
#define xt_p_mutex_init_with_name(a,b,c)   xt_p_mutex_init(a,b,c)
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))
#else
#define xt_p_mutex_init_with_name(a,b,c)   xt_p_mutex_init(a,b)
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init(a,b)
#endif
#define xt_lock_mutex_ns		xt_p_mutex_lock
#define xt_unlock_mutex_ns		xt_p_mutex_unlock
#define xt_mutex_trylock		xt_p_mutex_trylock

#else // XT_WIN

/* Finger weg! */
#ifdef pthread_mutex_t
#undef pthread_mutex_t
#endif
#ifdef pthread_rwlock_t
#undef pthread_rwlock_t
#endif
#ifdef pthread_mutex_init
#undef pthread_mutex_init
#endif
#ifdef pthread_mutex_destroy
#undef pthread_mutex_destroy
#endif
#ifdef pthread_mutex_lock
#undef pthread_mutex_lock
#endif
#ifdef pthread_mutex_unlock
#undef pthread_mutex_unlock
#endif
#ifdef pthread_cond_wait
#undef pthread_cond_wait
#endif
#ifdef pthread_cond_broadcast
#undef pthread_cond_broadcast
#endif
#ifdef pthread_mutex_trylock
#undef pthread_mutex_trylock
#endif

/*
 * -----------------------------------------------------------------------
 * Reedefinition of pthread locking, for debugging
 */

struct XTThread;


#ifdef XT_THREAD_LOCK_INFO

#define xt_p_mutex_init_with_name(a,b,c)   xt_p_mutex_init(a,b,c)
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))

#define xt_p_rwlock_init_with_name(a,b,c)   xt_p_rwlock_init(a,b,c)
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))

#else

#define xt_p_mutex_init_with_name(a,b,c)   xt_p_mutex_init(a,b)
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init(a,b)

#define xt_p_rwlock_init_with_name(a,b,c)   xt_p_rwlock_init(a,b)
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init_with_name(a,b)

#endif

#ifdef DEBUG_LOCKING

#ifdef	__cplusplus
extern "C" {
#endif

typedef struct xt_mutex_struct {
	unsigned short				mu_init;
	unsigned short				mu_trace;
	unsigned int				mu_line;
	const char					*mu_file;
	struct XTThread				*mu_locker;
	pthread_mutex_t				mu_plock;
#ifdef XT_THREAD_LOCK_INFO
	const char		   			*mu_name;
	XTThreadLockInfoRec 		mu_lock_info;
#endif
} xt_mutex_type;

typedef struct xt_rwlock_struct {
	u_int						rw_init;
	volatile u_int				rw_readers;
	struct XTThread				*rw_locker;
	pthread_rwlock_t			rw_plock;
#ifdef XT_THREAD_LOCK_INFO
	const char		   			*rw_name;
	XTThreadLockInfoRec 		rw_lock_info;
#endif
} xt_rwlock_type;

int		xt_p_rwlock_rdlock(xt_rwlock_type *mx);
int		xt_p_rwlock_wrlock(xt_rwlock_type *mx);
xtBool	xt_p_rwlock_try_wrlock(xt_rwlock_type *mx);
int		xt_p_rwlock_unlock(xt_rwlock_type *mx);

int xt_p_mutex_lock(xt_mutex_type *mx, u_int line, const char *file);
int xt_p_mutex_unlock(xt_mutex_type *mx);
int xt_p_mutex_trylock(xt_mutex_type *mutex);
int xt_p_mutex_destroy(xt_mutex_type *mutex);
#ifdef XT_THREAD_LOCK_INFO
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *name);
#else
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr);
#endif
int xt_p_rwlock_destroy(xt_rwlock_type * rwlock);
#ifdef XT_THREAD_LOCK_INFO
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr, const char *name);
#else
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr);
#endif
int xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex);
int xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mutex, const struct timespec *abstime);

#ifdef	__cplusplus
}
#endif

#define xt_slock_rwlock_ns			xt_p_rwlock_rdlock
#define xt_xlock_rwlock_ns			xt_p_rwlock_wrlock
#define xt_xlock_try_rwlock_ns		xt_p_rwlock_try_wrlock
#define xt_unlock_rwlock_ns			xt_p_rwlock_unlock

#define xt_lock_mutex_ns(x)			xt_p_mutex_lock(x, __LINE__, __FILE__)
#define xt_unlock_mutex_ns			xt_p_mutex_unlock
#define xt_mutex_trylock			xt_p_mutex_trylock

#else // DEBUG_LOCKING

#define xt_rwlock_struct			_opaque_pthread_rwlock_t
#define xt_mutex_struct				_opaque_pthread_mutex_t

#define xt_rwlock_type				pthread_rwlock_t
#define xt_mutex_type				pthread_mutex_t

#define xt_slock_rwlock_ns			pthread_rwlock_rdlock
#define xt_xlock_rwlock_ns			pthread_rwlock_wrlock
#define xt_xlock_try_rwlock_ns(x)	(pthread_rwlock_trywrlock(x) == 0)
#define xt_unlock_rwlock_ns			pthread_rwlock_unlock

#define xt_lock_mutex_ns			pthread_mutex_lock
#define xt_unlock_mutex_ns			pthread_mutex_unlock
#define xt_mutex_trylock			pthread_mutex_trylock

#define xt_p_mutex_trylock			pthread_mutex_trylock
#define xt_p_mutex_destroy			pthread_mutex_destroy
#define xt_p_mutex_init				pthread_mutex_init
#define xt_p_rwlock_destroy			pthread_rwlock_destroy
#define xt_p_rwlock_init			pthread_rwlock_init
#define xt_p_cond_wait				pthread_cond_wait
#define xt_p_cond_timedwait			pthread_cond_timedwait

#endif // DEBUG_LOCKING

#define xt_p_join				pthread_join

#endif // XT_WIN

#endif