blob: 77fb5878ffe69e859a7cb09506a143235d9cb7ae (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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_;
};
|