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