xpcom/glue/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: 2 -*-
michael@0 2 * vim: sw=2 ts=8 et :
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #ifndef mozilla_Monitor_h
michael@0 9 #define mozilla_Monitor_h
michael@0 10
michael@0 11 #include "mozilla/CondVar.h"
michael@0 12 #include "mozilla/Mutex.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15
michael@0 16 /**
michael@0 17 * Monitor provides a *non*-reentrant monitor: *not* a Java-style
michael@0 18 * monitor. If your code needs support for reentrancy, use
michael@0 19 * ReentrantMonitor instead. (Rarely should reentrancy be needed.)
michael@0 20 *
michael@0 21 * Instead of directly calling Monitor methods, it's safer and simpler
michael@0 22 * to instead use the RAII wrappers MonitorAutoLock and
michael@0 23 * MonitorAutoUnlock.
michael@0 24 */
michael@0 25 class NS_COM_GLUE Monitor
michael@0 26 {
michael@0 27 public:
michael@0 28 Monitor(const char* aName) :
michael@0 29 mMutex(aName),
michael@0 30 mCondVar(mMutex, "[Monitor.mCondVar]")
michael@0 31 {}
michael@0 32
michael@0 33 ~Monitor() {}
michael@0 34
michael@0 35 void Lock()
michael@0 36 {
michael@0 37 mMutex.Lock();
michael@0 38 }
michael@0 39
michael@0 40 void Unlock()
michael@0 41 {
michael@0 42 mMutex.Unlock();
michael@0 43 }
michael@0 44
michael@0 45 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
michael@0 46 {
michael@0 47 return mCondVar.Wait(interval);
michael@0 48 }
michael@0 49
michael@0 50 nsresult Notify()
michael@0 51 {
michael@0 52 return mCondVar.Notify();
michael@0 53 }
michael@0 54
michael@0 55 nsresult NotifyAll()
michael@0 56 {
michael@0 57 return mCondVar.NotifyAll();
michael@0 58 }
michael@0 59
michael@0 60 void AssertCurrentThreadOwns() const
michael@0 61 {
michael@0 62 mMutex.AssertCurrentThreadOwns();
michael@0 63 }
michael@0 64
michael@0 65 void AssertNotCurrentThreadOwns() const
michael@0 66 {
michael@0 67 mMutex.AssertNotCurrentThreadOwns();
michael@0 68 }
michael@0 69
michael@0 70 private:
michael@0 71 Monitor();
michael@0 72 Monitor(const Monitor&);
michael@0 73 Monitor& operator =(const Monitor&);
michael@0 74
michael@0 75 Mutex mMutex;
michael@0 76 CondVar mCondVar;
michael@0 77 };
michael@0 78
michael@0 79 /**
michael@0 80 * Lock the monitor for the lexical scope instances of this class are
michael@0 81 * bound to (except for MonitorAutoUnlock in nested scopes).
michael@0 82 *
michael@0 83 * The monitor must be unlocked when instances of this class are
michael@0 84 * created.
michael@0 85 */
michael@0 86 class NS_COM_GLUE MOZ_STACK_CLASS MonitorAutoLock
michael@0 87 {
michael@0 88 public:
michael@0 89 MonitorAutoLock(Monitor& aMonitor) :
michael@0 90 mMonitor(&aMonitor)
michael@0 91 {
michael@0 92 mMonitor->Lock();
michael@0 93 }
michael@0 94
michael@0 95 ~MonitorAutoLock()
michael@0 96 {
michael@0 97 mMonitor->Unlock();
michael@0 98 }
michael@0 99
michael@0 100 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
michael@0 101 {
michael@0 102 return mMonitor->Wait(interval);
michael@0 103 }
michael@0 104
michael@0 105 nsresult Notify()
michael@0 106 {
michael@0 107 return mMonitor->Notify();
michael@0 108 }
michael@0 109
michael@0 110 nsresult NotifyAll()
michael@0 111 {
michael@0 112 return mMonitor->NotifyAll();
michael@0 113 }
michael@0 114
michael@0 115 private:
michael@0 116 MonitorAutoLock();
michael@0 117 MonitorAutoLock(const MonitorAutoLock&);
michael@0 118 MonitorAutoLock& operator =(const MonitorAutoLock&);
michael@0 119 static void* operator new(size_t) CPP_THROW_NEW;
michael@0 120 static void operator delete(void*);
michael@0 121
michael@0 122 Monitor* mMonitor;
michael@0 123 };
michael@0 124
michael@0 125 /**
michael@0 126 * Unlock the monitor for the lexical scope instances of this class
michael@0 127 * are bound to (except for MonitorAutoLock in nested scopes).
michael@0 128 *
michael@0 129 * The monitor must be locked by the current thread when instances of
michael@0 130 * this class are created.
michael@0 131 */
michael@0 132 class NS_COM_GLUE MOZ_STACK_CLASS MonitorAutoUnlock
michael@0 133 {
michael@0 134 public:
michael@0 135 MonitorAutoUnlock(Monitor& aMonitor) :
michael@0 136 mMonitor(&aMonitor)
michael@0 137 {
michael@0 138 mMonitor->Unlock();
michael@0 139 }
michael@0 140
michael@0 141 ~MonitorAutoUnlock()
michael@0 142 {
michael@0 143 mMonitor->Lock();
michael@0 144 }
michael@0 145
michael@0 146 private:
michael@0 147 MonitorAutoUnlock();
michael@0 148 MonitorAutoUnlock(const MonitorAutoUnlock&);
michael@0 149 MonitorAutoUnlock& operator =(const MonitorAutoUnlock&);
michael@0 150 static void* operator new(size_t) CPP_THROW_NEW;
michael@0 151 static void operator delete(void*);
michael@0 152
michael@0 153 Monitor* mMonitor;
michael@0 154 };
michael@0 155
michael@0 156 } // namespace mozilla
michael@0 157
michael@0 158 #endif // mozilla_Monitor_h

mercurial