Bug 1700051: part 49) Add some documentation to `Selection::GetRangesForInterval...
[gecko.git] / xpcom / threads / CondVar.h
blob890e1c21a0f301f22a234f70c685a7e9fb7c8a74
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)
19 namespace mozilla {
21 /**
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 {
28 public:
29 /**
30 * OffTheBooksCondVar
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()
39 **/
40 OffTheBooksCondVar(OffTheBooksMutex& aLock, const char* aName)
41 : BlockingResourceBase(aName, eCondVar), mLock(&aLock) {}
43 /**
44 * ~OffTheBooksCondVar
45 * Clean up after this OffTheBooksCondVar, but NOT its associated Mutex.
46 **/
47 ~OffTheBooksCondVar() = default;
49 /**
50 * Wait
51 * @see prcvar.h
52 **/
53 #ifndef DEBUG
54 void Wait() {
55 # ifdef MOZILLA_INTERNAL_API
56 AUTO_PROFILER_THREAD_SLEEP;
57 # endif // MOZILLA_INTERNAL_API
58 mImpl.wait(*mLock);
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);
67 #else
68 // NOTE: debug impl is in BlockingResourceBase.cpp
69 void Wait();
70 CVStatus Wait(TimeDuration aDuration);
71 #endif
73 /**
74 * Notify
75 * @see prcvar.h
76 **/
77 void Notify() { mImpl.notify_one(); }
79 /**
80 * NotifyAll
81 * @see prcvar.h
82 **/
83 void NotifyAll() { mImpl.notify_all(); }
85 #ifdef DEBUG
86 /**
87 * AssertCurrentThreadOwnsMutex
88 * @see Mutex::AssertCurrentThreadOwns
89 **/
90 void AssertCurrentThreadOwnsMutex() { mLock->AssertCurrentThreadOwns(); }
92 /**
93 * AssertNotCurrentThreadOwnsMutex
94 * @see Mutex::AssertNotCurrentThreadOwns
95 **/
96 void AssertNotCurrentThreadOwnsMutex() {
97 mLock->AssertNotCurrentThreadOwns();
100 #else
101 void AssertCurrentThreadOwnsMutex() {}
102 void AssertNotCurrentThreadOwnsMutex() {}
104 #endif // ifdef DEBUG
106 private:
107 OffTheBooksCondVar();
108 OffTheBooksCondVar(const OffTheBooksCondVar&) = delete;
109 OffTheBooksCondVar& operator=(const OffTheBooksCondVar&) = delete;
111 OffTheBooksMutex* mLock;
112 detail::ConditionVariableImpl mImpl;
116 * CondVar
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 {
121 public:
122 CondVar(OffTheBooksMutex& aLock, const char* aName)
123 : OffTheBooksCondVar(aLock, aName) {
124 MOZ_COUNT_CTOR(CondVar);
127 MOZ_COUNTED_DTOR(CondVar)
129 private:
130 CondVar();
131 CondVar(const CondVar&);
132 CondVar& operator=(const CondVar&);
135 } // namespace mozilla
137 #endif // ifndef mozilla_CondVar_h