feat update pending_task_safety_flag
This commit is contained in:
@@ -6,8 +6,7 @@ sled::scoped_refptr<PendingTaskSafetyFlag>
|
||||
PendingTaskSafetyFlag::CreateInternal(bool alive)
|
||||
{
|
||||
// Explicit new, to access private constructor.
|
||||
return sled::scoped_refptr<PendingTaskSafetyFlag>(
|
||||
new PendingTaskSafetyFlag(alive));
|
||||
return sled::scoped_refptr<PendingTaskSafetyFlag>(new PendingTaskSafetyFlag(alive));
|
||||
}
|
||||
|
||||
sled::scoped_refptr<PendingTaskSafetyFlag>
|
||||
@@ -31,7 +30,7 @@ PendingTaskSafetyFlag::CreateDetachedInactive()
|
||||
void
|
||||
PendingTaskSafetyFlag::SetNotAlive()
|
||||
{
|
||||
alive_ = true;
|
||||
alive_ = false;
|
||||
}
|
||||
|
||||
void
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#include "sled/timer/task_queue_timeout.h"
|
||||
#include "sled/log/log.h"
|
||||
#include "sled/task_queue/pending_task_safety_flag.h"
|
||||
#include "sled/units/time_delta.h"
|
||||
|
||||
namespace sled {
|
||||
@@ -23,34 +24,40 @@ TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, Timeout
|
||||
LOGV("timer",
|
||||
"New timeout duration is less than scheduled - "
|
||||
"ghosting old delayed task");
|
||||
safety_flag_->SetNotAlive();
|
||||
safety_flag_ = PendingTaskSafetyFlag::Create();
|
||||
}
|
||||
|
||||
posted_task_expiration_ = timeout_expiration_;
|
||||
auto safety_flag = safety_flag_;
|
||||
parent_.task_queue_.PostDelayedTaskWithPrecision(
|
||||
precision_,
|
||||
[timeout_id, this]() {
|
||||
if (timeout_id != this->timeout_id_) { return; }
|
||||
LOGV("timer", "Timeout expired: {}", timeout_id);
|
||||
SafeTask(safety_flag_,
|
||||
[timeout_id, this]() {
|
||||
// if (timeout_id != this->timeout_id_) { return; }
|
||||
LOGV("timer", "Timeout expired: {}", timeout_id);
|
||||
ASSERT(posted_task_expiration_ != std::numeric_limits<TimeMs>::max(), "");
|
||||
posted_task_expiration_ = std::numeric_limits<TimeMs>::max();
|
||||
|
||||
ASSERT(posted_task_expiration_ != std::numeric_limits<TimeMs>::max(), "");
|
||||
posted_task_expiration_ = std::numeric_limits<TimeMs>::max();
|
||||
if (timeout_expiration_ == std::numeric_limits<TimeMs>::max()) {
|
||||
// cancelled timer
|
||||
// do nothing
|
||||
} else {
|
||||
const TimeMs now = parent_.get_time_();
|
||||
const DurationMs remaining = timeout_expiration_ - now;
|
||||
bool is_expired = timeout_expiration_ <= now;
|
||||
|
||||
if (timeout_expiration_ == std::numeric_limits<TimeMs>::max()) {
|
||||
// cancelled timer
|
||||
// do nothing
|
||||
} else {
|
||||
const TimeMs now = parent_.get_time_();
|
||||
if (timeout_expiration_ <= now) {
|
||||
timeout_expiration_ = std::numeric_limits<TimeMs>::max();
|
||||
LOGV("timer", "Timeout Triggered: {}", timeout_id);
|
||||
parent_.on_expired_(timeout_id_);
|
||||
} else {
|
||||
const DurationMs remaining = timeout_expiration_ - now;
|
||||
timeout_expiration_ = std::numeric_limits<TimeMs>::max();
|
||||
Start(remaining, timeout_id);
|
||||
}
|
||||
}
|
||||
},
|
||||
timeout_expiration_ = std::numeric_limits<TimeMs>::max();
|
||||
|
||||
if (!is_expired) {
|
||||
// continue wait
|
||||
Start(remaining, timeout_id);
|
||||
} else {
|
||||
LOGV("timer", "Timeout Triggered: {}", timeout_id);
|
||||
parent_.on_expired_(timeout_id_);
|
||||
}
|
||||
}
|
||||
}),
|
||||
sled::TimeDelta::Millis(duration_ms));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user