Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 1 | #if !defined(_concurrency_Thread_h_) |
| 2 | #define _concurrency_Thread_h_ 1 |
| 3 | |
| 4 | namespace facebook { namespace thrift { namespace concurrency { |
| 5 | |
| 6 | class Thread; |
| 7 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 8 | /** Minimal runnable class. More or less analogous to java.lang.Runnable. |
| 9 | |
| 10 | @author marc |
| 11 | @version $Id:$ */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 12 | |
| 13 | class Runnable { |
| 14 | |
| 15 | public: |
| 16 | |
| 17 | virtual ~Runnable() {}; |
| 18 | |
| 19 | virtual void run() = 0; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 20 | |
| 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 Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 32 | }; |
| 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 | |
| 39 | class 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 Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 58 | 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 Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** Factory to create platform-specific thread object and bind them to Runnable object for execution */ |
| 70 | |
| 71 | class 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_) |