As suspected, it didn't work
[progcomp2012.git] / judge / manager / thread_util.h
1 #ifndef THREAD_UTIL_H
2 #define THREAD_UTIL_H
3
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #include <sstream>
8 #include <string>
9 #include <pthread.h> //Needed for threading
10 #include <signal.h> //Needed for killing the threads (why not in pthread.h?)
11
12 #include <assert.h>
13
14
15 class Thread
16 {
17         public:
18                 Thread() : finished(false), thread(0),  started(false)
19                 {
20                         
21                 }
22
23                 virtual void Start() = 0;
24         protected:
25                 void Start(void* (ThreadFunction)(void*))
26                 {
27                         assert(!started);
28                         started = true;
29                         pthread_create(&(thread), NULL, ThreadFunction, (void*)(this));
30                         pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
31                 }
32
33         public:
34                 void Stop()
35                 {
36                         assert(started);
37                         if (!finished)
38                                 pthread_cancel(thread);
39                         
40                         pthread_join(thread, NULL);
41                         started = false;
42                 }
43
44                 virtual ~Thread()
45                 {
46                         if (started)
47                                 Stop();
48                 }
49
50                 bool Finished() const {return finished;}
51         protected:
52                 
53                 bool finished;
54
55         private:
56                 pthread_t thread;
57         protected:
58                 bool started;
59 };
60
61 class GetterThread : public Thread
62 {
63         public:
64                 GetterThread(FILE * newStream, std::string & newBuffer) : Thread(), stream(newStream), buffer(newBuffer)
65                 {
66                         
67                 }
68
69                 virtual ~GetterThread()
70                 {
71
72                 }
73
74                 virtual void Start() {assert(&buffer != NULL); Thread::Start(GetMessage);}
75
76         private:
77                 FILE * stream;
78         public:
79                 std::string & buffer;
80
81                 pthread_t thread;
82                 static pthread_mutex_t mutex;
83                 static void * GetMessage(void * p);
84
85 };
86
87
88 class TimerThread : public Thread
89 {
90         public:
91                 TimerThread(int newCount) : Thread(), count(newCount)
92                 {
93                         
94                 }
95
96                 virtual ~TimerThread()
97                 {
98
99                 }
100
101                 virtual void Start() {Thread::Start(Timeout);}
102
103         private:
104                 int count;
105
106                 static void * Timeout(void * p);
107
108 };
109
110 #endif //THREAD_UTIL_H
111
112 //EOF
113
114
115

UCC git Repository :: git.ucc.asn.au