1cb7eb8c9Sopenharmony_ci/*
2cb7eb8c9Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at
6cb7eb8c9Sopenharmony_ci *
7cb7eb8c9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb7eb8c9Sopenharmony_ci *
9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and
13cb7eb8c9Sopenharmony_ci * limitations under the License.
14cb7eb8c9Sopenharmony_ci */
15cb7eb8c9Sopenharmony_ci
16cb7eb8c9Sopenharmony_ci#include "ffrt_timer.h"
17cb7eb8c9Sopenharmony_ci
18cb7eb8c9Sopenharmony_ci#include "cloud_file_log.h"
19cb7eb8c9Sopenharmony_ci
20cb7eb8c9Sopenharmony_cinamespace OHOS::FileManagement::CloudFile {
21cb7eb8c9Sopenharmony_cistatic constexpr uint32_t TIMER_MAX_INTERVAL_MS = 200;
22cb7eb8c9Sopenharmony_ciusing namespace std;
23cb7eb8c9Sopenharmony_ci
24cb7eb8c9Sopenharmony_ciFfrtTimer::FfrtTimer(const std::string &name) : name_(name) {}
25cb7eb8c9Sopenharmony_ci
26cb7eb8c9Sopenharmony_ciFfrtTimer ::~FfrtTimer()
27cb7eb8c9Sopenharmony_ci{
28cb7eb8c9Sopenharmony_ci    Stop();
29cb7eb8c9Sopenharmony_ci}
30cb7eb8c9Sopenharmony_ci
31cb7eb8c9Sopenharmony_civoid FfrtTimer::Start(const TimerCallback &callback, uint32_t interval, uint32_t repatTimes)
32cb7eb8c9Sopenharmony_ci{
33cb7eb8c9Sopenharmony_ci    unique_lock<ffrt::mutex> lock(taskMutex_);
34cb7eb8c9Sopenharmony_ci    if (running_ == true) {
35cb7eb8c9Sopenharmony_ci        LOGE("timer may be is running, timerName:%{public}s", name_.c_str());
36cb7eb8c9Sopenharmony_ci        return;
37cb7eb8c9Sopenharmony_ci    }
38cb7eb8c9Sopenharmony_ci    LOGD("start timer, timerName:%{public}s", name_.c_str());
39cb7eb8c9Sopenharmony_ci    running_ = true;
40cb7eb8c9Sopenharmony_ci    auto task = [this, interval, callback, repatTimes]() {
41cb7eb8c9Sopenharmony_ci        LOGD("task entering loop");
42cb7eb8c9Sopenharmony_ci        uint32_t times = repatTimes;
43cb7eb8c9Sopenharmony_ci        while (times > 0) {
44cb7eb8c9Sopenharmony_ci            callback();
45cb7eb8c9Sopenharmony_ci            times--;
46cb7eb8c9Sopenharmony_ci            unique_lock<ffrt::mutex> lock(sleepMutex_);
47cb7eb8c9Sopenharmony_ci            bool stop =
48cb7eb8c9Sopenharmony_ci                sleepCv_.wait_for(lock, std::chrono::milliseconds(interval), [this]() { return !this->running_; });
49cb7eb8c9Sopenharmony_ci            if (stop) { // is stopped
50cb7eb8c9Sopenharmony_ci                break;
51cb7eb8c9Sopenharmony_ci            }
52cb7eb8c9Sopenharmony_ci        }
53cb7eb8c9Sopenharmony_ci        LOGD("task leaving loop");
54cb7eb8c9Sopenharmony_ci    };
55cb7eb8c9Sopenharmony_ci
56cb7eb8c9Sopenharmony_ci    isJoinable_ = std::make_unique<bool>(true);
57cb7eb8c9Sopenharmony_ci    ffrt::submit(task, {}, {isJoinable_.get()}, ffrt::task_attr().name(name_.c_str()));
58cb7eb8c9Sopenharmony_ci}
59cb7eb8c9Sopenharmony_ci
60cb7eb8c9Sopenharmony_civoid FfrtTimer::Stop()
61cb7eb8c9Sopenharmony_ci{
62cb7eb8c9Sopenharmony_ci    unique_lock<ffrt::mutex> lock(taskMutex_);
63cb7eb8c9Sopenharmony_ci    if (running_ == false) {
64cb7eb8c9Sopenharmony_ci        LOGE("timer may be is stopped, timerName:%{public}s", name_.c_str());
65cb7eb8c9Sopenharmony_ci        return;
66cb7eb8c9Sopenharmony_ci    }
67cb7eb8c9Sopenharmony_ci
68cb7eb8c9Sopenharmony_ci    LOGD("stop timer, timerName:%{public}s", name_.c_str());
69cb7eb8c9Sopenharmony_ci    {
70cb7eb8c9Sopenharmony_ci        std::unique_lock<ffrt::mutex> lock(sleepMutex_);
71cb7eb8c9Sopenharmony_ci        running_ = false;
72cb7eb8c9Sopenharmony_ci        sleepCv_.notify_one();
73cb7eb8c9Sopenharmony_ci    }
74cb7eb8c9Sopenharmony_ci
75cb7eb8c9Sopenharmony_ci    if (isJoinable_.get() && *isJoinable_) {
76cb7eb8c9Sopenharmony_ci        ffrt::wait({isJoinable_.get()});
77cb7eb8c9Sopenharmony_ci        *isJoinable_ = false;
78cb7eb8c9Sopenharmony_ci    }
79cb7eb8c9Sopenharmony_ci    LOGD("timer is stoped, timerName:%{public}s", name_.c_str());
80cb7eb8c9Sopenharmony_ci}
81cb7eb8c9Sopenharmony_ci} // namespace OHOS::FileManagement::CloudFile