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