forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPosixNSPR.cpp
373 lines (307 loc) · 7.94 KB
/
PosixNSPR.cpp
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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/PosixNSPR.h"
#include "js/Utility.h"
#ifdef JS_POSIX_NSPR
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h>
#endif
class nspr::Thread
{
pthread_t pthread_;
void (*start)(void* arg);
void* arg;
bool joinable;
public:
Thread(void (*start)(void* arg), void* arg, bool joinable)
: start(start), arg(arg), joinable(joinable) {}
static void* ThreadRoutine(void* arg);
pthread_t& pthread() { return pthread_; }
};
static pthread_key_t gSelfThreadIndex;
static nspr::Thread gMainThread(nullptr, nullptr, false);
void*
nspr::Thread::ThreadRoutine(void* arg)
{
Thread* self = static_cast<Thread*>(arg);
pthread_setspecific(gSelfThreadIndex, self);
self->start(self->arg);
if (!self->joinable)
js_delete(self);
return nullptr;
}
static bool gInitialized;
void
DummyDestructor(void*)
{
}
/* Should be called from the main thread. */
static void
Initialize()
{
gInitialized = true;
if (pthread_key_create(&gSelfThreadIndex, DummyDestructor)) {
MOZ_CRASH();
return;
}
pthread_setspecific(gSelfThreadIndex, &gMainThread);
}
PRThread*
PR_CreateThread(PRThreadType type,
void (*start)(void* arg),
void* arg,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state,
uint32_t stackSize)
{
MOZ_ASSERT(type == PR_USER_THREAD);
MOZ_ASSERT(priority == PR_PRIORITY_NORMAL);
if (!gInitialized) {
/*
* We assume that the first call to PR_CreateThread happens on the main
* thread.
*/
Initialize();
}
pthread_attr_t attr;
if (pthread_attr_init(&attr))
return nullptr;
if (stackSize && pthread_attr_setstacksize(&attr, stackSize)) {
pthread_attr_destroy(&attr);
return nullptr;
}
nspr::Thread* t = js_new<nspr::Thread>(start, arg,
state != PR_UNJOINABLE_THREAD);
if (!t) {
pthread_attr_destroy(&attr);
return nullptr;
}
if (pthread_create(&t->pthread(), &attr, &nspr::Thread::ThreadRoutine, t)) {
pthread_attr_destroy(&attr);
js_delete(t);
return nullptr;
}
if (state == PR_UNJOINABLE_THREAD) {
if (pthread_detach(t->pthread())) {
pthread_attr_destroy(&attr);
js_delete(t);
return nullptr;
}
}
pthread_attr_destroy(&attr);
return t;
}
PRStatus
PR_JoinThread(PRThread* thread)
{
if (pthread_join(thread->pthread(), nullptr))
return PR_FAILURE;
js_delete(thread);
return PR_SUCCESS;
}
PRThread*
PR_GetCurrentThread()
{
if (!gInitialized)
Initialize();
PRThread* thread = reinterpret_cast<PRThread*>(pthread_getspecific(gSelfThreadIndex));
if (!thread) {
thread = js_new<PRThread>(nullptr, nullptr, false);
if (!thread)
MOZ_CRASH();
pthread_setspecific(gSelfThreadIndex, thread);
}
return thread;
}
PRStatus
PR_SetCurrentThreadName(const char* name)
{
int result;
#ifdef XP_DARWIN
result = pthread_setname_np(name);
#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(pthread_self(), name);
result = 0;
#elif defined(__NetBSD__)
result = pthread_setname_np(pthread_self(), "%s", (void*)name);
#else
result = pthread_setname_np(pthread_self(), name);
#endif
if (result)
return PR_FAILURE;
return PR_SUCCESS;
}
static const size_t MaxTLSKeyCount = 32;
static size_t gTLSKeyCount;
static pthread_key_t gTLSKeys[MaxTLSKeyCount];
PRStatus
PR_NewThreadPrivateIndex(unsigned* newIndex, PRThreadPrivateDTOR destructor)
{
/*
* We only call PR_NewThreadPrivateIndex from the main thread, so there's no
* need to lock the table of TLS keys.
*/
MOZ_ASSERT(PR_GetCurrentThread() == &gMainThread);
pthread_key_t key;
if (pthread_key_create(&key, destructor))
return PR_FAILURE;
MOZ_ASSERT(gTLSKeyCount + 1 < MaxTLSKeyCount);
gTLSKeys[gTLSKeyCount] = key;
*newIndex = gTLSKeyCount;
gTLSKeyCount++;
return PR_SUCCESS;
}
PRStatus
PR_SetThreadPrivate(unsigned index, void* priv)
{
if (index >= gTLSKeyCount)
return PR_FAILURE;
if (pthread_setspecific(gTLSKeys[index], priv))
return PR_FAILURE;
return PR_SUCCESS;
}
void*
PR_GetThreadPrivate(unsigned index)
{
if (index >= gTLSKeyCount)
return nullptr;
return pthread_getspecific(gTLSKeys[index]);
}
PRStatus
PR_CallOnce(PRCallOnceType* once, PRCallOnceFN func)
{
MOZ_CRASH("PR_CallOnce unimplemented");
}
PRStatus
PR_CallOnceWithArg(PRCallOnceType* once, PRCallOnceWithArgFN func, void* arg)
{
MOZ_CRASH("PR_CallOnceWithArg unimplemented");
}
class nspr::Lock
{
pthread_mutex_t mutex_;
public:
Lock() {}
pthread_mutex_t& mutex() { return mutex_; }
};
PRLock*
PR_NewLock()
{
nspr::Lock* lock = js_new<nspr::Lock>();
if (!lock)
return nullptr;
if (pthread_mutex_init(&lock->mutex(), nullptr)) {
js_delete(lock);
return nullptr;
}
return lock;
}
void
PR_DestroyLock(PRLock* lock)
{
pthread_mutex_destroy(&lock->mutex());
js_delete(lock);
}
void
PR_Lock(PRLock* lock)
{
pthread_mutex_lock(&lock->mutex());
}
PRStatus
PR_Unlock(PRLock* lock)
{
if (pthread_mutex_unlock(&lock->mutex()))
return PR_FAILURE;
return PR_SUCCESS;
}
class nspr::CondVar
{
pthread_cond_t cond_;
nspr::Lock* lock_;
public:
explicit CondVar(nspr::Lock* lock) : lock_(lock) {}
pthread_cond_t& cond() { return cond_; }
nspr::Lock* lock() { return lock_; }
};
PRCondVar*
PR_NewCondVar(PRLock* lock)
{
nspr::CondVar* cvar = js_new<nspr::CondVar>(lock);
if (!cvar)
return nullptr;
if (pthread_cond_init(&cvar->cond(), nullptr)) {
js_delete(cvar);
return nullptr;
}
return cvar;
}
void
PR_DestroyCondVar(PRCondVar* cvar)
{
pthread_cond_destroy(&cvar->cond());
js_delete(cvar);
}
PRStatus
PR_NotifyCondVar(PRCondVar* cvar)
{
if (pthread_cond_signal(&cvar->cond()))
return PR_FAILURE;
return PR_SUCCESS;
}
PRStatus
PR_NotifyAllCondVar(PRCondVar* cvar)
{
if (pthread_cond_broadcast(&cvar->cond()))
return PR_FAILURE;
return PR_SUCCESS;
}
uint32_t
PR_MillisecondsToInterval(uint32_t milli)
{
return milli;
}
uint32_t
PR_MicrosecondsToInterval(uint32_t micro)
{
return (micro + 999) / 1000;
}
static const uint64_t TicksPerSecond = 1000;
static const uint64_t NanoSecondsInSeconds = 1000000000;
static const uint64_t MicroSecondsInSeconds = 1000000;
uint32_t
PR_TicksPerSecond()
{
return TicksPerSecond;
}
PRStatus
PR_WaitCondVar(PRCondVar* cvar, uint32_t timeout)
{
if (timeout == PR_INTERVAL_NO_TIMEOUT) {
if (pthread_cond_wait(&cvar->cond(), &cvar->lock()->mutex()))
return PR_FAILURE;
return PR_SUCCESS;
} else {
struct timespec ts;
struct timeval tv;
gettimeofday(&tv, nullptr);
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * (NanoSecondsInSeconds / MicroSecondsInSeconds);
ts.tv_nsec += timeout * (NanoSecondsInSeconds / TicksPerSecond);
while (uint64_t(ts.tv_nsec) >= NanoSecondsInSeconds) {
ts.tv_nsec -= NanoSecondsInSeconds;
ts.tv_sec++;
}
int result = pthread_cond_timedwait(&cvar->cond(), &cvar->lock()->mutex(), &ts);
if (result == 0 || result == ETIMEDOUT)
return PR_SUCCESS;
return PR_FAILURE;
}
}
#endif /* JS_POSIX_NSPR */