1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* 2006-03-22 Paul McCullagh
23
* This file contains windows specific code.
31
#include <my_pthread.h>
36
#include "locklist_xt.h"
39
//#define DEBUG_LOCKING
42
#define xt_cond_struct _opaque_pthread_cond_t
43
#define xt_cond_type pthread_cond_t
45
#define xt_cond_wait pthread_cond_wait
46
#define xt_cond_wakeall pthread_cond_broadcast
51
void xt_p_init_threading(void);
52
int xt_p_set_normal_priority(pthread_t thr);
53
int xt_p_set_low_priority(pthread_t thr);
54
int xt_p_set_high_priority(pthread_t thr);
65
typedef LPVOID pthread_key_t;
67
typedef struct xt_mutex_struct {
68
CRITICAL_SECTION mt_cs;
69
#ifdef XT_THREAD_LOCK_INFO
71
XTThreadLockInfoRec mt_lock_info;
75
typedef struct xt_rwlock_struct {
76
xt_mutex_type rw_ex_lock;
77
xt_mutex_type rw_sh_lock;
78
pthread_cond_t rw_sh_cond;
81
int rw_sh_complete_count;
83
#ifdef XT_THREAD_LOCK_INFO
85
XTThreadLockInfoRec rw_lock_info;
89
#ifdef XT_THREAD_LOCK_INFO
90
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *name);
92
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr);
94
int xt_p_mutex_destroy(xt_mutex_type *mutex);
95
int xt_p_mutex_lock(xt_mutex_type *mx);
96
int xt_p_mutex_unlock(xt_mutex_type *mx);
97
int xt_p_mutex_trylock(xt_mutex_type *mutex);
99
#ifdef XT_THREAD_LOCK_INFO
100
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_condattr_t *attr, const char *name);
102
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_condattr_t *attr);
104
int xt_p_rwlock_destroy(xt_rwlock_type *rwlock);
105
int xt_p_rwlock_rdlock(xt_rwlock_type *mx);
106
int xt_p_rwlock_wrlock(xt_rwlock_type *mx);
107
xtBool xt_p_rwlock_try_wrlock(xt_rwlock_type *rwl);
108
int xt_p_rwlock_unlock(xt_rwlock_type *mx);
110
int xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex);
111
int xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mutex, struct timespec *abstime);
113
int xt_p_join(pthread_t thread, void **value);
119
#ifdef XT_THREAD_LOCK_INFO
120
#define xt_p_rwlock_init_with_name(a,b,c) xt_p_rwlock_init(a,b,c)
121
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))
123
#define xt_p_rwlock_init_with_name(a,b,c) xt_p_rwlock_init(a,b,c)
124
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init(a,b)
127
#define xt_slock_rwlock_ns xt_p_rwlock_rdlock
128
#define xt_xlock_rwlock_ns xt_p_rwlock_wrlock
129
#define xt_xlock_try_rwlock_ns xt_p_rwlock_try_wrlock
130
#define xt_unlock_rwlock_ns xt_p_rwlock_unlock
132
#ifdef XT_THREAD_LOCK_INFO
133
#define xt_p_mutex_init_with_name(a,b,c) xt_p_mutex_init(a,b,c)
134
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))
136
#define xt_p_mutex_init_with_name(a,b,c) xt_p_mutex_init(a,b)
137
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init(a,b)
139
#define xt_lock_mutex_ns xt_p_mutex_lock
140
#define xt_unlock_mutex_ns xt_p_mutex_unlock
141
#define xt_mutex_trylock xt_p_mutex_trylock
146
#ifdef pthread_mutex_t
147
#undef pthread_mutex_t
149
#ifdef pthread_rwlock_t
150
#undef pthread_rwlock_t
152
#ifdef pthread_mutex_init
153
#undef pthread_mutex_init
155
#ifdef pthread_mutex_destroy
156
#undef pthread_mutex_destroy
158
#ifdef pthread_mutex_lock
159
#undef pthread_mutex_lock
161
#ifdef pthread_mutex_unlock
162
#undef pthread_mutex_unlock
164
#ifdef pthread_cond_wait
165
#undef pthread_cond_wait
167
#ifdef pthread_cond_broadcast
168
#undef pthread_cond_broadcast
170
#ifdef pthread_mutex_trylock
171
#undef pthread_mutex_trylock
175
* -----------------------------------------------------------------------
176
* Reedefinition of pthread locking, for debugging
182
#ifdef XT_THREAD_LOCK_INFO
184
#define xt_p_mutex_init_with_name(a,b,c) xt_p_mutex_init(a,b,c)
185
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))
187
#define xt_p_rwlock_init_with_name(a,b,c) xt_p_rwlock_init(a,b,c)
188
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init_with_name(a,b,LOCKLIST_ARG_SUFFIX(a))
192
#define xt_p_mutex_init_with_name(a,b,c) xt_p_mutex_init(a,b)
193
#define xt_p_mutex_init_with_autoname(a,b) xt_p_mutex_init(a,b)
195
#define xt_p_rwlock_init_with_name(a,b,c) xt_p_rwlock_init(a,b)
196
#define xt_p_rwlock_init_with_autoname(a,b) xt_p_rwlock_init_with_name(a,b)
206
typedef struct xt_mutex_struct {
207
unsigned short mu_init;
208
unsigned short mu_trace;
209
unsigned int mu_line;
211
struct XTThread *mu_locker;
212
pthread_mutex_t mu_plock;
213
#ifdef XT_THREAD_LOCK_INFO
215
XTThreadLockInfoRec mu_lock_info;
219
typedef struct xt_rwlock_struct {
221
volatile u_int rw_readers;
222
struct XTThread *rw_locker;
223
pthread_rwlock_t rw_plock;
224
#ifdef XT_THREAD_LOCK_INFO
226
XTThreadLockInfoRec rw_lock_info;
230
int xt_p_rwlock_rdlock(xt_rwlock_type *mx);
231
int xt_p_rwlock_wrlock(xt_rwlock_type *mx);
232
xtBool xt_p_rwlock_try_wrlock(xt_rwlock_type *mx);
233
int xt_p_rwlock_unlock(xt_rwlock_type *mx);
235
int xt_p_mutex_lock(xt_mutex_type *mx, u_int line, const char *file);
236
int xt_p_mutex_unlock(xt_mutex_type *mx);
237
int xt_p_mutex_trylock(xt_mutex_type *mutex);
238
int xt_p_mutex_destroy(xt_mutex_type *mutex);
239
#ifdef XT_THREAD_LOCK_INFO
240
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *name);
242
int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr);
244
int xt_p_rwlock_destroy(xt_rwlock_type * rwlock);
245
#ifdef XT_THREAD_LOCK_INFO
246
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr, const char *name);
248
int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr);
250
int xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex);
251
int xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mutex, const struct timespec *abstime);
257
#define xt_slock_rwlock_ns xt_p_rwlock_rdlock
258
#define xt_xlock_rwlock_ns xt_p_rwlock_wrlock
259
#define xt_xlock_try_rwlock_ns xt_p_rwlock_try_wrlock
260
#define xt_unlock_rwlock_ns xt_p_rwlock_unlock
262
#define xt_lock_mutex_ns(x) xt_p_mutex_lock(x, __LINE__, __FILE__)
263
#define xt_unlock_mutex_ns xt_p_mutex_unlock
264
#define xt_mutex_trylock xt_p_mutex_trylock
266
#else // DEBUG_LOCKING
268
#define xt_rwlock_struct _opaque_pthread_rwlock_t
269
#define xt_mutex_struct _opaque_pthread_mutex_t
271
#define xt_rwlock_type pthread_rwlock_t
272
#define xt_mutex_type pthread_mutex_t
274
#define xt_slock_rwlock_ns pthread_rwlock_rdlock
275
#define xt_xlock_rwlock_ns pthread_rwlock_wrlock
276
#define xt_xlock_try_rwlock_ns(x) (pthread_rwlock_trywrlock(x) == 0)
277
#define xt_unlock_rwlock_ns pthread_rwlock_unlock
279
#define xt_lock_mutex_ns pthread_mutex_lock
280
#define xt_unlock_mutex_ns pthread_mutex_unlock
281
#define xt_mutex_trylock pthread_mutex_trylock
283
#define xt_p_mutex_trylock pthread_mutex_trylock
284
#define xt_p_mutex_destroy pthread_mutex_destroy
285
#define xt_p_mutex_init pthread_mutex_init
286
#define xt_p_rwlock_destroy pthread_rwlock_destroy
287
#define xt_p_rwlock_init pthread_rwlock_init
288
#define xt_p_cond_wait pthread_cond_wait
289
#define xt_p_cond_timedwait pthread_cond_timedwait
291
#endif // DEBUG_LOCKING
293
#define xt_p_join pthread_join