xpcom/glue/CondVar.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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_CondVar_h
michael@0 8 #define mozilla_CondVar_h
michael@0 9
michael@0 10 #include "prcvar.h"
michael@0 11
michael@0 12 #include "mozilla/BlockingResourceBase.h"
michael@0 13 #include "mozilla/Mutex.h"
michael@0 14
michael@0 15 #ifdef MOZILLA_INTERNAL_API
michael@0 16 #include "GeckoProfiler.h"
michael@0 17 #endif //MOZILLA_INTERNAL_API
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20
michael@0 21
michael@0 22 /**
michael@0 23 * CondVar
michael@0 24 * Vanilla condition variable. Please don't use this unless you have a
michael@0 25 * compelling reason --- Monitor provides a simpler API.
michael@0 26 */
michael@0 27 class NS_COM_GLUE CondVar : BlockingResourceBase
michael@0 28 {
michael@0 29 public:
michael@0 30 /**
michael@0 31 * CondVar
michael@0 32 *
michael@0 33 * The CALLER owns |lock|.
michael@0 34 *
michael@0 35 * @param aLock A Mutex to associate with this condition variable.
michael@0 36 * @param aName A name which can reference this monitor
michael@0 37 * @returns If failure, nullptr.
michael@0 38 * If success, a valid Monitor* which must be destroyed
michael@0 39 * by Monitor::DestroyMonitor()
michael@0 40 **/
michael@0 41 CondVar(Mutex& aLock, const char* aName) :
michael@0 42 BlockingResourceBase(aName, eCondVar),
michael@0 43 mLock(&aLock)
michael@0 44 {
michael@0 45 MOZ_COUNT_CTOR(CondVar);
michael@0 46 // |lock| must necessarily already be known to the deadlock detector
michael@0 47 mCvar = PR_NewCondVar(mLock->mLock);
michael@0 48 if (!mCvar)
michael@0 49 NS_RUNTIMEABORT("Can't allocate mozilla::CondVar");
michael@0 50 }
michael@0 51
michael@0 52 /**
michael@0 53 * ~CondVar
michael@0 54 * Clean up after this CondVar, but NOT its associated Mutex.
michael@0 55 **/
michael@0 56 ~CondVar()
michael@0 57 {
michael@0 58 NS_ASSERTION(mCvar && mLock,
michael@0 59 "improperly constructed CondVar or double free");
michael@0 60 PR_DestroyCondVar(mCvar);
michael@0 61 mCvar = 0;
michael@0 62 mLock = 0;
michael@0 63 MOZ_COUNT_DTOR(CondVar);
michael@0 64 }
michael@0 65
michael@0 66 #ifndef DEBUG
michael@0 67 /**
michael@0 68 * Wait
michael@0 69 * @see prcvar.h
michael@0 70 **/
michael@0 71 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
michael@0 72 {
michael@0 73
michael@0 74 #ifdef MOZILLA_INTERNAL_API
michael@0 75 GeckoProfilerSleepRAII profiler_sleep;
michael@0 76 #endif //MOZILLA_INTERNAL_API
michael@0 77 // NSPR checks for lock ownership
michael@0 78 return PR_WaitCondVar(mCvar, interval) == PR_SUCCESS
michael@0 79 ? NS_OK : NS_ERROR_FAILURE;
michael@0 80 }
michael@0 81 #else
michael@0 82 nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT);
michael@0 83 #endif // ifndef DEBUG
michael@0 84
michael@0 85 /**
michael@0 86 * Notify
michael@0 87 * @see prcvar.h
michael@0 88 **/
michael@0 89 nsresult Notify()
michael@0 90 {
michael@0 91 // NSPR checks for lock ownership
michael@0 92 return PR_NotifyCondVar(mCvar) == PR_SUCCESS
michael@0 93 ? NS_OK : NS_ERROR_FAILURE;
michael@0 94 }
michael@0 95
michael@0 96 /**
michael@0 97 * NotifyAll
michael@0 98 * @see prcvar.h
michael@0 99 **/
michael@0 100 nsresult NotifyAll()
michael@0 101 {
michael@0 102 // NSPR checks for lock ownership
michael@0 103 return PR_NotifyAllCondVar(mCvar) == PR_SUCCESS
michael@0 104 ? NS_OK : NS_ERROR_FAILURE;
michael@0 105 }
michael@0 106
michael@0 107 #ifdef DEBUG
michael@0 108 /**
michael@0 109 * AssertCurrentThreadOwnsMutex
michael@0 110 * @see Mutex::AssertCurrentThreadOwns
michael@0 111 **/
michael@0 112 void AssertCurrentThreadOwnsMutex()
michael@0 113 {
michael@0 114 mLock->AssertCurrentThreadOwns();
michael@0 115 }
michael@0 116
michael@0 117 /**
michael@0 118 * AssertNotCurrentThreadOwnsMutex
michael@0 119 * @see Mutex::AssertNotCurrentThreadOwns
michael@0 120 **/
michael@0 121 void AssertNotCurrentThreadOwnsMutex()
michael@0 122 {
michael@0 123 mLock->AssertNotCurrentThreadOwns();
michael@0 124 }
michael@0 125
michael@0 126 #else
michael@0 127 void AssertCurrentThreadOwnsMutex()
michael@0 128 {
michael@0 129 }
michael@0 130 void AssertNotCurrentThreadOwnsMutex()
michael@0 131 {
michael@0 132 }
michael@0 133
michael@0 134 #endif // ifdef DEBUG
michael@0 135
michael@0 136 private:
michael@0 137 CondVar();
michael@0 138 CondVar(CondVar&);
michael@0 139 CondVar& operator=(CondVar&);
michael@0 140
michael@0 141 Mutex* mLock;
michael@0 142 PRCondVar* mCvar;
michael@0 143 };
michael@0 144
michael@0 145
michael@0 146 } // namespace mozilla
michael@0 147
michael@0 148
michael@0 149 #endif // ifndef mozilla_CondVar_h

mercurial