1 | #ifndef MARS_MThread
|
---|
2 | #define MARS_MThread
|
---|
3 |
|
---|
4 | #ifndef ROOT_TThread
|
---|
5 | #include <TThread.h>
|
---|
6 | #endif
|
---|
7 |
|
---|
8 | class MThread // We don't want MThread to be derived from TObject
|
---|
9 | {
|
---|
10 | private:
|
---|
11 | TThread fThread;
|
---|
12 |
|
---|
13 | Int_t fNumCleanups;
|
---|
14 |
|
---|
15 | virtual void CleanUp() { }
|
---|
16 | static void MapCleanUp(void *arg)
|
---|
17 | {
|
---|
18 | MThread *th = (MThread*)arg;
|
---|
19 | th->CleanUp();
|
---|
20 | }
|
---|
21 |
|
---|
22 | virtual Int_t Thread() = 0;
|
---|
23 | static void *MapThread(void *arg)
|
---|
24 | {
|
---|
25 | // GetPriority(); High: -1 - -20, Norm: 0, Low: 1-20
|
---|
26 | // pthread_setschedprio(SelfId(), priority);
|
---|
27 | // 0: ok,
|
---|
28 |
|
---|
29 | TThread::CleanUpPush((void*)&MapCleanUp, arg);
|
---|
30 |
|
---|
31 | MThread *th = (MThread*)arg;
|
---|
32 | return reinterpret_cast<void*>(th->Thread());
|
---|
33 | }
|
---|
34 |
|
---|
35 | public:
|
---|
36 | MThread(TThread::EPriority pri = TThread::kNormalPriority) :
|
---|
37 | fThread(MapThread, this, pri), fNumCleanups(0) { }
|
---|
38 | MThread(const char *thname, TThread::EPriority pri = TThread::kNormalPriority) :
|
---|
39 | fThread(thname, MapThread, this, pri), fNumCleanups(0) { }
|
---|
40 | virtual ~MThread() { }
|
---|
41 |
|
---|
42 | // Setter: Thread control
|
---|
43 | Int_t RunThread(void *arg = 0) { return fThread.Run(arg); }
|
---|
44 |
|
---|
45 | // Send cancel request and wait for cancellation
|
---|
46 | // 13 is returned if thread is not running,
|
---|
47 | // the return code of Join otherwise
|
---|
48 | Int_t CancelThread(void **ret = 0) {
|
---|
49 | const Int_t rc = fThread.Kill();
|
---|
50 | if (rc==13) // Thread not running
|
---|
51 | return rc;
|
---|
52 | return fThread.Join(ret);
|
---|
53 | }
|
---|
54 |
|
---|
55 | // Int_t Kill() { return fThread.Kill(); }
|
---|
56 | // Long_t Join(void **ret = 0) { return fThread.Join(ret); }
|
---|
57 |
|
---|
58 | // void SetPriority(EPriority pri)
|
---|
59 | // void Delete(Option_t *option="") { TObject::Delete(option); }
|
---|
60 |
|
---|
61 | // Getter
|
---|
62 | TThread::EState GetThreadState() const { return fThread.GetState(); }
|
---|
63 | TString GetThreadStateStr() const;
|
---|
64 | Long_t GetThreadId() const { return fThread.GetId(); }
|
---|
65 | // EPriority GetPriority() const { return fPriority; }
|
---|
66 |
|
---|
67 | Bool_t IsThreadRunning() const { return fThread.GetState()==TThread::kRunningState; }
|
---|
68 | Bool_t IsThreadCanceled() const { return fThread.GetState()==TThread::kCancelingState; }
|
---|
69 |
|
---|
70 | // This is a version of usleep which is a cancel point
|
---|
71 | static void Sleep(UInt_t us)
|
---|
72 | {
|
---|
73 | TThread::SetCancelOn();
|
---|
74 | usleep(us);
|
---|
75 | TThread::SetCancelOff();
|
---|
76 | }
|
---|
77 | };
|
---|
78 |
|
---|
79 | #endif
|
---|