17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#include "CppTimerManager.h"
177c804472Sopenharmony_ci
187c804472Sopenharmony_ci#include <thread>
197c804472Sopenharmony_ci
207c804472Sopenharmony_cistd::map<std::thread::id, std::unique_ptr<CppTimerManager>> CppTimerManager::managers;
217c804472Sopenharmony_ci
227c804472Sopenharmony_ci// Non-threaded security
237c804472Sopenharmony_ciCppTimerManager& CppTimerManager::GetTimerManager()
247c804472Sopenharmony_ci{
257c804472Sopenharmony_ci    std::thread::id curThreadId = std::this_thread::get_id();
267c804472Sopenharmony_ci    if (managers.find(curThreadId) == managers.end()) {
277c804472Sopenharmony_ci        managers[curThreadId] = std::make_unique<CppTimerManager>();
287c804472Sopenharmony_ci    }
297c804472Sopenharmony_ci    return *managers[curThreadId];
307c804472Sopenharmony_ci}
317c804472Sopenharmony_ci
327c804472Sopenharmony_civoid CppTimerManager::AddCppTimer(CppTimer& timer)
337c804472Sopenharmony_ci{
347c804472Sopenharmony_ci    runningTimers.push_back(&timer);
357c804472Sopenharmony_ci    ILOG("CppTimerManager::AddCppTimer %x %x", this, &timer);
367c804472Sopenharmony_ci}
377c804472Sopenharmony_ci
387c804472Sopenharmony_civoid CppTimerManager::RemoveCppTimer(CppTimer& timer)
397c804472Sopenharmony_ci{
407c804472Sopenharmony_ci    runningTimers.remove(&timer);
417c804472Sopenharmony_ci    ILOG("CppTimerManager::RemoveCppTimer %x %x", this, &timer);
427c804472Sopenharmony_ci}
437c804472Sopenharmony_ci
447c804472Sopenharmony_civoid CppTimerManager::RunTimerTick()
457c804472Sopenharmony_ci{
467c804472Sopenharmony_ci    std::list<CppTimer*> tempTimers = runningTimers;
477c804472Sopenharmony_ci    if (tempTimers.size() == 0) {
487c804472Sopenharmony_ci        ILOG("CppTimerManager::RunTimerTick No timer exec.");
497c804472Sopenharmony_ci    }
507c804472Sopenharmony_ci    auto iter = tempTimers.cbegin();
517c804472Sopenharmony_ci    while (iter != tempTimers.cend()) {
527c804472Sopenharmony_ci        CppTimer* timer = *iter;
537c804472Sopenharmony_ci        timer->RunTimerTick(callbackQueue);
547c804472Sopenharmony_ci
557c804472Sopenharmony_ci        iter++;
567c804472Sopenharmony_ci    }
577c804472Sopenharmony_ci
587c804472Sopenharmony_ci    callbackQueue.ConsumingCallback();
597c804472Sopenharmony_ci}
60