Merge branch 'master' of code.uocat.com:tqcq/sled

This commit is contained in:
tqcq
2024-03-21 08:20:07 +08:00
14 changed files with 198 additions and 165 deletions

View File

@@ -1,29 +0,0 @@
/**
* @file : {{FILE}}
* @created : {{TIMESTAMP}}
* @license : {{LICENSE}}
**/
#ifndef{{MACRO_GUARD } }
#define{{MACRO_GUARD } }
namespace {
{
CURSOR
}
}// namespace
{
class {
{
CAMEL_CLASS
}
} {
public:
};
}// namespace
#endif// {{MACRO_GUARD}}

View File

@@ -1,5 +1,6 @@
#include "sled/profiling/profiling.h"
#include <uprofile.h>
// #include <uprofile.h>
namespace sled {
Profiling *
@@ -12,7 +13,7 @@ Profiling::Instance()
bool
Profiling::Start(const std::string &file)
{
uprofile::start(file.c_str());
// uprofile::start(file.c_str());
started_ = true;
return true;
}
@@ -20,53 +21,53 @@ Profiling::Start(const std::string &file)
void
Profiling::Stop()
{
uprofile::stop();
// uprofile::stop();
}
void
Profiling::TimeBegin(const std::string &name)
{
if (!started_) return;
uprofile::timeBegin(name);
// uprofile::timeBegin(name);
}
void
Profiling::TimeEnd(const std::string &name)
{
if (!started_) return;
uprofile::timeEnd(name);
// uprofile::timeEnd(name);
}
void
Profiling::StartProcessMemoryMonitoring(int period_ms)
{
if (!started_) return;
uprofile::startProcessMemoryMonitoring(period_ms);
// uprofile::startProcessMemoryMonitoring(period_ms);
}
void
Profiling::StartSystemMemoryMonitoring(int period_ms)
{
if (!started_) return;
uprofile::startSystemMemoryMonitoring(period_ms);
// uprofile::startSystemMemoryMonitoring(period_ms);
}
void
Profiling::StartCPUUsageMonitoring(int period_ms)
{
if (!started_) return;
uprofile::startCPUUsageMonitoring(period_ms);
// uprofile::startCPUUsageMonitoring(period_ms);
}
void
Profiling::GetSystemMemory(int &total_mem, int &available_mem, int &free_mem)
{
uprofile::getSystemMemory(total_mem, available_mem, free_mem);
// uprofile::getSystemMemory(total_mem, available_mem, free_mem);
}
void
Profiling::GetProcessMemory(int &rss, int &shared)
{
uprofile::getProcessMemory(rss, shared);
// uprofile::getProcessMemory(rss, shared);
}
}// namespace sled

View File

@@ -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

View File

@@ -1,15 +1,17 @@
#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 {
TaskQueueTimeoutFactory::TaskQueueTimeout::TaskQueueTimeout(TaskQueueTimeoutFactory &parent,
sled::TaskQueueBase::DelayPrecision precision)
: parent_(parent),
precision_(precision)
precision_(precision),
safety_flag_(PendingTaskSafetyFlag::Create())
{}
TaskQueueTimeoutFactory::TaskQueueTimeout::~TaskQueueTimeout() {}
TaskQueueTimeoutFactory::TaskQueueTimeout::~TaskQueueTimeout() { safety_flag_->SetNotAlive(); }
void
TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, TimeoutID timeout_id)
@@ -23,32 +25,42 @@ 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]() {
LOGV("timer", "Timeout expired: {}", timeout_id);
ASSERT(posted_task_expiration_ != std::numeric_limits<TimeMs>::max(), "");
posted_task_expiration_ = std::numeric_limits<TimeMs>::max();
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<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_();
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);
}
}
},
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;
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));
}

View File

@@ -47,6 +47,7 @@ Timer::Stop()
{
if (is_running()) {
timeout_->Stop();
generation_ = TimerGeneration(generation_ + 1);
expiration_count_ = 0;
is_running_ = false;
}
@@ -58,6 +59,7 @@ Timer::Trigger(TimerGeneration generation)
if (!is_running_ || generation != generation_) { return; }
++expiration_count_;
is_running_ = false;
// if max_restarts > exppiration_count_ then restart
{
is_running_ = true;
@@ -82,12 +84,10 @@ TimerManager::CreateTimer(const std::string &name, Timer::OnExpired on_expired)
next_id_ = TimerID(next_id_ + 1);
TimerID id = next_id_;
std::unique_ptr<Timeout> timeout =
timeout_creator_(sled::TaskQueueBase::DelayPrecision::kHigh);
std::unique_ptr<Timeout> timeout = timeout_creator_(sled::TaskQueueBase::DelayPrecision::kHigh);
auto timer = std::unique_ptr<Timer>(new Timer(
id, name, std::move(on_expired),
/* ungrgister_handler=*/[this, id]() { timers_.erase(id); },
std::move(timeout)));
/* ungrgister_handler=*/[this, id]() { timers_.erase(id); }, std::move(timeout)));
timers_[id] = timer.get();
return timer;
}