AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
ConditionVariableWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_THREADING_CONDITIONVARIABLEWRAPPER_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_THREADING_CONDITIONVARIABLEWRAPPER_H_
18 
19 #include <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <unordered_set>
23 #include <thread>
24 
28 
29 namespace alexaClientSDK {
30 namespace avsCommon {
31 namespace utils {
32 namespace threading {
33 
41 public:
45  inline static std::string getTag() {
46  return "ConditionVariableWrapper";
47  }
48 
51 
55  void notifyOne();
56 
60  void notifyAll();
61 
74  template <typename Predicate>
75  void wait(std::unique_lock<std::mutex>& lock, Predicate pred);
76 
93  template <typename Rep, typename Period, typename Predicate>
94  bool waitFor(
95  std::unique_lock<std::mutex>& lock,
96  const std::chrono::duration<Rep, Period>& rel_time,
97  Predicate pred);
98 
116  template <typename Clock, typename Duration, typename Predicate>
117  bool waitUntil(
118  std::unique_lock<std::mutex>& lock,
119  const std::chrono::time_point<Clock, Duration>& timeout_time,
120  Predicate pred);
121 
122 private:
129  void waitInner(std::unique_lock<std::mutex>& lock, std::function<bool()> pred);
130 
138  bool waitForInner(std::unique_lock<std::mutex>& lock, std::chrono::nanoseconds relTime, std::function<bool()> pred);
139 
141  static std::atomic<uint64_t> g_id;
142 
144  const uint64_t m_id;
145 
147  std::condition_variable m_cv;
148 
153  std::mutex m_mutex;
154 
159  std::shared_ptr<power::PowerResource> m_notifyOnePowerResource;
160 
165  uint64_t m_notifyOnePowerResourceRefCount;
166 
168  std::unordered_set<std::shared_ptr<power::PowerResource>> m_frozenResources;
169 };
170 
171 template <typename Predicate>
172 void ConditionVariableWrapper::wait(std::unique_lock<std::mutex>& lock, Predicate pred) {
173  waitInner(lock, pred);
174 }
175 
176 template <typename Rep, typename Period, typename Predicate>
178  std::unique_lock<std::mutex>& lock,
179  const std::chrono::duration<Rep, Period>& rel_time,
180  Predicate pred) {
181  return waitForInner(lock, rel_time, pred);
182 };
183 
184 template <typename Clock, typename Duration, typename Predicate>
186  std::unique_lock<std::mutex>& lock,
187  const std::chrono::time_point<Clock, Duration>& timeout_time,
188  Predicate pred) {
189  return waitForInner(lock, timeout_time - Clock::now(), pred);
190 };
191 
192 } // namespace threading
193 } // namespace utils
194 } // namespace avsCommon
195 } // namespace alexaClientSDK
196 
197 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_THREADING_CONDITIONVARIABLEWRAPPER_H_
void wait(std::unique_lock< std::mutex > &lock, Predicate pred)
Definition: ConditionVariableWrapper.h:172
bool waitFor(std::unique_lock< std::mutex > &lock, const std::chrono::duration< Rep, Period > &rel_time, Predicate pred)
Definition: ConditionVariableWrapper.h:177
::std::string string
Definition: gtest-port.h:1097
bool waitUntil(std::unique_lock< std::mutex > &lock, const std::chrono::time_point< Clock, Duration > &timeout_time, Predicate pred)
Definition: ConditionVariableWrapper.h:185
static std::string getTag()
Definition: ConditionVariableWrapper.h:45
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0