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
298
|
typedef struct PMutex {
pthread_mutex_t mutex;
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spinlock_t lock;
#endif
volatile pthread_t thread;
volatile int counter;
} PMutex;
static void
PMutexInit(
PMutex *pmutexPtr)
{
pthread_mutex_init(&pmutexPtr->mutex, NULL);
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spin_init(&pmutexPtr->lock, PTHREAD_PROCESS_PRIVATE);
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = 0;
pmutexPtr->counter = 0;
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spin_unlock(&pmutexPtr->lock);
#endif
}
static void
PMutexDestroy(
PMutex *pmutexPtr)
{
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spin_destroy(&pmutexPtr->lock);
#endif
pthread_mutex_destroy(&pmutexPtr->mutex);
}
#ifdef HAVE_STDATOMIC_H
static void
PMutexLock(
PMutex *pmutexPtr)
{
pthread_t mythread = pthread_self();
if (__atomic_load_n(&pmutexPtr->counter, __ATOMIC_RELAXED) == 0) {
pthread_mutex_lock(&pmutexPtr->mutex);
__atomic_store_n(&pmutexPtr->thread, mythread, __ATOMIC_RELAXED);
} else if (__atomic_load_n(&pmutexPtr->thread, __ATOMIC_RELAXED) != mythread) {
pthread_mutex_lock(&pmutexPtr->mutex);
__atomic_store_n(&pmutexPtr->thread, mythread, __ATOMIC_RELAXED);
__atomic_store_n(&pmutexPtr->counter, 0, __ATOMIC_RELAXED);
}
__atomic_add_fetch(&pmutexPtr->counter, 1, __ATOMIC_RELAXED);
}
static void
PMutexUnlock(
PMutex *pmutexPtr)
{
if (__atomic_sub_fetch(&pmutexPtr->counter, 1, __ATOMIC_RELAXED) == 0) {
__atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_RELAXED);
pthread_mutex_unlock(&pmutexPtr->mutex);
}
}
static void
PCondWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr)
{
int counter =__atomic_exchange_n(&pmutexPtr->counter, 0, __ATOMIC_RELAXED);
__atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_RELAXED);
pthread_cond_wait(pcondPtr, &pmutexPtr->mutex);
__atomic_store_n(&pmutexPtr->thread, pthread_self(), __ATOMIC_RELAXED);
__atomic_store_n(&pmutexPtr->counter, counter, __ATOMIC_RELAXED);
}
static void
PCondTimedWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr,
struct timespec *ptime)
{
int counter =__atomic_exchange_n(&pmutexPtr->counter, 0, __ATOMIC_RELAXED);
__atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_RELAXED);
pthread_cond_timedwait(pcondPtr, &pmutexPtr->mutex, ptime);
__atomic_store_n(&pmutexPtr->thread, pthread_self(), __ATOMIC_RELAXED);
__atomic_store_n(&pmutexPtr->counter, counter, __ATOMIC_RELAXED);
}
#else /* HAVE_STDATOMIC_H */
static void
PMutexLock(
PMutex *pmutexPtr)
{
pthread_t self = pthread_self();;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
if ((pmutexPtr->thread != self || pmutexPtr->counter == 0)) {
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pthread_mutex_lock(&pmutexPtr->mutex);
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = self;
pmutexPtr->counter = 0;
}
pmutexPtr->counter++;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
}
static void
PMutexUnlock(
PMutex *pmutexPtr)
{
pthread_t self = pthread_self();
int do_unlock;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
if (pmutexPtr->thread != self) {
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
Tcl_Panic("mutex not owned");
}
do_unlock = 0;
pmutexPtr->counter--;
if (pmutexPtr->counter == 0) {
pmutexPtr->thread = 0;
do_unlock = 1;
}
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
if (do_unlock) {
pthread_mutex_unlock(&pmutexPtr->mutex);
}
}
static void
PCondWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr)
{
pthread_t self = pthread_self();
int counter;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
if (pmutexPtr->thread != self) {
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
Tcl_Panic("mutex not owned");
}
counter = pmutexPtr->counter;
pmutexPtr->counter = 0;
pmutexPtr->thread = 0;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pthread_cond_wait(pcondPtr, &pmutexPtr->mutex);
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = self;
pmutexPtr->counter = counter;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
}
static void
PCondTimedWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr,
struct timespec *ptime)
{
pthread_t self = pthread_self();
int counter;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
if (pmutexPtr->thread != self) {
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
Tcl_Panic("mutex not owned");
}
counter = pmutexPtr->counter;
pmutexPtr->counter = 0;
pmutexPtr->thread = 0;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pthread_cond_timedwait(pcondPtr, &pmutexPtr->mutex, ptime);
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = self;
pmutexPtr->counter = counter;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
}
#endif /* HAVE_STDATOMIC_H */
/*
* globalLock is used to serialize creation of mutexes, condition variables,
* and thread local storage. This is the only place that can count on the
|
|
<
<
<
<
>
>
|
>
>
>
>
<
|
>
|
<
<
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
|
|
|
|
>
>
>
>
>
>
|
|
|
|
|
>
|
|
>
>
>
>
|
|
<
<
<
>
|
|
|
|
>
>
<
|
>
|
>
>
>
>
<
<
<
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
<
<
<
<
<
<
<
<
|
<
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
<
<
<
<
<
<
<
<
|
<
>
|
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
typedef struct PMutex {
pthread_mutex_t mutex;
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spinlock_t lock;
#endif
volatile pthread_t thread;
int counter; // Number of additional locks in the same thread.
} PMutex;
static void
PMutexInit(
PMutex *pmutexPtr)
{
pthread_mutex_init(&pmutexPtr->mutex, NULL);
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spin_init(&pmutexPtr->lock, PTHREAD_PROCESS_PRIVATE);
#endif
pmutexPtr->thread = 0;
pmutexPtr->counter = 0;
}
static void
PMutexDestroy(
PMutex *pmutexPtr)
{
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spin_destroy(&pmutexPtr->lock);
#endif
pthread_mutex_destroy(&pmutexPtr->mutex);
}
#ifdef HAVE_STDATOMIC_H
static void
PMutexLock(
PMutex *pmutexPtr)
{
pthread_t mythread = pthread_self();
pthread_t prevthread = 0;
if (__atomic_load_n(&pmutexPtr->thread, __ATOMIC_SEQ_CST) == mythread) {
// We owned the lock already, so it's recursive.
pmutexPtr->counter++;
} else if (__atomic_compare_exchange_n(&pmutexPtr->thread, &prevthread, mythread, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
// No-one owns the lock, so we can safely lock it.
pthread_mutex_lock(&pmutexPtr->mutex);
} else {
// Someone else owned the lock, so we can safely lock it. Then we own it.
pthread_mutex_lock(&pmutexPtr->mutex);
__atomic_store_n(&pmutexPtr->thread, mythread, __ATOMIC_SEQ_CST);
}
}
static void
PMutexUnlock(
PMutex *pmutexPtr)
{
pthread_t mythread = pthread_self();
if (__atomic_load_n(&pmutexPtr->thread, __ATOMIC_SEQ_CST) != mythread) {
Tcl_Panic("mutex not owned");
}
if (pmutexPtr->counter) {
// It's recursive
pmutexPtr->counter--;
} else {
__atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_SEQ_CST);
pthread_mutex_unlock(&pmutexPtr->mutex);
}
}
static void
PCondWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr)
{
pthread_t mythread = pthread_self();
if (__atomic_load_n(&pmutexPtr->thread, __ATOMIC_SEQ_CST) != mythread) {
Tcl_Panic("mutex not owned");
}
int counter = pmutexPtr->counter;
pmutexPtr->counter = 0;
__atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_SEQ_CST);
pthread_cond_wait(pcondPtr, &pmutexPtr->mutex);
__atomic_store_n(&pmutexPtr->thread, mythread, __ATOMIC_SEQ_CST);
pmutexPtr->counter = counter;
}
static void
PCondTimedWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr,
struct timespec *ptime)
{
pthread_t mythread = pthread_self();
if (__atomic_load_n(&pmutexPtr->thread, __ATOMIC_SEQ_CST) != mythread) {
Tcl_Panic("mutex not owned");
}
int counter = pmutexPtr->counter;
pmutexPtr->counter = 0;
__atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_SEQ_CST);
pthread_cond_timedwait(pcondPtr, &pmutexPtr->mutex, ptime);
__atomic_store_n(&pmutexPtr->thread, mythread, __ATOMIC_SEQ_CST);
pmutexPtr->counter = counter;
}
#else /* HAVE_STDATOMIC_H */
static void
PMutexLock(
PMutex *pmutexPtr)
{
pthread_t mythread = pthread_self();
pthread_t mutexthread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
mutexthread = pmutexPtr->thread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
if (mutexthread == mythread) {
// We owned the lock already, so it's recursive.
pmutexPtr->counter++;
} else {
pthread_mutex_lock(&pmutexPtr->mutex);
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = mythread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
}
}
static void
PMutexUnlock(
PMutex *pmutexPtr)
{
pthread_t mythread = pthread_self();
pthread_t mutexthread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
mutexthread = pmutexPtr->thread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
if (mutexthread != mythread) {
Tcl_Panic("mutex not owned");
}
if (pmutexPtr->counter) {
// It's recursive
pmutexPtr->counter--;
} else {
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = 0;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pthread_mutex_unlock(&pmutexPtr->mutex);
}
}
static void
PCondWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr)
{
pthread_t mythread = pthread_self();
pthread_t mutexthread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
mutexthread = pmutexPtr->thread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
if (mutexthread != mythread) {
Tcl_Panic("mutex not owned");
}
int counter = pmutexPtr->counter;
pmutexPtr->counter = 0;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = 0;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pthread_cond_wait(pcondPtr, &pmutexPtr->mutex);
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = mythread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pmutexPtr->counter = counter;
}
static void
PCondTimedWait(
pthread_cond_t *pcondPtr,
PMutex *pmutexPtr,
struct timespec *ptime)
{
pthread_t mythread = pthread_self();
pthread_t mutexthread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
mutexthread = pmutexPtr->thread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
if (mutexthread != mythread) {
Tcl_Panic("mutex not owned");
}
int counter = pmutexPtr->counter;
pmutexPtr->counter = 0;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = 0;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pthread_cond_timedwait(pcondPtr, &pmutexPtr->mutex, ptime);
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_lock(&pmutexPtr->lock);
#endif
pmutexPtr->thread = mythread;
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spin_unlock(&pmutexPtr->lock);
#endif
pmutexPtr->counter = counter;
}
#endif /* HAVE_STDATOMIC_H */
/*
* globalLock is used to serialize creation of mutexes, condition variables,
* and thread local storage. This is the only place that can count on the
|