1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "CppTimerManager.h"
17
18 #include <thread>
19
20 using namespace std;
21 std::map<std::thread::id, unique_ptr<CppTimerManager>> CppTimerManager::managers;
22
23 // Non-threaded security
GetTimerManager()24 CppTimerManager& CppTimerManager::GetTimerManager()
25 {
26 thread::id curThreadId = this_thread::get_id();
27 if (managers.find(curThreadId) == managers.end()) {
28 managers[curThreadId] = make_unique<CppTimerManager>();
29 }
30 return *managers[curThreadId];
31 }
32
AddCppTimer(CppTimer & timer)33 void CppTimerManager::AddCppTimer(CppTimer& timer)
34 {
35 runningTimers.push_back(&timer);
36 ILOG("CppTimerManager::AddCppTimer %x %x", this, &timer);
37 }
38
RemoveCppTimer(CppTimer & timer)39 void CppTimerManager::RemoveCppTimer(CppTimer& timer)
40 {
41 runningTimers.remove(&timer);
42 ILOG("CppTimerManager::RemoveCppTimer %x %x", this, &timer);
43 }
44
RunTimerTick()45 void CppTimerManager::RunTimerTick()
46 {
47 auto tempTimers = runningTimers;
48 if (tempTimers.size() == 0) {
49 ILOG("CppTimerManager::RunTimerTick No timer exec.");
50 }
51 auto iter = tempTimers.begin();
52 while (iter != tempTimers.end()) {
53 CppTimer* timer = *iter;
54 timer->RunTimerTick(callbackQueue);
55
56 iter++;
57 }
58
59 callbackQueue.ConsumingCallback();
60 }
61