Fixed a crash in task scheduler

PiperOrigin-RevId: 657213567
This commit is contained in:
Guogang Li
2024-07-29 08:45:07 -07:00
committed by Copybara-Service
parent 0074ffb990
commit 9413448e6e
4 changed files with 40 additions and 34 deletions
@@ -19,12 +19,12 @@
#include <memory>
#include <utility>
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/flags/nearby_platform_feature_flags.h"
#include "internal/platform/implementation/cancelable.h"
#include "internal/platform/logging.h"
#include "internal/platform/mutex_lock.h"
#include "internal/platform/runnable.h"
namespace nearby {
@@ -40,7 +40,7 @@ ScheduledExecutor::ScheduledExecutor()
// using std:shared_ptr<> instead of std::unique_ptr<>.
std::shared_ptr<api::Cancelable> ScheduledExecutor::Schedule(
Runnable&& runnable, absl::Duration duration) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
if (NearbyFlags::GetInstance().GetBoolFlag(
platform::config_package_nearby::nearby_platform_feature::
kEnableTaskScheduler)) {
@@ -73,7 +73,7 @@ std::shared_ptr<api::Cancelable> ScheduledExecutor::Schedule(
}
void ScheduledExecutor::Execute(Runnable&& runnable) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
if (shut_down_) {
NEARBY_LOGS(ERROR) << __func__
<< ": Attempt to Execute on a shut down executor.";
@@ -84,7 +84,7 @@ void ScheduledExecutor::Execute(Runnable&& runnable) {
}
void ScheduledExecutor::Shutdown() {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
if (!shut_down_) {
shut_down_ = true;
for (auto& task : scheduled_tasks_) {
@@ -23,13 +23,13 @@
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/synchronization/mutex.h"
#include "absl/synchronization/notification.h"
#include "absl/time/time.h"
#include "internal/platform/implementation/cancelable.h"
#include "internal/platform/implementation/scheduled_executor.h"
#include "internal/platform/implementation/windows/executor.h"
#include "internal/platform/implementation/windows/task_scheduler.h"
#include "internal/platform/mutex.h"
#include "internal/platform/runnable.h"
namespace nearby {
@@ -97,7 +97,7 @@ class ScheduledExecutor : public api::ScheduledExecutor {
bool is_executed_ = false;
};
Mutex mutex_;
absl::Mutex mutex_;
std::unique_ptr<nearby::windows::Executor> executor_ ABSL_GUARDED_BY(mutex_) =
nullptr;
std::vector<std::shared_ptr<ScheduledTask>> scheduled_tasks_
@@ -20,10 +20,10 @@
#include <memory>
#include <utility>
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "internal/platform/implementation/cancelable.h"
#include "internal/platform/logging.h"
#include "internal/platform/mutex_lock.h"
#include "internal/platform/runnable.h"
namespace nearby::windows {
@@ -40,7 +40,8 @@ TaskScheduler::TaskScheduler() {
NEARBY_LOGS(INFO) << __func__ << ": Created task scheduler: " << this;
}
TaskScheduler::~TaskScheduler() {
Shutdown();
absl::MutexLock lock(&mutex_);
ShutdownInternal();
NEARBY_LOGS(INFO) << __func__ << ": Destroyed task scheduler: " << this;
}
@@ -52,7 +53,7 @@ std::shared_ptr<api::Cancelable> TaskScheduler::Schedule(
std::shared_ptr<api::Cancelable> TaskScheduler::Schedule(
Runnable&& runnable, absl::Duration duration,
absl::Duration repeat_interval) {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
NEARBY_LOGS(INFO) << __func__
<< ": Scheduling task on task scheduler:" << this
<< ", duration: " << absl::ToInt64Milliseconds(duration)
@@ -84,26 +85,9 @@ std::shared_ptr<api::Cancelable> TaskScheduler::Schedule(
}
void TaskScheduler::Shutdown() {
MutexLock lock(&mutex_);
absl::MutexLock lock(&mutex_);
NEARBY_LOGS(INFO) << __func__ << ": Shutting down task scheduler:" << this;
if (is_shutdown_) {
return;
}
for (auto& task : scheduled_tasks_) {
// Wait for running task to finish.
if (!DeleteTimerQueueTimer(
nullptr, reinterpret_cast<HANDLE>(task.second->timer_handle()),
INVALID_HANDLE_VALUE)) {
if (GetLastError() != ERROR_IO_PENDING) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to delete timer queue timer: "
<< task.second->timer_handle()
<< " error: " << GetLastError();
}
}
}
scheduled_tasks_.clear();
is_shutdown_ = true;
ShutdownInternal();
NEARBY_LOGS(INFO) << __func__ << ": Shut down task scheduler:" << this;
}
@@ -114,7 +98,7 @@ TaskScheduler::ScheduledTask::ScheduledTask(TaskScheduler& task_scheduler,
bool TaskScheduler::ScheduledTask::Cancel() {
NEARBY_LOGS(INFO) << __func__ << ": Cancelling timer " << timer_handle_
<< " from task scheduler:" << this;
return task_scheduler_->remove_scheduled_task(timer_handle_);
return task_scheduler_->RemoveScheduledTask(timer_handle_);
}
void TaskScheduler::ScheduledTask::SetTimerHandle(intptr_t timer_handle) {
@@ -125,8 +109,8 @@ Runnable* TaskScheduler::ScheduledTask::runnable() { return &runnable_; }
intptr_t TaskScheduler::ScheduledTask::timer_handle() { return timer_handle_; }
bool TaskScheduler::remove_scheduled_task(intptr_t timer_handle) {
MutexLock lock(&mutex_);
bool TaskScheduler::RemoveScheduledTask(intptr_t timer_handle) {
absl::MutexLock lock(&mutex_);
auto it = scheduled_tasks_.find(timer_handle);
if (it == scheduled_tasks_.end()) {
return false;
@@ -146,4 +130,25 @@ bool TaskScheduler::remove_scheduled_task(intptr_t timer_handle) {
return true;
}
void TaskScheduler::ShutdownInternal() {
if (is_shutdown_) {
return;
}
for (auto& task : scheduled_tasks_) {
// Wait for running task to finish.
if (!DeleteTimerQueueTimer(
nullptr, reinterpret_cast<HANDLE>(task.second->timer_handle()),
INVALID_HANDLE_VALUE)) {
if (GetLastError() != ERROR_IO_PENDING) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to delete timer queue timer: "
<< task.second->timer_handle()
<< " error: " << GetLastError();
}
}
}
scheduled_tasks_.clear();
is_shutdown_ = true;
}
} // namespace nearby::windows
@@ -20,9 +20,9 @@
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "internal/platform/implementation/cancelable.h"
#include "internal/platform/mutex.h"
#include "internal/platform/runnable.h"
namespace nearby::windows {
@@ -65,9 +65,10 @@ class TaskScheduler {
intptr_t timer_handle_;
};
bool remove_scheduled_task(intptr_t timer_handle) ABSL_LOCKS_EXCLUDED(mutex_);
bool RemoveScheduledTask(intptr_t timer_handle) ABSL_LOCKS_EXCLUDED(mutex_);
void ShutdownInternal() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Mutex mutex_;
absl::Mutex mutex_;
bool is_shutdown_ ABSL_GUARDED_BY(mutex_) = false;
absl::flat_hash_map<intptr_t, std::shared_ptr<ScheduledTask>> scheduled_tasks_
ABSL_GUARDED_BY(mutex_);