AlexaClientSDK  3.0.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() noexcept;
49 
53  MultiTimer() noexcept;
54 
58  ~MultiTimer() noexcept;
59 
69  Token submitTask(const std::chrono::milliseconds& delay, std::function<void()> task) noexcept;
70 
76  void cancelTask(Token token) noexcept;
77 
78 private:
84  bool executeTimer() noexcept;
85 
92  bool hasNextLocked(std::unique_lock<std::mutex>& lock) noexcept;
93 
95  using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
96 
99  const std::string m_timerMoniker;
100 
102  std::condition_variable m_waitCondition;
103 
105  std::mutex m_waitMutex;
106 
108  threading::TaskThread m_timerThread;
109 
111  std::multimap<TimePoint, Token> m_timers;
112 
114  std::map<Token, std::pair<TimePoint, std::function<void()>>> m_tasks;
115 
117  bool m_isRunning;
118 
120  bool m_isBeingDestroyed;
121 
123  Token m_nextToken;
124 };
125 
126 } // namespace timing
127 } // namespace utils
128 } // namespace avsCommon
129 } // namespace alexaClientSDK
130 
131 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_TIMING_MULTITIMER_H_
Definition: CapabilityConfiguration.h:190
Token submitTask(const std::chrono::milliseconds &delay, std::function< void()> task) noexcept
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
const
Definition: upload.py:398
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
static std::shared_ptr< MultiTimer > createMultiTimer() noexcept

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