1 /*
2 * Copyright (c) 2023-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 "delayed_task_group.h"
17 #include "steady_clock.h"
18 #include "dp_log.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
DelayedTaskGroup(const std::string & name,TaskFunc func,const ThreadPool * threadPool)23 DelayedTaskGroup::DelayedTaskGroup(const std::string& name, TaskFunc func, const ThreadPool* threadPool)
24 : BaseTaskGroup(name, std::move(func), true, threadPool), mutex_(), timeBroker_(nullptr), paramMap_()
25 {
26 DP_DEBUG_LOG("(%s) entered.", GetName().c_str());
27 Initialize();
28 }
29
~DelayedTaskGroup()30 DelayedTaskGroup::~DelayedTaskGroup()
31 {
32 DP_DEBUG_LOG("(%s) entered.", GetName().c_str());
33 std::lock_guard<std::mutex> lock(mutex_);
34 if (timeBroker_) {
35 timeBroker_.reset();
36 }
37 paramMap_.clear();
38 }
39
Initialize()40 void DelayedTaskGroup::Initialize()
41 {
42 DP_DEBUG_LOG("(%s) Initialize entered.", GetName().c_str());
43 BaseTaskGroup::Initialize();
44 timeBroker_ = TimeBroker::Create("DelayedTaskGroup");
45 }
46
SubmitTask(std::any param)47 bool DelayedTaskGroup::SubmitTask(std::any param)
48 {
49 if (!param.has_value()) {
50 DP_DEBUG_LOG("(%s) SubmitTask failed due to containing no parameter.", GetName().c_str());
51 return false;
52 }
53 std::lock_guard<std::mutex> lock(mutex_);
54 auto&& [delayTimeMs, task] = std::any_cast<std::tuple<uint32_t, std::function<void()>>&&>(std::move(param));
55 DP_DEBUG_LOG("(%s) SubmitTask, delayTimeMs %{public}d ,expiring timestamp: %{public}d",
56 GetName().c_str(),
57 static_cast<int>(delayTimeMs),
58 static_cast<int>(SteadyClock::GetTimestampMilli() + delayTimeMs));
59 uint32_t handle;
60 auto ret = timeBroker_->RegisterCallback(delayTimeMs, [this](uint32_t handle) { TimerExpired(handle); }, handle);
61 if (ret) {
62 paramMap_.emplace(handle, std::move(task));
63 } else {
64 DP_ERR_LOG("(%s) SubmitTask failed due to RegisterCallback.", GetName().c_str());
65 }
66 return ret;
67 }
68
TimerExpired(uint32_t handle)69 void DelayedTaskGroup::TimerExpired(uint32_t handle)
70 {
71 DP_DEBUG_LOG("(%s) TimerExpired, handle = %{public}d", GetName().c_str(), static_cast<int>(handle));
72 std::lock_guard<std::mutex> lock(mutex_);
73 if (paramMap_.count(handle) == 0) {
74 DP_DEBUG_LOG("(%s) TimerExpired, handle = %{public}d", GetName().c_str(), static_cast<int>(handle));
75 return;
76 }
77 BaseTaskGroup::SubmitTask(std::move(paramMap_[handle]));
78 paramMap_.erase(handle);
79 }
80 } //namespace DeferredProcessing
81 } // namespace CameraStandard
82 } // namespace OHOS
83