!373 fix: async task is executed synchronously when posted from main worker thread

Merge pull request !373 from mayunteng/asynctask
This commit is contained in:
openharmony_ci 2022-12-21 12:36:04 +00:00 committed by Gitee
commit b767b6ad34
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 5 additions and 14 deletions

View File

@ -117,9 +117,6 @@ int32_t DelegateTasks::PostSyncTask(DTaskCallback callback)
int32_t DelegateTasks::PostAsyncTask(DTaskCallback callback)
{
CHKPR(callback, ERROR_NULL_POINTER);
if (IsCallFromWorkerThread()) {
return callback();
}
auto task = PostTask(callback);
if (task == nullptr) {
FI_HILOGE("Post async task failed");
@ -146,10 +143,6 @@ void DelegateTasks::PopPendingTaskList(std::vector<TaskPtr> &tasks)
DelegateTasks::TaskPtr DelegateTasks::PostTask(DTaskCallback callback, Promise *promise)
{
if (IsCallFromWorkerThread()) {
FI_HILOGE("This interface cannot be called from a worker thread.");
return nullptr;
}
std::lock_guard<std::mutex> guard(mux_);
FI_HILOGD("tasks_ size %{public}d", static_cast<int32_t>(tasks_.size()));
static constexpr int32_t maxTasksLimit = 1000;

View File

@ -33,7 +33,6 @@ namespace DeviceStatus {
namespace {
constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceManager" };
constexpr int32_t DEFAULT_WAIT_TIME_MS { 1000 };
constexpr int32_t DEFAULT_DELAY_FOR_ASYNC_TASK { 1 };
constexpr int32_t WAIT_FOR_ONCE { 1 };
} // namespace
@ -131,17 +130,16 @@ void DeviceManager::OnGetDeviceIds(std::vector<int32_t> &deviceIds)
{
CALL_INFO_TRACE;
CHKPV(context_);
int32_t timerId;
for (const int32_t id : deviceIds) {
timerId = context_->GetTimerManager().AddTimer(DEFAULT_DELAY_FOR_ASYNC_TASK,
WAIT_FOR_ONCE, std::bind(&DeviceManager::GetDeviceAsync, this, id));
if (timerId < 0) {
FI_HILOGE("Add timer failed");
int32_t ret = context_->GetDelegateTasks().PostAsyncTask(
std::bind(&DeviceManager::GetDeviceAsync, this, id));
if (ret != RET_OK) {
FI_HILOGE("PostAsyncTask failed");
}
}
timerId = context_->GetTimerManager().AddTimer(DEFAULT_WAIT_TIME_MS,
int32_t timerId = context_->GetTimerManager().AddTimer(DEFAULT_WAIT_TIME_MS,
WAIT_FOR_ONCE, std::bind(&DeviceManager::Synchronize, this));
if (timerId < 0) {
FI_HILOGE("Add timer failed");