js/src/vm/Monitor.h

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 #ifndef vm_Monitor_h
michael@0 8 #define vm_Monitor_h
michael@0 9
michael@0 10 #ifdef JS_THREADSAFE
michael@0 11 #include "mozilla/DebugOnly.h"
michael@0 12 #endif
michael@0 13
michael@0 14 #include <stddef.h>
michael@0 15
michael@0 16 #include "jslock.h"
michael@0 17
michael@0 18 #include "js/Utility.h"
michael@0 19
michael@0 20 namespace js {
michael@0 21
michael@0 22 // A base class used for types intended to be used in a parallel
michael@0 23 // fashion, such as the workers in the |ThreadPool| class. Combines a
michael@0 24 // lock and a condition variable. You can acquire the lock or signal
michael@0 25 // the condition variable using the |AutoLockMonitor| type.
michael@0 26
michael@0 27 class Monitor
michael@0 28 {
michael@0 29 protected:
michael@0 30 friend class AutoLockMonitor;
michael@0 31 friend class AutoUnlockMonitor;
michael@0 32
michael@0 33 PRLock *lock_;
michael@0 34 PRCondVar *condVar_;
michael@0 35
michael@0 36 public:
michael@0 37 Monitor()
michael@0 38 : lock_(nullptr),
michael@0 39 condVar_(nullptr)
michael@0 40 { }
michael@0 41
michael@0 42 ~Monitor() {
michael@0 43 #ifdef JS_THREADSAFE
michael@0 44 if (lock_)
michael@0 45 PR_DestroyLock(lock_);
michael@0 46 if (condVar_)
michael@0 47 PR_DestroyCondVar(condVar_);
michael@0 48 #endif
michael@0 49 }
michael@0 50
michael@0 51 bool init();
michael@0 52 };
michael@0 53
michael@0 54 class AutoLockMonitor
michael@0 55 {
michael@0 56 private:
michael@0 57 #ifdef JS_THREADSAFE
michael@0 58 Monitor &monitor;
michael@0 59 #endif
michael@0 60
michael@0 61 public:
michael@0 62 AutoLockMonitor(Monitor &monitor)
michael@0 63 #ifdef JS_THREADSAFE
michael@0 64 : monitor(monitor)
michael@0 65 {
michael@0 66 PR_Lock(monitor.lock_);
michael@0 67 }
michael@0 68 #else
michael@0 69 {}
michael@0 70 #endif
michael@0 71
michael@0 72 ~AutoLockMonitor() {
michael@0 73 #ifdef JS_THREADSAFE
michael@0 74 PR_Unlock(monitor.lock_);
michael@0 75 #endif
michael@0 76 }
michael@0 77
michael@0 78 bool isFor(Monitor &other) const {
michael@0 79 #ifdef JS_THREADSAFE
michael@0 80 return monitor.lock_ == other.lock_;
michael@0 81 #else
michael@0 82 return true;
michael@0 83 #endif
michael@0 84 }
michael@0 85
michael@0 86 void wait(PRCondVar *condVar) {
michael@0 87 #ifdef JS_THREADSAFE
michael@0 88 mozilla::DebugOnly<PRStatus> status =
michael@0 89 PR_WaitCondVar(condVar, PR_INTERVAL_NO_TIMEOUT);
michael@0 90 MOZ_ASSERT(status == PR_SUCCESS);
michael@0 91 #endif
michael@0 92 }
michael@0 93
michael@0 94 void wait() {
michael@0 95 #ifdef JS_THREADSAFE
michael@0 96 wait(monitor.condVar_);
michael@0 97 #endif
michael@0 98 }
michael@0 99
michael@0 100 void notify(PRCondVar *condVar) {
michael@0 101 #ifdef JS_THREADSAFE
michael@0 102 mozilla::DebugOnly<PRStatus> status = PR_NotifyCondVar(condVar);
michael@0 103 MOZ_ASSERT(status == PR_SUCCESS);
michael@0 104 #endif
michael@0 105 }
michael@0 106
michael@0 107 void notify() {
michael@0 108 #ifdef JS_THREADSAFE
michael@0 109 notify(monitor.condVar_);
michael@0 110 #endif
michael@0 111 }
michael@0 112
michael@0 113 void notifyAll(PRCondVar *condVar) {
michael@0 114 #ifdef JS_THREADSAFE
michael@0 115 mozilla::DebugOnly<PRStatus> status = PR_NotifyAllCondVar(monitor.condVar_);
michael@0 116 MOZ_ASSERT(status == PR_SUCCESS);
michael@0 117 #endif
michael@0 118 }
michael@0 119
michael@0 120 void notifyAll() {
michael@0 121 #ifdef JS_THREADSAFE
michael@0 122 notifyAll(monitor.condVar_);
michael@0 123 #endif
michael@0 124 }
michael@0 125 };
michael@0 126
michael@0 127 class AutoUnlockMonitor
michael@0 128 {
michael@0 129 private:
michael@0 130 #ifdef JS_THREADSAFE
michael@0 131 Monitor &monitor;
michael@0 132 #endif
michael@0 133
michael@0 134 public:
michael@0 135 AutoUnlockMonitor(Monitor &monitor)
michael@0 136 #ifdef JS_THREADSAFE
michael@0 137 : monitor(monitor)
michael@0 138 {
michael@0 139 PR_Unlock(monitor.lock_);
michael@0 140 }
michael@0 141 #else
michael@0 142 {}
michael@0 143 #endif
michael@0 144
michael@0 145 ~AutoUnlockMonitor() {
michael@0 146 #ifdef JS_THREADSAFE
michael@0 147 PR_Lock(monitor.lock_);
michael@0 148 #endif
michael@0 149 }
michael@0 150
michael@0 151 bool isFor(Monitor &other) const {
michael@0 152 #ifdef JS_THREADSAFE
michael@0 153 return monitor.lock_ == other.lock_;
michael@0 154 #else
michael@0 155 return true;
michael@0 156 #endif
michael@0 157 }
michael@0 158 };
michael@0 159
michael@0 160 } // namespace js
michael@0 161
michael@0 162 #endif /* vm_Monitor_h */

mercurial