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 | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 13 | namespace facebook { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 14 | |
| 15 | class Thread; |
| 16 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 17 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 18 | * Minimal runnable class. More or less analogous to java.lang.Runnable. |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 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 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 46 | * Minimal thread class. Returned by thread factory bound to a Runnable object |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 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 { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 55 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 56 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 57 | |
| 58 | typedef unsigned long long id_t; |
| 59 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 60 | virtual ~Thread() {}; |
| 61 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Starts the thread. Does platform specific thread creation and |
| 64 | * configuration then invokes the run method of the Runnable object bound |
| 65 | * to this thread. |
| 66 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | virtual void start() = 0; |
| 68 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 69 | /** |
| 70 | * Join this thread. Current thread blocks until this target thread |
| 71 | * completes. |
| 72 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 73 | virtual void join() = 0; |
| 74 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 75 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 76 | * Gets the thread's platform-specific ID |
| 77 | */ |
| 78 | virtual id_t id() = 0; |
| 79 | |
| 80 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 81 | * Gets the runnable object this thread is hosting |
| 82 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 83 | virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 84 | |
| 85 | protected: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 86 | virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 89 | boost::shared_ptr<Runnable> _runnable; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 90 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Factory to create platform-specific thread object and bind them to Runnable |
| 95 | * object for execution |
| 96 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 97 | class ThreadFactory { |
| 98 | |
| 99 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 100 | virtual ~ThreadFactory() {} |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 101 | virtual boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const = 0; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame^] | 102 | |
| 103 | /** Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread */ |
| 104 | |
| 105 | static const Thread::id_t unknown_thread_id; |
| 106 | |
| 107 | virtual Thread::id_t currentThreadId() const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | }}} // facebook::thrift::concurrency |
| 111 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 112 | #endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_ |