mirror of
https://gitee.com/openharmony/msdp_device_status
synced 2025-02-25 18:23:04 +00:00
!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:
commit
b767b6ad34
@ -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;
|
||||
|
@ -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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user