David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_CONCURRENCY_THREAD_H_ |
| 21 | #define _THRIFT_CONCURRENCY_THREAD_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | |
David Reiss | 7247b8c | 2009-04-02 23:05:40 +0000 | [diff] [blame] | 23 | #include <stdint.h> |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 24 | #include <boost/shared_ptr.hpp> |
| 25 | #include <boost/weak_ptr.hpp> |
| 26 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 27 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 28 | |
| 29 | class Thread; |
| 30 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 32 | * Minimal runnable class. More or less analogous to java.lang.Runnable. |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 33 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 34 | * @version $Id:$ |
| 35 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 36 | class Runnable { |
| 37 | |
| 38 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 39 | virtual ~Runnable() {}; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | virtual void run() = 0; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 41 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Gets the thread object that is hosting this runnable object - can return |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 44 | * 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] | 45 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 46 | virtual boost::shared_ptr<Thread> thread() { return thread_.lock(); } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 47 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 48 | /** |
| 49 | * Sets the thread that is executing this object. This is only meant for |
| 50 | * use by concrete implementations of Thread. |
| 51 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 52 | virtual void thread(boost::shared_ptr<Thread> value) { thread_ = value; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 53 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 54 | private: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 55 | boost::weak_ptr<Thread> thread_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 58 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 59 | * Minimal thread class. Returned by thread factory bound to a Runnable object |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 60 | * and ready to start execution. More or less analogous to java.lang.Thread |
| 61 | * (minus all the thread group, priority, mode and other baggage, since that |
| 62 | * is difficult to abstract across platforms and is left for platform-specific |
| 63 | * ThreadFactory implemtations to deal with |
| 64 | * |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 65 | * @see apache::thrift::concurrency::ThreadFactory) |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 66 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | class Thread { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 68 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 69 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 70 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 71 | typedef uint64_t id_t; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 72 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 73 | virtual ~Thread() {}; |
| 74 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Starts the thread. Does platform specific thread creation and |
| 77 | * configuration then invokes the run method of the Runnable object bound |
| 78 | * to this thread. |
| 79 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 80 | virtual void start() = 0; |
| 81 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 82 | /** |
| 83 | * Join this thread. Current thread blocks until this target thread |
| 84 | * completes. |
| 85 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 86 | virtual void join() = 0; |
| 87 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 88 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 89 | * Gets the thread's platform-specific ID |
| 90 | */ |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 91 | virtual id_t getId() = 0; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 94 | * Gets the runnable object this thread is hosting |
| 95 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 96 | virtual boost::shared_ptr<Runnable> runnable() const { return _runnable; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 97 | |
| 98 | protected: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 99 | virtual void runnable(boost::shared_ptr<Runnable> value) { _runnable = value; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 100 | |
| 101 | private: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 102 | boost::shared_ptr<Runnable> _runnable; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 103 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 106 | /** |
| 107 | * Factory to create platform-specific thread object and bind them to Runnable |
| 108 | * object for execution |
| 109 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 110 | class ThreadFactory { |
| 111 | |
| 112 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 113 | virtual ~ThreadFactory() {} |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 114 | 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] | 115 | |
| 116 | /** Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread */ |
| 117 | |
| 118 | static const Thread::id_t unknown_thread_id; |
| 119 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 120 | virtual Thread::id_t getCurrentThreadId() const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 123 | }}} // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 124 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 125 | #endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_ |