Line data Source code
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/. */
6 :
7 : #ifndef mozilla_CondVar_h
8 : #define mozilla_CondVar_h
9 :
10 : #include "mozilla/BlockingResourceBase.h"
11 : #include "mozilla/PlatformConditionVariable.h"
12 : #include "mozilla/Mutex.h"
13 :
14 : #ifdef MOZILLA_INTERNAL_API
15 : #include "GeckoProfiler.h"
16 : #endif //MOZILLA_INTERNAL_API
17 :
18 : namespace mozilla {
19 :
20 :
21 : /**
22 : * CondVar
23 : * Vanilla condition variable. Please don't use this unless you have a
24 : * compelling reason --- Monitor provides a simpler API.
25 : */
26 : class CondVar : BlockingResourceBase
27 : {
28 : public:
29 : /**
30 : * CondVar
31 : *
32 : * The CALLER owns |aLock|.
33 : *
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 332 : CondVar(Mutex& aLock, const char* aName)
41 332 : : BlockingResourceBase(aName, eCondVar)
42 332 : , mLock(&aLock)
43 : {
44 332 : MOZ_COUNT_CTOR(CondVar);
45 332 : }
46 :
47 : /**
48 : * ~CondVar
49 : * Clean up after this CondVar, but NOT its associated Mutex.
50 : **/
51 18 : ~CondVar()
52 18 : {
53 18 : MOZ_COUNT_DTOR(CondVar);
54 18 : }
55 :
56 : #ifndef DEBUG
57 : /**
58 : * Wait
59 : * @see prcvar.h
60 : **/
61 : nsresult Wait(PRIntervalTime aInterval = PR_INTERVAL_NO_TIMEOUT)
62 : {
63 :
64 : #ifdef MOZILLA_INTERNAL_API
65 : AutoProfilerThreadSleep sleep;
66 : #endif //MOZILLA_INTERNAL_API
67 : if (aInterval == PR_INTERVAL_NO_TIMEOUT) {
68 : mImpl.wait(*mLock);
69 : } else {
70 : mImpl.wait_for(*mLock, TimeDuration::FromMilliseconds(double(aInterval)));
71 : }
72 : return NS_OK;
73 : }
74 : #else
75 : nsresult Wait(PRIntervalTime aInterval = PR_INTERVAL_NO_TIMEOUT);
76 : #endif // ifndef DEBUG
77 :
78 : /**
79 : * Notify
80 : * @see prcvar.h
81 : **/
82 2112 : nsresult Notify()
83 : {
84 2112 : mImpl.notify_one();
85 2112 : return NS_OK;
86 : }
87 :
88 : /**
89 : * NotifyAll
90 : * @see prcvar.h
91 : **/
92 437 : nsresult NotifyAll()
93 : {
94 437 : mImpl.notify_all();
95 437 : return NS_OK;
96 : }
97 :
98 : #ifdef DEBUG
99 : /**
100 : * AssertCurrentThreadOwnsMutex
101 : * @see Mutex::AssertCurrentThreadOwns
102 : **/
103 696 : void AssertCurrentThreadOwnsMutex()
104 : {
105 696 : mLock->AssertCurrentThreadOwns();
106 696 : }
107 :
108 : /**
109 : * AssertNotCurrentThreadOwnsMutex
110 : * @see Mutex::AssertNotCurrentThreadOwns
111 : **/
112 : void AssertNotCurrentThreadOwnsMutex()
113 : {
114 : mLock->AssertNotCurrentThreadOwns();
115 : }
116 :
117 : #else
118 : void AssertCurrentThreadOwnsMutex() {}
119 : void AssertNotCurrentThreadOwnsMutex() {}
120 :
121 : #endif // ifdef DEBUG
122 :
123 : private:
124 : CondVar();
125 : CondVar(const CondVar&) = delete;
126 : CondVar& operator=(const CondVar&) = delete;
127 :
128 : Mutex* mLock;
129 : detail::ConditionVariableImpl mImpl;
130 : };
131 :
132 :
133 : } // namespace mozilla
134 :
135 :
136 : #endif // ifndef mozilla_CondVar_h
|