xpcom/glue/Mutex.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: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: sw=4 ts=4 et :
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 mozilla_Mutex_h
michael@0 8 #define mozilla_Mutex_h
michael@0 9
michael@0 10 #include "prlock.h"
michael@0 11
michael@0 12 #include "mozilla/BlockingResourceBase.h"
michael@0 13 #include "mozilla/GuardObjects.h"
michael@0 14
michael@0 15 //
michael@0 16 // Provides:
michael@0 17 //
michael@0 18 // - Mutex, a non-recursive mutex
michael@0 19 // - MutexAutoLock, an RAII class for ensuring that Mutexes are properly
michael@0 20 // locked and unlocked
michael@0 21 // - MutexAutoUnlock, complementary sibling to MutexAutoLock
michael@0 22 //
michael@0 23 // - OffTheBooksMutex, a non-recursive mutex that doesn't do leak checking
michael@0 24 // - OffTheBooksMutexAuto{Lock,Unlock} - Like MutexAuto{Lock,Unlock}, but for
michael@0 25 // an OffTheBooksMutex.
michael@0 26 //
michael@0 27 // Using MutexAutoLock/MutexAutoUnlock etc. is MUCH preferred to making bare
michael@0 28 // calls to Lock and Unlock.
michael@0 29 //
michael@0 30 namespace mozilla {
michael@0 31
michael@0 32 /**
michael@0 33 * OffTheBooksMutex is identical to Mutex, except that OffTheBooksMutex doesn't
michael@0 34 * include leak checking. Sometimes you want to intentionally "leak" a mutex
michael@0 35 * until shutdown; in these cases, OffTheBooksMutex is for you.
michael@0 36 */
michael@0 37 class NS_COM_GLUE OffTheBooksMutex : BlockingResourceBase
michael@0 38 {
michael@0 39 public:
michael@0 40 /**
michael@0 41 * @param name A name which can reference this lock
michael@0 42 * @returns If failure, nullptr
michael@0 43 * If success, a valid Mutex* which must be destroyed
michael@0 44 * by Mutex::DestroyMutex()
michael@0 45 **/
michael@0 46 OffTheBooksMutex(const char* name) :
michael@0 47 BlockingResourceBase(name, eMutex)
michael@0 48 {
michael@0 49 mLock = PR_NewLock();
michael@0 50 if (!mLock)
michael@0 51 NS_RUNTIMEABORT("Can't allocate mozilla::Mutex");
michael@0 52 }
michael@0 53
michael@0 54 ~OffTheBooksMutex()
michael@0 55 {
michael@0 56 NS_ASSERTION(mLock,
michael@0 57 "improperly constructed Lock or double free");
michael@0 58 // NSPR does consistency checks for us
michael@0 59 PR_DestroyLock(mLock);
michael@0 60 mLock = 0;
michael@0 61 }
michael@0 62
michael@0 63 #ifndef DEBUG
michael@0 64 /**
michael@0 65 * Lock
michael@0 66 * @see prlock.h
michael@0 67 **/
michael@0 68 void Lock()
michael@0 69 {
michael@0 70 PR_Lock(mLock);
michael@0 71 }
michael@0 72
michael@0 73 /**
michael@0 74 * Unlock
michael@0 75 * @see prlock.h
michael@0 76 **/
michael@0 77 void Unlock()
michael@0 78 {
michael@0 79 PR_Unlock(mLock);
michael@0 80 }
michael@0 81
michael@0 82 /**
michael@0 83 * AssertCurrentThreadOwns
michael@0 84 * @see prlock.h
michael@0 85 **/
michael@0 86 void AssertCurrentThreadOwns () const
michael@0 87 {
michael@0 88 }
michael@0 89
michael@0 90 /**
michael@0 91 * AssertNotCurrentThreadOwns
michael@0 92 * @see prlock.h
michael@0 93 **/
michael@0 94 void AssertNotCurrentThreadOwns () const
michael@0 95 {
michael@0 96 }
michael@0 97
michael@0 98 #else
michael@0 99 void Lock();
michael@0 100 void Unlock();
michael@0 101
michael@0 102 void AssertCurrentThreadOwns () const
michael@0 103 {
michael@0 104 PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mLock);
michael@0 105 }
michael@0 106
michael@0 107 void AssertNotCurrentThreadOwns () const
michael@0 108 {
michael@0 109 // FIXME bug 476536
michael@0 110 }
michael@0 111
michael@0 112 #endif // ifndef DEBUG
michael@0 113
michael@0 114 private:
michael@0 115 OffTheBooksMutex();
michael@0 116 OffTheBooksMutex(const OffTheBooksMutex&);
michael@0 117 OffTheBooksMutex& operator=(const OffTheBooksMutex&);
michael@0 118
michael@0 119 PRLock* mLock;
michael@0 120
michael@0 121 friend class CondVar;
michael@0 122 };
michael@0 123
michael@0 124 /**
michael@0 125 * Mutex
michael@0 126 * When possible, use MutexAutoLock/MutexAutoUnlock to lock/unlock this
michael@0 127 * mutex within a scope, instead of calling Lock/Unlock directly.
michael@0 128 */
michael@0 129 class NS_COM_GLUE Mutex : public OffTheBooksMutex
michael@0 130 {
michael@0 131 public:
michael@0 132 Mutex(const char* name)
michael@0 133 : OffTheBooksMutex(name)
michael@0 134 {
michael@0 135 MOZ_COUNT_CTOR(Mutex);
michael@0 136 }
michael@0 137
michael@0 138 ~Mutex()
michael@0 139 {
michael@0 140 MOZ_COUNT_DTOR(Mutex);
michael@0 141 }
michael@0 142
michael@0 143 private:
michael@0 144 Mutex();
michael@0 145 Mutex(const Mutex&);
michael@0 146 Mutex& operator=(const Mutex&);
michael@0 147 };
michael@0 148
michael@0 149 /**
michael@0 150 * MutexAutoLock
michael@0 151 * Acquires the Mutex when it enters scope, and releases it when it leaves
michael@0 152 * scope.
michael@0 153 *
michael@0 154 * MUCH PREFERRED to bare calls to Mutex.Lock and Unlock.
michael@0 155 */
michael@0 156 template<typename T>
michael@0 157 class NS_COM_GLUE MOZ_STACK_CLASS BaseAutoLock
michael@0 158 {
michael@0 159 public:
michael@0 160 /**
michael@0 161 * Constructor
michael@0 162 * The constructor aquires the given lock. The destructor
michael@0 163 * releases the lock.
michael@0 164 *
michael@0 165 * @param aLock A valid mozilla::Mutex* returned by
michael@0 166 * mozilla::Mutex::NewMutex.
michael@0 167 **/
michael@0 168 BaseAutoLock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
michael@0 169 mLock(&aLock)
michael@0 170 {
michael@0 171 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 172 NS_ASSERTION(mLock, "null mutex");
michael@0 173 mLock->Lock();
michael@0 174 }
michael@0 175
michael@0 176 ~BaseAutoLock(void) {
michael@0 177 mLock->Unlock();
michael@0 178 }
michael@0 179
michael@0 180 private:
michael@0 181 BaseAutoLock();
michael@0 182 BaseAutoLock(BaseAutoLock&);
michael@0 183 BaseAutoLock& operator=(BaseAutoLock&);
michael@0 184 static void* operator new(size_t) CPP_THROW_NEW;
michael@0 185 static void operator delete(void*);
michael@0 186
michael@0 187 T* mLock;
michael@0 188 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
michael@0 189 };
michael@0 190
michael@0 191 typedef BaseAutoLock<Mutex> MutexAutoLock;
michael@0 192 typedef BaseAutoLock<OffTheBooksMutex> OffTheBooksMutexAutoLock;
michael@0 193
michael@0 194 /**
michael@0 195 * MutexAutoUnlock
michael@0 196 * Releases the Mutex when it enters scope, and re-acquires it when it leaves
michael@0 197 * scope.
michael@0 198 *
michael@0 199 * MUCH PREFERRED to bare calls to Mutex.Unlock and Lock.
michael@0 200 */
michael@0 201 template<typename T>
michael@0 202 class NS_COM_GLUE MOZ_STACK_CLASS BaseAutoUnlock
michael@0 203 {
michael@0 204 public:
michael@0 205 BaseAutoUnlock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
michael@0 206 mLock(&aLock)
michael@0 207 {
michael@0 208 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 209 NS_ASSERTION(mLock, "null lock");
michael@0 210 mLock->Unlock();
michael@0 211 }
michael@0 212
michael@0 213 ~BaseAutoUnlock()
michael@0 214 {
michael@0 215 mLock->Lock();
michael@0 216 }
michael@0 217
michael@0 218 private:
michael@0 219 BaseAutoUnlock();
michael@0 220 BaseAutoUnlock(BaseAutoUnlock&);
michael@0 221 BaseAutoUnlock& operator =(BaseAutoUnlock&);
michael@0 222 static void* operator new(size_t) CPP_THROW_NEW;
michael@0 223 static void operator delete(void*);
michael@0 224
michael@0 225 T* mLock;
michael@0 226 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
michael@0 227 };
michael@0 228
michael@0 229 typedef BaseAutoUnlock<Mutex> MutexAutoUnlock;
michael@0 230 typedef BaseAutoUnlock<OffTheBooksMutex> OffTheBooksMutexAutoUnlock;
michael@0 231
michael@0 232 } // namespace mozilla
michael@0 233
michael@0 234
michael@0 235 #endif // ifndef mozilla_Mutex_h

mercurial