blob: 2416887e12d661bb6f8ac59d5cd70cdbe8af113c [file] [log] [blame]
Marc Slemko66949872006-07-15 01:52:39 +00001#if !defined(_concurrency_Thread_h_)
2#define _concurrency_Thread_h_ 1
3
4namespace facebook { namespace thrift { namespace concurrency {
5
6class Thread;
7
Marc Slemko0e53ccd2006-07-17 23:51:05 +00008/** Minimal runnable class. More or less analogous to java.lang.Runnable.
9
10 @author marc
11 @version $Id:$ */
Marc Slemko66949872006-07-15 01:52:39 +000012
13class Runnable {
14
15 public:
16
17 virtual ~Runnable() {};
18
19 virtual void run() = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000020
21 virtual Thread* thread() {return _thread;}
22
23 private:
24
25 /** Sets the thread that is executing this object. This is only meant for use by concrete implementations of Thread. */
26
27 friend class Thread;
28
29 virtual void thread(Thread* value) {_thread = value;}
30
31 Thread* _thread;
Marc Slemko66949872006-07-15 01:52:39 +000032};
33
34/** Minimal thread class. Returned by thread factory bound to a Runnable object and ready to start execution. More or less analogous to java.lang.Thread
35 (minus all the thread group, priority, mode and other baggage, since that is difficult to abstract across platforms and is left for platform-specific
36 ThreadFactory implemtations to deal with - @see facebook::thrift::concurrency::ThreadFactory) */
37
38
39class Thread {
40
41 public:
42
43 virtual ~Thread() {};
44
45 /** Starts the thread. Does platform specific thread creation and configuration then invokes the run method of the Runnable object bound to this
46 thread. */
47
48 virtual void start() = 0;
49
50 /** Join this thread
51
52 Current thread blocks until this target thread completes. */
53
54 virtual void join() = 0;
55
56 /** Gets the runnable object this thread is hosting */
57
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000058 virtual Runnable* runnable() const {return _runnable;}
59
60 protected:
61
62 virtual void runnable(Runnable* value, bool x=false) {_runnable = value; _runnable->thread(this);}
63
64 private:
65
66 Runnable* _runnable;
Marc Slemko66949872006-07-15 01:52:39 +000067};
68
69/** Factory to create platform-specific thread object and bind them to Runnable object for execution */
70
71class ThreadFactory {
72
73 public:
74
75 virtual ~ThreadFactory() {}
76
77 virtual Thread* newThread(Runnable* runnable) const = 0;
78};
79
80}}} // facebook::thrift::concurrency
81
82#endif // !defined(_concurrency_Thread_h_)