aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/deprecated/threadable/threadable.h
diff options
context:
space:
mode:
authormonster <monster@ydb.tech>2022-07-07 14:41:37 +0300
committermonster <monster@ydb.tech>2022-07-07 14:41:37 +0300
commit06e5c21a835c0e923506c4ff27929f34e00761c2 (patch)
tree75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/deprecated/threadable/threadable.h
parent03f024c4412e3aa613bb543cf1660176320ba8f4 (diff)
downloadydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz
fix ya.make
Diffstat (limited to 'library/cpp/deprecated/threadable/threadable.h')
-rw-r--r--library/cpp/deprecated/threadable/threadable.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/library/cpp/deprecated/threadable/threadable.h b/library/cpp/deprecated/threadable/threadable.h
new file mode 100644
index 0000000000..77fb5878ff
--- /dev/null
+++ b/library/cpp/deprecated/threadable/threadable.h
@@ -0,0 +1,81 @@
+#pragma once
+
+#include <util/system/thread.h>
+#include <util/system/defaults.h>
+#include <util/generic/ptr.h>
+
+//deprecated. use future.h instead
+template <class T>
+class TThreadable: public T {
+public:
+ using TThreadProc = TThread::TThreadProc;
+
+ inline TThreadable()
+ : Arg(nullptr)
+ , ThreadInit(nullptr)
+ {
+ }
+
+ inline int Run(void* arg = nullptr, size_t stackSize = 0, TThreadProc init = DefInit) {
+ if (Thread_) {
+ return 1;
+ }
+
+ Arg = arg;
+ ThreadInit = init;
+
+ try {
+ THolder<TThread> thread(new TThread(TThread::TParams(Dispatch, this, stackSize)));
+
+ thread->Start();
+ Thread_.Swap(thread);
+ } catch (...) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+ inline int Join(void** result = nullptr) {
+ if (!Thread_) {
+ return 1;
+ }
+
+ try {
+ void* ret = Thread_->Join();
+
+ if (result) {
+ *result = ret;
+ }
+
+ Thread_.Destroy();
+
+ return 0;
+ } catch (...) {
+ }
+
+ return 1;
+ }
+
+protected:
+ static TThreadable<T>* This(void* ptr) {
+ return (TThreadable<T>*)ptr;
+ }
+
+ static void* Dispatch(void* ptr) {
+ void* result = This(ptr)->ThreadInit(This(ptr)->Arg);
+
+ return result ? result : (void*)(size_t)This(ptr)->T::Run(This(ptr)->Arg);
+ }
+
+ static void* DefInit(void*) {
+ return nullptr;
+ }
+
+public:
+ void* Arg;
+ TThreadProc ThreadInit;
+
+private:
+ THolder<TThread> Thread_;
+};