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 std::map<std::thread::id, std::unique_ptr<CppTimerManager>> CppTimerManager::managers; 21 22 // Non-threaded security GetTimerManager()23CppTimerManager& CppTimerManager::GetTimerManager() 24 { 25 std::thread::id curThreadId = std::this_thread::get_id(); 26 if (managers.find(curThreadId) == managers.end()) { 27 managers[curThreadId] = std::make_unique<CppTimerManager>(); 28 } 29 return *managers[curThreadId]; 30 } 31 AddCppTimer(CppTimer & timer)32void CppTimerManager::AddCppTimer(CppTimer& timer) 33 { 34 runningTimers.push_back(&timer); 35 ILOG("CppTimerManager::AddCppTimer"); 36 } 37 RemoveCppTimer(CppTimer & timer)38void CppTimerManager::RemoveCppTimer(CppTimer& timer) 39 { 40 runningTimers.remove(&timer); 41 ILOG("CppTimerManager::RemoveCppTimer"); 42 } 43 RunTimerTick()44void CppTimerManager::RunTimerTick() 45 { 46 std::list<CppTimer*> tempTimers = runningTimers; 47 if (tempTimers.size() == 0) { 48 ILOG("CppTimerManager::RunTimerTick No timer exec."); 49 } 50 auto iter = tempTimers.cbegin(); 51 while (iter != tempTimers.cend()) { 52 CppTimer* timer = *iter; 53 timer->RunTimerTick(callbackQueue); 54 55 iter++; 56 } 57 58 callbackQueue.ConsumingCallback(); 59 } 60