mirror of
https://github.com/openharmony/aafwk_standard.git
synced 2026-07-18 11:44:32 -04:00
9a9cd53c84
Signed-off-by: zhongjianfei <zhongjianfei@huawei.com> Change-Id: I0436a3a061297b02b50bef0861d66e8243419b5e
64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
/*
|
|
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "sync_task.h"
|
|
#include <thread>
|
|
|
|
namespace OHOS {
|
|
namespace AppExecFwk {
|
|
// Help to calculate hash code of object.
|
|
template<typename T>
|
|
inline size_t CalculateHashCode(const T &obj)
|
|
{
|
|
std::hash<T> calculateHashCode;
|
|
return calculateHashCode(obj);
|
|
}
|
|
|
|
SyncTask::SyncTask(const std::shared_ptr<Runnable> &runnable, TaskPriority priority,
|
|
const std::shared_ptr<BaseTaskDispatcher> &baseTaskDispatcher)
|
|
: Task(runnable, priority, baseTaskDispatcher)
|
|
{
|
|
executed_.store(false);
|
|
}
|
|
|
|
void SyncTask::Run()
|
|
{
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
auto threadId = std::this_thread::get_id();
|
|
HILOG_INFO("SyncTask::Run begin thread=%{public}zu", CalculateHashCode(threadId));
|
|
Task::Run();
|
|
executed_.store(true);
|
|
|
|
// |waitTask| may have been multi invoked.
|
|
condition_variable_.notify_all();
|
|
HILOG_INFO("SyncTask::Run end thread=%{public}zu", CalculateHashCode(threadId));
|
|
}
|
|
|
|
void SyncTask::WaitTask()
|
|
{
|
|
auto threadId = std::this_thread::get_id();
|
|
HILOG_INFO("SyncTask::WaitTask begin thread=%{public}zu", CalculateHashCode(threadId));
|
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
while (executed_ == false) {
|
|
condition_variable_.wait(lock);
|
|
}
|
|
|
|
HILOG_INFO("SyncTask::WaitTask end thread=%{public}zu", CalculateHashCode(threadId));
|
|
}
|
|
|
|
} // namespace AppExecFwk
|
|
} // namespace OHOS
|