js/src/vm/PosixNSPR.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "vm/PosixNSPR.h"
michael@0 8
michael@0 9 #include "js/Utility.h"
michael@0 10
michael@0 11 #ifdef JS_POSIX_NSPR
michael@0 12
michael@0 13 #include <errno.h>
michael@0 14 #include <sys/time.h>
michael@0 15 #include <time.h>
michael@0 16
michael@0 17 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
michael@0 18 #include <pthread_np.h>
michael@0 19 #endif
michael@0 20
michael@0 21 class nspr::Thread
michael@0 22 {
michael@0 23 pthread_t pthread_;
michael@0 24 void (*start)(void *arg);
michael@0 25 void *arg;
michael@0 26 bool joinable;
michael@0 27
michael@0 28 public:
michael@0 29 Thread(void (*start)(void *arg), void *arg, bool joinable)
michael@0 30 : start(start), arg(arg), joinable(joinable) {}
michael@0 31
michael@0 32 static void *ThreadRoutine(void *arg);
michael@0 33
michael@0 34 pthread_t &pthread() { return pthread_; }
michael@0 35 };
michael@0 36
michael@0 37 static pthread_key_t gSelfThreadIndex;
michael@0 38 static nspr::Thread gMainThread(nullptr, nullptr, false);
michael@0 39
michael@0 40 void *
michael@0 41 nspr::Thread::ThreadRoutine(void *arg)
michael@0 42 {
michael@0 43 Thread *self = static_cast<Thread *>(arg);
michael@0 44 pthread_setspecific(gSelfThreadIndex, self);
michael@0 45 self->start(self->arg);
michael@0 46 if (!self->joinable)
michael@0 47 js_delete(self);
michael@0 48 return nullptr;
michael@0 49 }
michael@0 50
michael@0 51 static bool gInitialized;
michael@0 52
michael@0 53 void
michael@0 54 DummyDestructor(void *)
michael@0 55 {
michael@0 56 }
michael@0 57
michael@0 58 /* Should be called from the main thread. */
michael@0 59 static void
michael@0 60 Initialize()
michael@0 61 {
michael@0 62 gInitialized = true;
michael@0 63
michael@0 64 if (pthread_key_create(&gSelfThreadIndex, DummyDestructor)) {
michael@0 65 MOZ_CRASH();
michael@0 66 return;
michael@0 67 }
michael@0 68
michael@0 69 pthread_setspecific(gSelfThreadIndex, &gMainThread);
michael@0 70 }
michael@0 71
michael@0 72 PRThread *
michael@0 73 PR_CreateThread(PRThreadType type,
michael@0 74 void (*start)(void *arg),
michael@0 75 void *arg,
michael@0 76 PRThreadPriority priority,
michael@0 77 PRThreadScope scope,
michael@0 78 PRThreadState state,
michael@0 79 uint32_t stackSize)
michael@0 80 {
michael@0 81 JS_ASSERT(type == PR_USER_THREAD);
michael@0 82 JS_ASSERT(priority == PR_PRIORITY_NORMAL);
michael@0 83
michael@0 84 if (!gInitialized) {
michael@0 85 /*
michael@0 86 * We assume that the first call to PR_CreateThread happens on the main
michael@0 87 * thread.
michael@0 88 */
michael@0 89 Initialize();
michael@0 90 }
michael@0 91
michael@0 92 pthread_attr_t attr;
michael@0 93 if (pthread_attr_init(&attr))
michael@0 94 return nullptr;
michael@0 95
michael@0 96 if (stackSize && pthread_attr_setstacksize(&attr, stackSize)) {
michael@0 97 pthread_attr_destroy(&attr);
michael@0 98 return nullptr;
michael@0 99 }
michael@0 100
michael@0 101 nspr::Thread *t = js_new<nspr::Thread>(start, arg,
michael@0 102 state != PR_UNJOINABLE_THREAD);
michael@0 103 if (!t) {
michael@0 104 pthread_attr_destroy(&attr);
michael@0 105 return nullptr;
michael@0 106 }
michael@0 107
michael@0 108 if (pthread_create(&t->pthread(), &attr, &nspr::Thread::ThreadRoutine, t)) {
michael@0 109 pthread_attr_destroy(&attr);
michael@0 110 js_delete(t);
michael@0 111 return nullptr;
michael@0 112 }
michael@0 113
michael@0 114 if (state == PR_UNJOINABLE_THREAD) {
michael@0 115 if (pthread_detach(t->pthread())) {
michael@0 116 pthread_attr_destroy(&attr);
michael@0 117 js_delete(t);
michael@0 118 return nullptr;
michael@0 119 }
michael@0 120 }
michael@0 121
michael@0 122 pthread_attr_destroy(&attr);
michael@0 123
michael@0 124 return t;
michael@0 125 }
michael@0 126
michael@0 127 PRStatus
michael@0 128 PR_JoinThread(PRThread *thread)
michael@0 129 {
michael@0 130 if (pthread_join(thread->pthread(), nullptr))
michael@0 131 return PR_FAILURE;
michael@0 132
michael@0 133 js_delete(thread);
michael@0 134
michael@0 135 return PR_SUCCESS;
michael@0 136 }
michael@0 137
michael@0 138 PRThread *
michael@0 139 PR_GetCurrentThread()
michael@0 140 {
michael@0 141 if (!gInitialized)
michael@0 142 Initialize();
michael@0 143
michael@0 144 return (PRThread *)pthread_getspecific(gSelfThreadIndex);
michael@0 145 }
michael@0 146
michael@0 147 PRStatus
michael@0 148 PR_SetCurrentThreadName(const char *name)
michael@0 149 {
michael@0 150 int result;
michael@0 151 #ifdef XP_MACOSX
michael@0 152 result = pthread_setname_np(name);
michael@0 153 #elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
michael@0 154 pthread_set_name_np(pthread_self(), name);
michael@0 155 result = 0;
michael@0 156 #elif defined(__NetBSD__)
michael@0 157 result = pthread_setname_np(pthread_self(), "%s", (void *)name);
michael@0 158 #else
michael@0 159 result = pthread_setname_np(pthread_self(), name);
michael@0 160 #endif
michael@0 161 if (result)
michael@0 162 return PR_FAILURE;
michael@0 163 return PR_SUCCESS;
michael@0 164 }
michael@0 165
michael@0 166 static const size_t MaxTLSKeyCount = 32;
michael@0 167 static size_t gTLSKeyCount;
michael@0 168 static pthread_key_t gTLSKeys[MaxTLSKeyCount];
michael@0 169
michael@0 170 PRStatus
michael@0 171 PR_NewThreadPrivateIndex(unsigned *newIndex, PRThreadPrivateDTOR destructor)
michael@0 172 {
michael@0 173 /*
michael@0 174 * We only call PR_NewThreadPrivateIndex from the main thread, so there's no
michael@0 175 * need to lock the table of TLS keys.
michael@0 176 */
michael@0 177 JS_ASSERT(PR_GetCurrentThread() == &gMainThread);
michael@0 178
michael@0 179 pthread_key_t key;
michael@0 180 if (pthread_key_create(&key, destructor))
michael@0 181 return PR_FAILURE;
michael@0 182
michael@0 183 JS_ASSERT(gTLSKeyCount + 1 < MaxTLSKeyCount);
michael@0 184
michael@0 185 gTLSKeys[gTLSKeyCount] = key;
michael@0 186 *newIndex = gTLSKeyCount;
michael@0 187 gTLSKeyCount++;
michael@0 188
michael@0 189 return PR_SUCCESS;
michael@0 190 }
michael@0 191
michael@0 192 PRStatus
michael@0 193 PR_SetThreadPrivate(unsigned index, void *priv)
michael@0 194 {
michael@0 195 if (index >= gTLSKeyCount)
michael@0 196 return PR_FAILURE;
michael@0 197 if (pthread_setspecific(gTLSKeys[index], priv))
michael@0 198 return PR_FAILURE;
michael@0 199 return PR_SUCCESS;
michael@0 200 }
michael@0 201
michael@0 202 void *
michael@0 203 PR_GetThreadPrivate(unsigned index)
michael@0 204 {
michael@0 205 if (index >= gTLSKeyCount)
michael@0 206 return nullptr;
michael@0 207 return pthread_getspecific(gTLSKeys[index]);
michael@0 208 }
michael@0 209
michael@0 210 PRStatus
michael@0 211 PR_CallOnce(PRCallOnceType *once, PRCallOnceFN func)
michael@0 212 {
michael@0 213 MOZ_ASSUME_UNREACHABLE("PR_CallOnce unimplemented");
michael@0 214 }
michael@0 215
michael@0 216 PRStatus
michael@0 217 PR_CallOnceWithArg(PRCallOnceType *once, PRCallOnceWithArgFN func, void *arg)
michael@0 218 {
michael@0 219 MOZ_ASSUME_UNREACHABLE("PR_CallOnceWithArg unimplemented");
michael@0 220 }
michael@0 221
michael@0 222 class nspr::Lock
michael@0 223 {
michael@0 224 pthread_mutex_t mutex_;
michael@0 225
michael@0 226 public:
michael@0 227 Lock() {}
michael@0 228 pthread_mutex_t &mutex() { return mutex_; }
michael@0 229 };
michael@0 230
michael@0 231 PRLock *
michael@0 232 PR_NewLock()
michael@0 233 {
michael@0 234 nspr::Lock *lock = js_new<nspr::Lock>();
michael@0 235 if (!lock)
michael@0 236 return nullptr;
michael@0 237
michael@0 238 if (pthread_mutex_init(&lock->mutex(), nullptr)) {
michael@0 239 js_delete(lock);
michael@0 240 return nullptr;
michael@0 241 }
michael@0 242
michael@0 243 return lock;
michael@0 244 }
michael@0 245
michael@0 246 void
michael@0 247 PR_DestroyLock(PRLock *lock)
michael@0 248 {
michael@0 249 pthread_mutex_destroy(&lock->mutex());
michael@0 250 js_delete(lock);
michael@0 251 }
michael@0 252
michael@0 253 void
michael@0 254 PR_Lock(PRLock *lock)
michael@0 255 {
michael@0 256 pthread_mutex_lock(&lock->mutex());
michael@0 257 }
michael@0 258
michael@0 259 PRStatus
michael@0 260 PR_Unlock(PRLock *lock)
michael@0 261 {
michael@0 262 if (pthread_mutex_unlock(&lock->mutex()))
michael@0 263 return PR_FAILURE;
michael@0 264 return PR_SUCCESS;
michael@0 265 }
michael@0 266
michael@0 267 class nspr::CondVar
michael@0 268 {
michael@0 269 pthread_cond_t cond_;
michael@0 270 nspr::Lock *lock_;
michael@0 271
michael@0 272 public:
michael@0 273 CondVar(nspr::Lock *lock) : lock_(lock) {}
michael@0 274 pthread_cond_t &cond() { return cond_; }
michael@0 275 nspr::Lock *lock() { return lock_; }
michael@0 276 };
michael@0 277
michael@0 278 PRCondVar *
michael@0 279 PR_NewCondVar(PRLock *lock)
michael@0 280 {
michael@0 281 nspr::CondVar *cvar = js_new<nspr::CondVar>(lock);
michael@0 282 if (!cvar)
michael@0 283 return nullptr;
michael@0 284
michael@0 285 if (pthread_cond_init(&cvar->cond(), nullptr)) {
michael@0 286 js_delete(cvar);
michael@0 287 return nullptr;
michael@0 288 }
michael@0 289
michael@0 290 return cvar;
michael@0 291 }
michael@0 292
michael@0 293 void
michael@0 294 PR_DestroyCondVar(PRCondVar *cvar)
michael@0 295 {
michael@0 296 pthread_cond_destroy(&cvar->cond());
michael@0 297 js_delete(cvar);
michael@0 298 }
michael@0 299
michael@0 300 PRStatus
michael@0 301 PR_NotifyCondVar(PRCondVar *cvar)
michael@0 302 {
michael@0 303 if (pthread_cond_signal(&cvar->cond()))
michael@0 304 return PR_FAILURE;
michael@0 305 return PR_SUCCESS;
michael@0 306 }
michael@0 307
michael@0 308 PRStatus
michael@0 309 PR_NotifyAllCondVar(PRCondVar *cvar)
michael@0 310 {
michael@0 311 if (pthread_cond_broadcast(&cvar->cond()))
michael@0 312 return PR_FAILURE;
michael@0 313 return PR_SUCCESS;
michael@0 314 }
michael@0 315
michael@0 316 uint32_t
michael@0 317 PR_MillisecondsToInterval(uint32_t milli)
michael@0 318 {
michael@0 319 return milli;
michael@0 320 }
michael@0 321
michael@0 322 static const uint64_t TicksPerSecond = 1000;
michael@0 323 static const uint64_t NanoSecondsInSeconds = 1000000000;
michael@0 324 static const uint64_t MicroSecondsInSeconds = 1000000;
michael@0 325
michael@0 326 uint32_t
michael@0 327 PR_TicksPerSecond()
michael@0 328 {
michael@0 329 return TicksPerSecond;
michael@0 330 }
michael@0 331
michael@0 332 PRStatus
michael@0 333 PR_WaitCondVar(PRCondVar *cvar, uint32_t timeout)
michael@0 334 {
michael@0 335 if (timeout == PR_INTERVAL_NO_TIMEOUT) {
michael@0 336 if (pthread_cond_wait(&cvar->cond(), &cvar->lock()->mutex()))
michael@0 337 return PR_FAILURE;
michael@0 338 return PR_SUCCESS;
michael@0 339 } else {
michael@0 340 struct timespec ts;
michael@0 341 struct timeval tv;
michael@0 342
michael@0 343 gettimeofday(&tv, nullptr);
michael@0 344 ts.tv_sec = tv.tv_sec;
michael@0 345 ts.tv_nsec = tv.tv_usec * (NanoSecondsInSeconds / MicroSecondsInSeconds);
michael@0 346
michael@0 347 ts.tv_nsec += timeout * (NanoSecondsInSeconds / TicksPerSecond);
michael@0 348 while (uint64_t(ts.tv_nsec) >= NanoSecondsInSeconds) {
michael@0 349 ts.tv_nsec -= NanoSecondsInSeconds;
michael@0 350 ts.tv_sec++;
michael@0 351 }
michael@0 352
michael@0 353 int result = pthread_cond_timedwait(&cvar->cond(), &cvar->lock()->mutex(), &ts);
michael@0 354 if (result == 0 || result == ETIMEDOUT)
michael@0 355 return PR_SUCCESS;
michael@0 356 return PR_FAILURE;
michael@0 357 }
michael@0 358 }
michael@0 359
michael@0 360 #endif /* JS_POSIX_NSPR */

mercurial