aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/task_scheduler/task_scheduler.cpp
diff options
context:
space:
mode:
authorsskvor <sskvor@yandex-team.com>2025-03-24 10:44:12 +0300
committersskvor <sskvor@yandex-team.com>2025-03-24 11:00:27 +0300
commit569a094e5262e0a6cdfda5db18fe1bac0d6857d1 (patch)
treea6f2a9da2931a771b560cff4edfaa02873570bcf /library/cpp/threading/task_scheduler/task_scheduler.cpp
parentfa64d7938d59edd222ba66433bcc0ba5abde121e (diff)
downloadydb-569a094e5262e0a6cdfda5db18fe1bac0d6857d1.tar.gz
[library/cpp/threading] Add convenience methods to schedule functions in TTaskScheduler
commit_hash:b8c3ae8e8ca6b0fea0bbf820f40513da41a242f8
Diffstat (limited to 'library/cpp/threading/task_scheduler/task_scheduler.cpp')
-rw-r--r--library/cpp/threading/task_scheduler/task_scheduler.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/library/cpp/threading/task_scheduler/task_scheduler.cpp b/library/cpp/threading/task_scheduler/task_scheduler.cpp
index 174dde4bf75..b1d90d24dbb 100644
--- a/library/cpp/threading/task_scheduler/task_scheduler.cpp
+++ b/library/cpp/threading/task_scheduler/task_scheduler.cpp
@@ -3,11 +3,38 @@
#include <util/system/thread.h>
#include <util/string/cast.h>
#include <util/stream/output.h>
+#include <util/generic/yexception.h>
TTaskScheduler::ITask::~ITask() {}
TTaskScheduler::IRepeatedTask::~IRepeatedTask() {}
+class TFunctionTask final : public TTaskScheduler::ITask {
+public:
+ explicit TFunctionTask(std::function<TInstant()> function)
+ : Function_{std::move(function)}
+ {}
+
+ TInstant Process() override {
+ return Function_();
+ }
+private:
+ std::function<TInstant()> Function_;
+};
+
+class TRepeatedFunctionTask final : public TTaskScheduler::IRepeatedTask {
+public:
+ explicit TRepeatedFunctionTask(std::function<bool()> function)
+ : Function_{std::move(function)}
+ {}
+
+ bool Process() override {
+ return Function_();
+ }
+
+private:
+ std::function<bool()> Function_;
+};
class TTaskScheduler::TWorkerThread
: public ISimpleThread
@@ -147,6 +174,35 @@ bool TTaskScheduler::Add(IRepeatedTaskRef task, TDuration period) {
return Add(t, deadline);
}
+static void ThrowOnTaskLimitReached(bool sucessfullyScheduled) {
+ if (!sucessfullyScheduled) {
+ throw TTaskScheduler::TTaskSchedulerTaskLimitReached{} << "Failed to schedule task";
+ }
+}
+
+bool TTaskScheduler::AddFunc(std::function<TInstant()> function, TInstant expire) {
+ return Add(MakeIntrusive<TFunctionTask>(std::move(function)), expire);
+}
+
+bool TTaskScheduler::AddRepeatedFunc(std::function<bool()> function, TDuration period) {
+ return Add(MakeIntrusive<TRepeatedFunctionTask>(std::move(function)), period);
+}
+
+void TTaskScheduler::SafeAdd(ITaskRef task, TInstant expire) {
+ ThrowOnTaskLimitReached(Add(std::move(task), expire));
+}
+
+void TTaskScheduler::SafeAdd(IRepeatedTaskRef task, TDuration period) {
+ ThrowOnTaskLimitReached(Add(std::move(task), period));
+}
+
+void TTaskScheduler::SafeAddFunc(std::function<TInstant()> function, TInstant expire) {
+ ThrowOnTaskLimitReached(AddFunc(std::move(function), expire));
+}
+
+void TTaskScheduler::SafeAddRepeatedFunc(std::function<bool()> function, TDuration period) {
+ ThrowOnTaskLimitReached(AddRepeatedFunc(std::move(function), period));
+}
const bool debugOutput = false;