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
|
/* $OpenBSD: rthread_mutex.c,v 1.5 2019/02/13 13:09:32 mpi Exp $ */
/*
* Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
* Copyright (c) 2012 Philip Guenther <guenther@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rthread.h"
#include "cancel.h"
#include "synch.h"
/*
* States defined in "Futexes Are Tricky" 5.2
*/
enum {
UNLOCKED = 0,
LOCKED = 1, /* locked without waiter */
CONTENDED = 2, /* threads waiting for this mutex */
};
#define SPIN_COUNT 128
#if defined(__i386__) || defined(__amd64__)
#define SPIN_WAIT() asm volatile("pause": : : "memory")
#else
#define SPIN_WAIT() do { } while (0)
#endif
static _atomic_lock_t static_init_lock = _SPINLOCK_UNLOCKED;
int
pthread_mutex_init(pthread_mutex_t *mutexp, const pthread_mutexattr_t *attr)
{
pthread_mutex_t mutex;
mutex = calloc(1, sizeof(*mutex));
if (mutex == NULL)
return (ENOMEM);
if (attr == NULL) {
mutex->type = PTHREAD_MUTEX_DEFAULT;
mutex->prioceiling = -1;
} else {
mutex->type = (*attr)->ma_type;
mutex->prioceiling = (*attr)->ma_protocol ==
PTHREAD_PRIO_PROTECT ? (*attr)->ma_prioceiling : -1;
}
*mutexp = mutex;
return (0);
}
DEF_STRONG(pthread_mutex_init);
int
pthread_mutex_destroy(pthread_mutex_t *mutexp)
{
pthread_mutex_t mutex;
if (mutexp == NULL || *mutexp == NULL)
return (EINVAL);
mutex = *mutexp;
if (mutex) {
if (mutex->lock != UNLOCKED) {
#define MSG "pthread_mutex_destroy on mutex with waiters!\n"
write(2, MSG, sizeof(MSG) - 1);
#undef MSG
return (EBUSY);
}
free((void *)mutex);
*mutexp = NULL;
}
return (0);
}
DEF_STRONG(pthread_mutex_destroy);
static int
_rthread_mutex_trylock(pthread_mutex_t mutex, int trywait,
const struct timespec *abs)
{
pthread_t self = pthread_self();
if (atomic_cas_uint(&mutex->lock, UNLOCKED, LOCKED) == UNLOCKED) {
membar_enter_after_atomic();
mutex->owner = self;
return (0);
}
if (mutex->owner == self) {
int type = mutex->type;
/* already owner? handle recursive behavior */
if (type != PTHREAD_MUTEX_RECURSIVE) {
if (trywait || type == PTHREAD_MUTEX_ERRORCHECK)
return (trywait ? EBUSY : EDEADLK);
/* self-deadlock is disallowed by strict */
if (type == PTHREAD_MUTEX_STRICT_NP && abs == NULL)
abort();
/* self-deadlock, possibly until timeout */
while (_twait(&mutex->type, type, CLOCK_REALTIME,
abs) != ETIMEDOUT)
;
return (ETIMEDOUT);
} else {
if (mutex->count == INT_MAX)
return (EAGAIN);
mutex->count++;
return (0);
}
}
return (EBUSY);
}
static int
_rthread_mutex_timedlock(pthread_mutex_t *mutexp, int trywait,
const struct timespec *abs, int timed)
{
pthread_t self = pthread_self();
pthread_mutex_t mutex;
unsigned int i, lock;
int error = 0;
if (mutexp == NULL)
return (EINVAL);
/*
* If the mutex is statically initialized, perform the dynamic
* initialization. Note: _thread_mutex_lock() in libc requires
* pthread_mutex_lock() to perform the mutex init when *mutexp
* is NULL.
*/
if (*mutexp == NULL) {
_spinlock(&static_init_lock);
if (*mutexp == NULL)
error = pthread_mutex_init(mutexp, NULL);
_spinunlock(&static_init_lock);
if (error != 0)
return (EINVAL);
}
mutex = *mutexp;
_rthread_debug(5, "%p: mutex_%slock %p (%p)\n", self,
(timed ? "timed" : (trywait ? "try" : "")), (void *)mutex,
(void *)mutex->owner);
error = _rthread_mutex_trylock(mutex, trywait, abs);
if (error != EBUSY || trywait)
return (error);
/* Try hard to not enter the kernel. */
for (i = 0; i < SPIN_COUNT; i++) {
if (mutex->lock == UNLOCKED)
break;
SPIN_WAIT();
}
lock = atomic_cas_uint(&mutex->lock, UNLOCKED, LOCKED);
if (lock == UNLOCKED) {
membar_enter_after_atomic();
mutex->owner = self;
return (0);
}
if (lock != CONTENDED) {
/* Indicate that we're waiting on this mutex. */
lock = atomic_swap_uint(&mutex->lock, CONTENDED);
}
while (lock != UNLOCKED) {
error = _twait(&mutex->lock, CONTENDED, CLOCK_REALTIME, abs);
if (error == ETIMEDOUT)
return (error);
/*
* We cannot know if there's another waiter, so in
* doubt set the state to CONTENDED.
*/
lock = atomic_swap_uint(&mutex->lock, CONTENDED);
};
membar_enter_after_atomic();
mutex->owner = self;
return (0);
}
int
pthread_mutex_trylock(pthread_mutex_t *mutexp)
{
return (_rthread_mutex_timedlock(mutexp, 1, NULL, 0));
}
int
pthread_mutex_timedlock(pthread_mutex_t *mutexp, const struct timespec *abs)
{
return (_rthread_mutex_timedlock(mutexp, 0, abs, 1));
}
int
pthread_mutex_lock(pthread_mutex_t *mutexp)
{
return (_rthread_mutex_timedlock(mutexp, 0, NULL, 0));
}
DEF_STRONG(pthread_mutex_lock);
int
pthread_mutex_unlock(pthread_mutex_t *mutexp)
{
pthread_t self = pthread_self();
pthread_mutex_t mutex;
if (mutexp == NULL)
return (EINVAL);
if (*mutexp == NULL)
#if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_ERRORCHECK
return (EPERM);
#elif PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
return(0);
#else
abort();
#endif
mutex = *mutexp;
_rthread_debug(5, "%p: mutex_unlock %p (%p)\n", self, (void *)mutex,
(void *)mutex->owner);
if (mutex->owner != self) {
_rthread_debug(5, "%p: different owner %p (%p)\n", self, (void *)mutex,
(void *)mutex->owner);
if (mutex->type == PTHREAD_MUTEX_ERRORCHECK ||
mutex->type == PTHREAD_MUTEX_RECURSIVE) {
return (EPERM);
} else {
/*
* For mutex type NORMAL our undefined behavior for
* unlocking an unlocked mutex is to succeed without
* error. All other undefined behaviors are to
* abort() immediately.
*/
if (mutex->owner == NULL &&
mutex->type == PTHREAD_MUTEX_NORMAL)
return (0);
else
abort();
}
}
if (mutex->type == PTHREAD_MUTEX_RECURSIVE) {
if (mutex->count > 0) {
mutex->count--;
return (0);
}
}
mutex->owner = NULL;
membar_exit_before_atomic();
if (atomic_dec_int_nv(&mutex->lock) != UNLOCKED) {
mutex->lock = UNLOCKED;
_wake(&mutex->lock, 1);
}
return (0);
}
DEF_STRONG(pthread_mutex_unlock);
|