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 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 4 | #include <boost/shared_ptr.hpp> |
| 5 | #include <boost/weak_ptr.hpp> |
| 6 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 7 | namespace facebook { namespace thrift { namespace concurrency { |
| 8 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 9 | using namespace boost; |
| 10 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 11 | class Thread; |
| 12 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 13 | /** Minimal runnable class. More or less analogous to java.lang.Runnable. |
| 14 | |
| 15 | @author marc |
| 16 | @version $Id:$ */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 17 | |
| 18 | class Runnable { |
| 19 | |
| 20 | public: |
| 21 | |
| 22 | virtual ~Runnable() {}; |
| 23 | |
| 24 | virtual void run() = 0; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 25 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 26 | /** Gets the thread object that is hosting this runnable object - can return an empty shared pointer if no references remain on thet thread object */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 27 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 28 | virtual shared_ptr<Thread> thread() {return _thread.lock();} |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 29 | |
| 30 | /** Sets the thread that is executing this object. This is only meant for use by concrete implementations of Thread. */ |
| 31 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 32 | virtual void thread(shared_ptr<Thread> value) {_thread = value;} |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 33 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 34 | private: |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 35 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 36 | weak_ptr<Thread> _thread; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /** 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 |
| 40 | (minus all the thread group, priority, mode and other baggage, since that is difficult to abstract across platforms and is left for platform-specific |
| 41 | ThreadFactory implemtations to deal with - @see facebook::thrift::concurrency::ThreadFactory) */ |
| 42 | |
| 43 | |
| 44 | class Thread { |
| 45 | |
| 46 | public: |
| 47 | |
| 48 | virtual ~Thread() {}; |
| 49 | |
| 50 | /** Starts the thread. Does platform specific thread creation and configuration then invokes the run method of the Runnable object bound to this |
| 51 | thread. */ |
| 52 | |
| 53 | virtual void start() = 0; |
| 54 | |
| 55 | /** Join this thread |
| 56 | |
| 57 | Current thread blocks until this target thread completes. */ |
| 58 | |
| 59 | virtual void join() = 0; |
| 60 | |
| 61 | /** Gets the runnable object this thread is hosting */ |
| 62 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 63 | virtual shared_ptr<Runnable> runnable() const {return _runnable;} |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 64 | |
| 65 | protected: |
| 66 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 67 | virtual void runnable(shared_ptr<Runnable> value) {_runnable = value;} |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 68 | |
| 69 | private: |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 70 | shared_ptr<Runnable> _runnable; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** Factory to create platform-specific thread object and bind them to Runnable object for execution */ |
| 74 | |
| 75 | class ThreadFactory { |
| 76 | |
| 77 | public: |
| 78 | |
| 79 | virtual ~ThreadFactory() {} |
| 80 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 81 | virtual shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | }}} // facebook::thrift::concurrency |
| 85 | |
| 86 | #endif // !defined(_concurrency_Thread_h_) |