AlexaClientSDK  1.26.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
MultiTimer.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_TIMING_MULTITIMER_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_TIMING_MULTITIMER_H_
18 
19 #include <cstdint>
20 #include <chrono>
21 #include <condition_variable>
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 
28 
29 namespace alexaClientSDK {
30 namespace avsCommon {
31 namespace utils {
32 namespace timing {
33 
39 class MultiTimer {
40 public:
42  using Token = uint64_t;
43 
48  static std::shared_ptr<MultiTimer> createMultiTimer();
49 
53  MultiTimer();
54 
58  ~MultiTimer();
59 
69  Token submitTask(const std::chrono::milliseconds& delay, std::function<void()> task);
70 
76  void cancelTask(Token token);
77 
78 private:
84  bool executeTimer();
85 
92  bool hasNextLocked(std::unique_lock<std::mutex>& lock);
93 
95  using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
96 
98  std::condition_variable m_waitCondition;
99 
101  std::mutex m_waitMutex;
102 
104  threading::TaskThread m_timerThread;
105 
107  std::multimap<TimePoint, Token> m_timers;
108 
110  std::map<Token, std::pair<TimePoint, std::function<void()>>> m_tasks;
111 
113  bool m_isRunning;
114 
116  bool m_isBeingDestroyed;
117 
119  Token m_nextToken;
120 };
121 
122 } // namespace timing
123 } // namespace utils
124 } // namespace avsCommon
125 } // namespace alexaClientSDK
126 
127 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_TIMING_MULTITIMER_H_
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void cancelTask(Token token)
Definition: MultiTimer.cpp:83
uint64_t Token
Alias for the token used to identify a task. This can be used to cancel a task execution.
Definition: MultiTimer.h:42
Token submitTask(const std::chrono::milliseconds &delay, std::function< void()> task)
Definition: MultiTimer.cpp:60
static std::shared_ptr< MultiTimer > createMultiTimer()
Definition: MultiTimer.cpp:40

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