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 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 23 | #include <memory> |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 24 | #include <thread> |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 25 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 26 | #include <thrift/concurrency/Monitor.h> |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 27 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 28 | namespace apache { |
| 29 | namespace thrift { |
| 30 | namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | |
| 32 | class Thread; |
| 33 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 34 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 35 | * Minimal runnable class. More or less analogous to java.lang.Runnable. |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 36 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 37 | * @version $Id:$ |
| 38 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 39 | class Runnable { |
| 40 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 41 | public: |
James E. King III | f95620d | 2019-01-28 18:15:13 -0500 | [diff] [blame] | 42 | virtual ~Runnable() = default; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 43 | virtual void run() = 0; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 44 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Gets the thread object that is hosting this runnable object - can return |
Jens Geyer | 1a8e048 | 2015-04-30 20:29:20 +0200 | [diff] [blame] | 47 | * an empty boost::shared pointer if no references remain on that thread object |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 48 | */ |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 49 | virtual std::shared_ptr<Thread> thread() { return thread_.lock(); } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 50 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Sets the thread that is executing this object. This is only meant for |
| 53 | * use by concrete implementations of Thread. |
| 54 | */ |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 55 | virtual void thread(std::shared_ptr<Thread> value) { thread_ = value; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 56 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 57 | private: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 58 | std::weak_ptr<Thread> thread_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 61 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 62 | * Minimal thread class. Returned by thread factory bound to a Runnable object |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 63 | * and ready to start execution. More or less analogous to java.lang.Thread |
| 64 | * (minus all the thread group, priority, mode and other baggage, since that |
| 65 | * is difficult to abstract across platforms and is left for platform-specific |
gaganso | dd7e117 | 2020-04-20 03:20:05 +0200 | [diff] [blame] | 66 | * ThreadFactory implementations to deal with |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 67 | * |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 68 | * @see apache::thrift::concurrency::ThreadFactory) |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 69 | */ |
rglarix | 10f2556 | 2020-10-07 18:34:51 +0200 | [diff] [blame] | 70 | class Thread : public std::enable_shared_from_this<Thread> { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 71 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 72 | public: |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 73 | typedef std::thread::id id_t; |
rglarix | 10f2556 | 2020-10-07 18:34:51 +0200 | [diff] [blame] | 74 | typedef void (*thread_funct_t)(std::shared_ptr<Thread> ); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 75 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 76 | enum STATE { uninitialized, starting, started, stopping, stopped }; |
| 77 | |
| 78 | static void threadMain(std::shared_ptr<Thread> thread); |
| 79 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 80 | static inline bool is_current(id_t t) { return t == std::this_thread::get_id(); } |
| 81 | static inline id_t get_current() { return std::this_thread::get_id(); } |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 82 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 83 | Thread(bool detached, std::shared_ptr<Runnable> runnable) |
| 84 | : state_(uninitialized), detached_(detached) { |
| 85 | this->_runnable = runnable; |
| 86 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 87 | |
rglarix | 10f2556 | 2020-10-07 18:34:51 +0200 | [diff] [blame] | 88 | virtual ~Thread() { |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 89 | if (!detached_ && thread_->joinable()) { |
| 90 | try { |
| 91 | join(); |
| 92 | } catch (...) { |
| 93 | // We're really hosed. |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | STATE getState() const |
| 99 | { |
| 100 | Synchronized sync(monitor_); |
| 101 | return state_; |
| 102 | } |
| 103 | |
| 104 | void setState(STATE newState) |
| 105 | { |
| 106 | Synchronized sync(monitor_); |
| 107 | state_ = newState; |
| 108 | |
| 109 | // unblock start() with the knowledge that the thread has actually |
| 110 | // started running, which avoids a race in detached threads. |
| 111 | if (newState == started) { |
| 112 | monitor_.notify(); |
| 113 | } |
| 114 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 115 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Starts the thread. Does platform specific thread creation and |
| 118 | * configuration then invokes the run method of the Runnable object bound |
| 119 | * to this thread. |
| 120 | */ |
rglarix | 10f2556 | 2020-10-07 18:34:51 +0200 | [diff] [blame] | 121 | virtual void start() { |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 122 | if (getState() != uninitialized) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | std::shared_ptr<Thread> selfRef = shared_from_this(); |
| 127 | setState(starting); |
| 128 | |
| 129 | Synchronized sync(monitor_); |
rglarix | 10f2556 | 2020-10-07 18:34:51 +0200 | [diff] [blame] | 130 | thread_ = std::unique_ptr<std::thread>(new std::thread(getThreadFunc(), selfRef)); |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 131 | |
| 132 | if (detached_) |
| 133 | thread_->detach(); |
| 134 | |
| 135 | // Wait for the thread to start and get far enough to grab everything |
| 136 | // that it needs from the calling context, thus absolving the caller |
| 137 | // from being required to hold on to runnable indefinitely. |
| 138 | monitor_.wait(); |
| 139 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 140 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 141 | /** |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 142 | * Join this thread. If this thread is joinable, the calling thread blocks |
| 143 | * until this thread completes. If the target thread is not joinable, then |
| 144 | * nothing happens. |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 145 | */ |
rglarix | 10f2556 | 2020-10-07 18:34:51 +0200 | [diff] [blame] | 146 | virtual void join() { |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 147 | if (!detached_ && state_ != uninitialized) { |
| 148 | thread_->join(); |
| 149 | } |
| 150 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 151 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 152 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 153 | * Gets the thread's platform-specific ID |
| 154 | */ |
cyy | f83d3f9 | 2019-01-12 12:53:12 +0800 | [diff] [blame] | 155 | Thread::id_t getId() const { return thread_.get() ? thread_->get_id() : std::thread::id(); } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 156 | |
| 157 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 158 | * Gets the runnable object this thread is hosting |
| 159 | */ |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 160 | std::shared_ptr<Runnable> runnable() const { return _runnable; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 161 | |
rglarix | 10f2556 | 2020-10-07 18:34:51 +0200 | [diff] [blame] | 162 | protected: |
| 163 | |
| 164 | virtual thread_funct_t getThreadFunc() const { |
| 165 | return threadMain; |
| 166 | } |
| 167 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 168 | private: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 169 | std::shared_ptr<Runnable> _runnable; |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 170 | std::unique_ptr<std::thread> thread_; |
| 171 | Monitor monitor_; |
| 172 | STATE state_; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 173 | bool detached_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 174 | }; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 175 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 176 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 180 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 181 | #endif // #ifndef _THRIFT_CONCURRENCY_THREAD_H_ |