From e08d8b7984fc80040991411e13604d07663b4ab6 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sat, 23 Mar 2024 09:03:00 +0800 Subject: [PATCH] feat update --- include/sled/task_queue/pending_task_safety_flag.h | 4 +++- include/sled/timer/task_queue_timeout.h | 6 ++++-- src/task_queue/pending_task_safety_flag.cc | 11 +++++++++-- src/timer/task_queue_timeout.cc | 14 ++++++++++---- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/include/sled/task_queue/pending_task_safety_flag.h b/include/sled/task_queue/pending_task_safety_flag.h index f1c1170..8c3b6e4 100644 --- a/include/sled/task_queue/pending_task_safety_flag.h +++ b/include/sled/task_queue/pending_task_safety_flag.h @@ -4,12 +4,13 @@ * @license : MIT **/ -#pragma once #ifndef SLED_TASK_QUEUE_PENDING_TASK_SAFETY_FLAG_H #define SLED_TASK_QUEUE_PENDING_TASK_SAFETY_FLAG_H +#pragma once #include "sled/ref_counted_base.h" #include "sled/scoped_refptr.h" +#include "sled/synchronization/sequence_checker.h" #include namespace sled { @@ -31,6 +32,7 @@ protected: private: static sled::scoped_refptr CreateInternal(bool alive); bool alive_ = true; + SequenceChecker main_sequence_; }; class ScopedTaskSafety final { diff --git a/include/sled/timer/task_queue_timeout.h b/include/sled/timer/task_queue_timeout.h index 18b54dd..c91ae52 100644 --- a/include/sled/timer/task_queue_timeout.h +++ b/include/sled/timer/task_queue_timeout.h @@ -1,8 +1,9 @@ -#pragma once -#include "sled/scoped_refptr.h" #ifndef SLED_TIMER_QUEUE_TIMEOUT_H #define SLED_TIMER_QUEUE_TIMEOUT_H +#pragma once +#include "sled/scoped_refptr.h" +#include "sled/synchronization/sequence_checker.h" #include "sled/task_queue/pending_task_safety_flag.h" #include "sled/task_queue/task_queue_base.h" #include "sled/timer/timeout.h" @@ -45,6 +46,7 @@ private: scoped_refptr safety_flag_; }; + sled::SequenceChecker thread_checker_; sled::TaskQueueBase &task_queue_; const std::function get_time_; const std::function on_expired_; diff --git a/src/task_queue/pending_task_safety_flag.cc b/src/task_queue/pending_task_safety_flag.cc index 80d10ea..44bed19 100644 --- a/src/task_queue/pending_task_safety_flag.cc +++ b/src/task_queue/pending_task_safety_flag.cc @@ -18,30 +18,37 @@ PendingTaskSafetyFlag::Create() sled::scoped_refptr PendingTaskSafetyFlag::CreateDetached() { - return CreateInternal(true); + scoped_refptr safety_flag = CreateInternal(true); + safety_flag->main_sequence_.Detach(); + return safety_flag; } sled::scoped_refptr PendingTaskSafetyFlag::CreateDetachedInactive() { - return CreateInternal(false); + scoped_refptr safety_flag = CreateInternal(false); + safety_flag->main_sequence_.Detach(); + return safety_flag; } void PendingTaskSafetyFlag::SetNotAlive() { + SLED_DCHECK_RUN_ON(&main_sequence_); alive_ = false; } void PendingTaskSafetyFlag::SetAlive() { + SLED_DCHECK_RUN_ON(&main_sequence_); alive_ = true; } bool PendingTaskSafetyFlag::alive() const { + SLED_DCHECK_RUN_ON(&main_sequence_); return alive_; } diff --git a/src/timer/task_queue_timeout.cc b/src/timer/task_queue_timeout.cc index 678781d..b13ca14 100644 --- a/src/timer/task_queue_timeout.cc +++ b/src/timer/task_queue_timeout.cc @@ -11,11 +11,16 @@ TaskQueueTimeoutFactory::TaskQueueTimeout::TaskQueueTimeout(TaskQueueTimeoutFact safety_flag_(PendingTaskSafetyFlag::Create()) {} -TaskQueueTimeoutFactory::TaskQueueTimeout::~TaskQueueTimeout() { safety_flag_->SetNotAlive(); } +TaskQueueTimeoutFactory::TaskQueueTimeout::~TaskQueueTimeout() +{ + SLED_DCHECK_RUN_ON(&parent_.thread_checker_); + safety_flag_->SetNotAlive(); +} void TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, TimeoutID timeout_id) { + SLED_DCHECK_RUN_ON(&parent_.thread_checker_); ASSERT(timeout_expiration_ == std::numeric_limits::max(), ""); timeout_expiration_ = parent_.get_time_() + duration_ms; timeout_id_ = timeout_id; @@ -36,10 +41,9 @@ TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, Timeout precision_, SafeTask(safety_flag_, [timeout_id, this]() { - // if (timeout_id != this->timeout_id_) { return; } LOGV("timer", "Timeout expired: {}", timeout_id); - // FIXME: this is a bug, the posted_task_expiration_ should be reset to max - ASSERT(posted_task_expiration_ != std::numeric_limits::max(), ""); + SLED_DCHECK_RUN_ON(&parent_.thread_checker_); + DCHECK(posted_task_expiration_ != std::numeric_limits::max(), ""); posted_task_expiration_ = std::numeric_limits::max(); if (timeout_expiration_ == std::numeric_limits::max()) { @@ -67,6 +71,8 @@ TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, Timeout void TaskQueueTimeoutFactory::TaskQueueTimeout::Stop() { + + SLED_DCHECK_RUN_ON(&parent_.thread_checker_); timeout_expiration_ = std::numeric_limits::max(); }