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
|
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
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
|
*
* Waits for a limited amount of time on a condition variable linked to a
* recursive lock. (Similar to pthread_cond_timedwait)
*
*----------------------------------------------------------------------
*/
#ifndef HAVE_DECL_PTHREAD_MUTEX_RECURSIVE
#define HAVE_DECL_PTHREAD_MUTEX_RECURSIVE 0
#endif
#if HAVE_DECL_PTHREAD_MUTEX_RECURSIVE
/*
* Pthread has native reentrant (AKA recursive) mutexes. Use them for
* Tcl_Mutex.
*/
typedef pthread_mutex_t PMutex;
static void
PMutexInit(
PMutex *pmutexPtr)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(pmutexPtr, &attr);
}
#define PMutexDestroy pthread_mutex_destroy
#define PMutexLock pthread_mutex_lock
#define PMutexUnlock pthread_mutex_unlock
#define PCondWait pthread_cond_wait
#define PCondTimedWait pthread_cond_timedwait
#else /* !HAVE_PTHREAD_MUTEX_RECURSIVE */
/*
* No native support for reentrant mutexes. Emulate them with regular mutexes
* and thread-local counters.
* No correct native support for reentrant mutexes. Emulate them with regular mutexes
* and threadlocal counters.
*/
typedef struct PMutex {
pthread_mutex_t mutex;
#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H)
pthread_spinlock_t lock;
#endif
pthread_t thread;
int counter;
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();
if (__atomic_load_n(&pmutexPtr->thread, __ATOMIC_SEQ_CST) == mythread) {
// We own the lock already, so it's recursive.
if (pmutexPtr->thread != pthread_self() || pmutexPtr->counter == 0) {
pmutexPtr->counter++;
} else {
// We don't owns the lock, so we have to 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->thread = pthread_self();
pmutexPtr->counter = 0;
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
}
pmutexPtr->counter++;
}
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");
}
pmutexPtr->counter--;
if (pmutexPtr->counter == 0) {
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_PTHREAD_MUTEX_RECURSIVE */
#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
* ability to statically initialize the mutex.
*/
|