• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "single_delayed_task_mgr.h"
17 
18 #include <thread>
19 
20 #include "app_log_tag_wrapper.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
SingleDelayedTaskMgr(const std::string & taskName,uint64_t delayedTimeMs)24 SingleDelayedTaskMgr::SingleDelayedTaskMgr(const std::string &taskName, uint64_t delayedTimeMs)
25     : taskName_(taskName), delayedTimeMs_(delayedTimeMs)
26 {}
27 
ScheduleDelayedTask(std::function<void ()> func)28 void SingleDelayedTaskMgr::ScheduleDelayedTask(std::function<void()> func)
29 {
30     {
31         std::lock_guard<std::mutex> lock(mutex_);
32         executeTime_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(delayedTimeMs_);
33         if (isRunning_) {
34             return;
35         }
36         isRunning_ = true;
37     }
38 
39     std::weak_ptr<SingleDelayedTaskMgr> weakPtr = weak_from_this();
40     auto task = [weakPtr, func]() {
41         while (true) {
42             auto sharedPtr = weakPtr.lock();
43             if (sharedPtr == nullptr) {
44                 LOG_W(BMS_TAG_DEFAULT, "SingleDelayedTaskMgr null");
45                 return;
46             }
47             auto executeTime = sharedPtr->GetExecuteTime();
48             auto now = std::chrono::steady_clock::now();
49             std::chrono::milliseconds sleepTime =
50                 std::chrono::duration_cast<std::chrono::milliseconds>(executeTime - now);
51             if (sleepTime.count() <= 0) {
52                 LOG_I(BMS_TAG_DEFAULT, "execute begin %{public}s", sharedPtr->taskName_.c_str());
53                 break;
54             }
55             sharedPtr = nullptr;
56             std::this_thread::sleep_for(sleepTime);
57         }
58         func();
59         auto sharedPtr = weakPtr.lock();
60         if (sharedPtr == nullptr) {
61             LOG_W(BMS_TAG_DEFAULT, "SingleDelayedTaskMgr null");
62             return;
63         }
64         sharedPtr->SetTaskFinished();
65     };
66     std::thread(task).detach();
67 }
68 
GetExecuteTime() const69 std::chrono::time_point<std::chrono::steady_clock> SingleDelayedTaskMgr::GetExecuteTime() const
70 {
71     std::lock_guard<std::mutex> lock(mutex_);
72     return executeTime_;
73 }
74 
SetTaskFinished()75 void SingleDelayedTaskMgr::SetTaskFinished()
76 {
77     std::lock_guard<std::mutex> lock(mutex_);
78     isRunning_ = false;
79 }
80 }  // namespace AppExecFwk
81 }  // namespace OHOS
82