1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_CondVar_h
8 #define mozilla_CondVar_h
10 #include "mozilla/BlockingResourceBase.h"
11 #include "mozilla/PlatformConditionVariable.h"
12 #include "mozilla/Mutex.h"
13 #include "mozilla/TimeStamp.h"
15 #if defined(MOZILLA_INTERNAL_API) && !defined(DEBUG)
16 # include "GeckoProfiler.h"
17 #endif // defined( MOZILLA_INTERNAL_API) && !defined(DEBUG)
22 * Similarly to OffTheBooksMutex, OffTheBooksCondvar is identical to CondVar,
23 * except that OffTheBooksCondVar doesn't include leak checking. Sometimes
24 * you want to intentionally "leak" a CondVar until shutdown; in these cases,
25 * OffTheBooksCondVar is for you.
27 class OffTheBooksCondVar
: BlockingResourceBase
{
32 * The CALLER owns |aLock|.
34 * @param aLock A Mutex to associate with this condition variable.
35 * @param aName A name which can reference this monitor
36 * @returns If failure, nullptr.
37 * If success, a valid Monitor* which must be destroyed
38 * by Monitor::DestroyMonitor()
40 OffTheBooksCondVar(OffTheBooksMutex
& aLock
, const char* aName
)
41 : BlockingResourceBase(aName
, eCondVar
), mLock(&aLock
) {}
45 * Clean up after this OffTheBooksCondVar, but NOT its associated Mutex.
47 ~OffTheBooksCondVar() = default;
55 # ifdef MOZILLA_INTERNAL_API
56 AUTO_PROFILER_THREAD_SLEEP
;
57 # endif // MOZILLA_INTERNAL_API
61 CVStatus
Wait(TimeDuration aDuration
) {
62 # ifdef MOZILLA_INTERNAL_API
63 AUTO_PROFILER_THREAD_SLEEP
;
64 # endif // MOZILLA_INTERNAL_API
65 return mImpl
.wait_for(*mLock
, aDuration
);
68 // NOTE: debug impl is in BlockingResourceBase.cpp
70 CVStatus
Wait(TimeDuration aDuration
);
77 void Notify() { mImpl
.notify_one(); }
83 void NotifyAll() { mImpl
.notify_all(); }
87 * AssertCurrentThreadOwnsMutex
88 * @see Mutex::AssertCurrentThreadOwns
90 void AssertCurrentThreadOwnsMutex() { mLock
->AssertCurrentThreadOwns(); }
93 * AssertNotCurrentThreadOwnsMutex
94 * @see Mutex::AssertNotCurrentThreadOwns
96 void AssertNotCurrentThreadOwnsMutex() {
97 mLock
->AssertNotCurrentThreadOwns();
101 void AssertCurrentThreadOwnsMutex() {}
102 void AssertNotCurrentThreadOwnsMutex() {}
104 #endif // ifdef DEBUG
107 OffTheBooksCondVar();
108 OffTheBooksCondVar(const OffTheBooksCondVar
&) = delete;
109 OffTheBooksCondVar
& operator=(const OffTheBooksCondVar
&) = delete;
111 OffTheBooksMutex
* mLock
;
112 detail::ConditionVariableImpl mImpl
;
117 * Vanilla condition variable. Please don't use this unless you have a
118 * compelling reason --- Monitor provides a simpler API.
120 class CondVar
: public OffTheBooksCondVar
{
122 CondVar(OffTheBooksMutex
& aLock
, const char* aName
)
123 : OffTheBooksCondVar(aLock
, aName
) {
124 MOZ_COUNT_CTOR(CondVar
);
127 MOZ_COUNTED_DTOR(CondVar
)
131 CondVar(const CondVar
&);
132 CondVar
& operator=(const CondVar
&);
135 } // namespace mozilla
137 #endif // ifndef mozilla_CondVar_h