diff options
author | max42 <max42@yandex-team.com> | 2023-07-29 00:02:16 +0300 |
---|---|---|
committer | max42 <max42@yandex-team.com> | 2023-07-29 00:02:16 +0300 |
commit | 73b89de71748a21e102d27b9f3ed1bf658766cb5 (patch) | |
tree | 188bbd2d622fa91cdcbb1b6d6d77fbc84a0646f5 /library/cpp/threading/cron/cron.cpp | |
parent | 528e321bcc2a2b67b53aeba58c3bd88305a141ee (diff) | |
download | ydb-73b89de71748a21e102d27b9f3ed1bf658766cb5.tar.gz |
YT-19210: expose YQL shared library for YT.
After this, a new target libyqlplugin.so appears. in open-source cmake build.
Diff in open-source YDB repo looks like the following: https://paste.yandex-team.ru/f302bdb4-7ef2-4362-91c7-6ca45f329264
Diffstat (limited to 'library/cpp/threading/cron/cron.cpp')
-rw-r--r-- | library/cpp/threading/cron/cron.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/library/cpp/threading/cron/cron.cpp b/library/cpp/threading/cron/cron.cpp new file mode 100644 index 0000000000..e7c1c59735 --- /dev/null +++ b/library/cpp/threading/cron/cron.cpp @@ -0,0 +1,69 @@ +#include "cron.h" + +#include <library/cpp/deprecated/atomic/atomic.h> + +#include <util/system/thread.h> +#include <util/system/event.h> + +using namespace NCron; + +namespace { + struct TPeriodicHandle: public IHandle { + inline TPeriodicHandle(TJob job, TDuration interval, const TString& threadName) + : Job(job) + , Interval(interval) + , Done(false) + { + TThread::TParams params(DoRun, this); + if (!threadName.empty()) { + params.SetName(threadName); + } + Thread = MakeHolder<TThread>(params); + Thread->Start(); + } + + static inline void* DoRun(void* data) noexcept { + ((TPeriodicHandle*)data)->Run(); + + return nullptr; + } + + inline void Run() noexcept { + while (true) { + Job(); + + Event.WaitT(Interval); + + if (AtomicGet(Done)) { + return; + } + } + } + + ~TPeriodicHandle() override { + AtomicSet(Done, true); + Event.Signal(); + Thread->Join(); + } + + TJob Job; + TDuration Interval; + TManualEvent Event; + TAtomic Done; + THolder<TThread> Thread; + }; +} + +IHandlePtr NCron::StartPeriodicJob(TJob job) { + return NCron::StartPeriodicJob(job, TDuration::Seconds(0), ""); +} + +IHandlePtr NCron::StartPeriodicJob(TJob job, TDuration interval) { + return NCron::StartPeriodicJob(job, interval, ""); +} + +IHandlePtr NCron::StartPeriodicJob(TJob job, TDuration interval, const TString& threadName) { + return new TPeriodicHandle(job, interval, threadName); +} + +IHandle::~IHandle() = default; |