henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_THREAD_H_ |
| 12 | #define RTC_BASE_THREAD_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <list> |
| 16 | #include <memory> |
| 17 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include <type_traits> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 20 | #if defined(WEBRTC_POSIX) |
| 21 | #include <pthread.h> |
| 22 | #endif |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/constructor_magic.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 24 | #include "rtc_base/location.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/message_handler.h" |
| 26 | #include "rtc_base/message_queue.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/platform_thread_types.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/socket_server.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 29 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 30 | |
| 31 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "rtc_base/win32.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | namespace rtc { |
| 36 | |
| 37 | class Thread; |
| 38 | |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 39 | namespace rtc_thread_internal { |
| 40 | |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 41 | class MessageLikeTask : public MessageData { |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 42 | public: |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 43 | virtual void Run() = 0; |
| 44 | }; |
| 45 | |
| 46 | template <class FunctorT> |
| 47 | class MessageWithFunctor final : public MessageLikeTask { |
| 48 | public: |
| 49 | explicit MessageWithFunctor(FunctorT&& functor) |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 50 | : functor_(std::forward<FunctorT>(functor)) {} |
| 51 | |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 52 | void Run() override { functor_(); } |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 53 | |
| 54 | private: |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 55 | ~MessageWithFunctor() override {} |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 56 | |
| 57 | typename std::remove_reference<FunctorT>::type functor_; |
| 58 | |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 59 | RTC_DISALLOW_COPY_AND_ASSIGN(MessageWithFunctor); |
| 60 | }; |
| 61 | |
| 62 | class MessageHandlerWithTask final : public MessageHandler { |
| 63 | public: |
| 64 | MessageHandlerWithTask() = default; |
| 65 | |
| 66 | void OnMessage(Message* msg) override { |
| 67 | static_cast<MessageLikeTask*>(msg->pdata)->Run(); |
| 68 | delete msg->pdata; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | ~MessageHandlerWithTask() override {} |
| 73 | |
| 74 | RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandlerWithTask); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace rtc_thread_internal |
| 78 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | class ThreadManager { |
| 80 | public: |
| 81 | static const int kForever = -1; |
| 82 | |
| 83 | // Singleton, constructor and destructor are private. |
| 84 | static ThreadManager* Instance(); |
| 85 | |
| 86 | Thread* CurrentThread(); |
| 87 | void SetCurrentThread(Thread* thread); |
| 88 | |
| 89 | // Returns a thread object with its thread_ ivar set |
| 90 | // to whatever the OS uses to represent the thread. |
| 91 | // If there already *is* a Thread object corresponding to this thread, |
| 92 | // this method will return that. Otherwise it creates a new Thread |
| 93 | // object whose wrapped() method will return true, and whose |
| 94 | // handle will, on Win32, be opened with only synchronization privileges - |
| 95 | // if you need more privilegs, rather than changing this method, please |
| 96 | // write additional code to adjust the privileges, or call a different |
| 97 | // factory method of your own devising, because this one gets used in |
| 98 | // unexpected contexts (like inside browser plugins) and it would be a |
| 99 | // shame to break it. It is also conceivable on Win32 that we won't even |
| 100 | // be able to get synchronization privileges, in which case the result |
| 101 | // will have a null handle. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | Thread* WrapCurrentThread(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 103 | void UnwrapCurrentThread(); |
| 104 | |
| 105 | bool IsMainThread(); |
| 106 | |
| 107 | private: |
| 108 | ThreadManager(); |
| 109 | ~ThreadManager(); |
| 110 | |
| 111 | #if defined(WEBRTC_POSIX) |
| 112 | pthread_key_t key_; |
| 113 | #endif |
| 114 | |
| 115 | #if defined(WEBRTC_WIN) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 116 | const DWORD key_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | // The thread to potentially autowrap. |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 120 | const PlatformThreadRef main_thread_ref_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 121 | |
| 122 | RTC_DISALLOW_COPY_AND_ASSIGN(ThreadManager); |
| 123 | }; |
| 124 | |
| 125 | struct _SendMessage { |
| 126 | _SendMessage() {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 127 | Thread* thread; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 128 | Message msg; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 129 | bool* ready; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | class Runnable { |
| 133 | public: |
| 134 | virtual ~Runnable() {} |
| 135 | virtual void Run(Thread* thread) = 0; |
| 136 | |
| 137 | protected: |
| 138 | Runnable() {} |
| 139 | |
| 140 | private: |
| 141 | RTC_DISALLOW_COPY_AND_ASSIGN(Runnable); |
| 142 | }; |
| 143 | |
| 144 | // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). |
| 145 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 146 | class RTC_LOCKABLE Thread : public MessageQueue { |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 147 | public: |
Erik Språng | 6e70f18 | 2019-05-16 10:27:17 +0000 | [diff] [blame] | 148 | // DEPRECATED. |
| 149 | // The default constructor should not be used because it hides whether or |
| 150 | // not a socket server will be associated with the thread. Most instances |
| 151 | // of Thread do actually not need one, so please use either of the Create* |
| 152 | // methods to construct an instance of Thread. |
| 153 | Thread(); |
| 154 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 155 | explicit Thread(SocketServer* ss); |
| 156 | explicit Thread(std::unique_ptr<SocketServer> ss); |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 157 | // Constructors meant for subclasses; they should call DoInit themselves and |
| 158 | // pass false for |do_init|, so that DoInit is called only on the fully |
| 159 | // instantiated class, which avoids a vptr data race. |
| 160 | Thread(SocketServer* ss, bool do_init); |
| 161 | Thread(std::unique_ptr<SocketServer> ss, bool do_init); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 162 | |
| 163 | // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or |
| 164 | // guarantee Stop() is explicitly called before the subclass is destroyed). |
| 165 | // This is required to avoid a data race between the destructor modifying the |
| 166 | // vtable, and the Thread::PreRun calling the virtual method Run(). |
| 167 | ~Thread() override; |
| 168 | |
| 169 | static std::unique_ptr<Thread> CreateWithSocketServer(); |
| 170 | static std::unique_ptr<Thread> Create(); |
| 171 | static Thread* Current(); |
| 172 | |
| 173 | // Used to catch performance regressions. Use this to disallow blocking calls |
| 174 | // (Invoke) for a given scope. If a synchronous call is made while this is in |
| 175 | // effect, an assert will be triggered. |
| 176 | // Note that this is a single threaded class. |
| 177 | class ScopedDisallowBlockingCalls { |
| 178 | public: |
| 179 | ScopedDisallowBlockingCalls(); |
Sebastian Jansson | 9debe5a | 2019-03-22 15:42:38 +0100 | [diff] [blame] | 180 | ScopedDisallowBlockingCalls(const ScopedDisallowBlockingCalls&) = delete; |
| 181 | ScopedDisallowBlockingCalls& operator=(const ScopedDisallowBlockingCalls&) = |
| 182 | delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 183 | ~ScopedDisallowBlockingCalls(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 184 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 185 | private: |
| 186 | Thread* const thread_; |
| 187 | const bool previous_state_; |
| 188 | }; |
| 189 | |
| 190 | bool IsCurrent() const; |
| 191 | |
| 192 | // Sleeps the calling thread for the specified number of milliseconds, during |
| 193 | // which time no processing is performed. Returns false if sleeping was |
| 194 | // interrupted by a signal (POSIX only). |
| 195 | static bool SleepMs(int millis); |
| 196 | |
| 197 | // Sets the thread's name, for debugging. Must be called before Start(). |
| 198 | // If |obj| is non-null, its value is appended to |name|. |
| 199 | const std::string& name() const { return name_; } |
| 200 | bool SetName(const std::string& name, const void* obj); |
| 201 | |
| 202 | // Starts the execution of the thread. |
| 203 | bool Start(Runnable* runnable = nullptr); |
| 204 | |
| 205 | // Tells the thread to stop and waits until it is joined. |
| 206 | // Never call Stop on the current thread. Instead use the inherited Quit |
| 207 | // function which will exit the base MessageQueue without terminating the |
| 208 | // underlying OS thread. |
| 209 | virtual void Stop(); |
| 210 | |
| 211 | // By default, Thread::Run() calls ProcessMessages(kForever). To do other |
| 212 | // work, override Run(). To receive and dispatch messages, call |
| 213 | // ProcessMessages occasionally. |
| 214 | virtual void Run(); |
| 215 | |
| 216 | virtual void Send(const Location& posted_from, |
| 217 | MessageHandler* phandler, |
| 218 | uint32_t id = 0, |
| 219 | MessageData* pdata = nullptr); |
| 220 | |
| 221 | // Convenience method to invoke a functor on another thread. Caller must |
| 222 | // provide the |ReturnT| template argument, which cannot (easily) be deduced. |
| 223 | // Uses Send() internally, which blocks the current thread until execution |
| 224 | // is complete. |
| 225 | // Ex: bool result = thread.Invoke<bool>(RTC_FROM_HERE, |
| 226 | // &MyFunctionReturningBool); |
| 227 | // NOTE: This function can only be called when synchronous calls are allowed. |
| 228 | // See ScopedDisallowBlockingCalls for details. |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 229 | // NOTE: Blocking invokes are DISCOURAGED, consider if what you're doing can |
| 230 | // be achieved with PostTask() and callbacks instead. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 231 | template <class ReturnT, class FunctorT> |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 232 | ReturnT Invoke(const Location& posted_from, FunctorT&& functor) { |
| 233 | FunctorMessageHandler<ReturnT, FunctorT> handler( |
| 234 | std::forward<FunctorT>(functor)); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 235 | InvokeInternal(posted_from, &handler); |
| 236 | return handler.MoveResult(); |
| 237 | } |
| 238 | |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 239 | // Posts a task to invoke the functor on |this| thread asynchronously, i.e. |
| 240 | // without blocking the thread that invoked PostTask(). Ownership of |functor| |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 241 | // is passed and (usually, see below) destroyed on |this| thread after it is |
| 242 | // invoked. |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 243 | // Requirements of FunctorT: |
| 244 | // - FunctorT is movable. |
| 245 | // - FunctorT implements "T operator()()" or "T operator()() const" for some T |
| 246 | // (if T is not void, the return value is discarded on |this| thread). |
| 247 | // - FunctorT has a public destructor that can be invoked from |this| thread |
| 248 | // after operation() has been invoked. |
| 249 | // - The functor must not cause the thread to quit before PostTask() is done. |
| 250 | // |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 251 | // Destruction of the functor/task mimics what TaskQueue::PostTask does: If |
| 252 | // the task is run, it will be destroyed on |this| thread. However, if there |
| 253 | // are pending tasks by the time the Thread is destroyed, or a task is posted |
| 254 | // to a thread that is quitting, the task is destroyed immediately, on the |
| 255 | // calling thread. Destroying the Thread only blocks for any currently running |
| 256 | // task to complete. Note that TQ abstraction is even vaguer on how |
| 257 | // destruction happens in these cases, allowing destruction to happen |
| 258 | // asynchronously at a later time and on some arbitrary thread. So to ease |
| 259 | // migration, don't depend on Thread::PostTask destroying un-run tasks |
| 260 | // immediately. |
| 261 | // |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 262 | // Example - Calling a class method: |
| 263 | // class Foo { |
| 264 | // public: |
| 265 | // void DoTheThing(); |
| 266 | // }; |
| 267 | // Foo foo; |
| 268 | // thread->PostTask(RTC_FROM_HERE, Bind(&Foo::DoTheThing, &foo)); |
| 269 | // |
| 270 | // Example - Calling a lambda function: |
| 271 | // thread->PostTask(RTC_FROM_HERE, |
| 272 | // [&x, &y] { x.TrackComputations(y.Compute()); }); |
| 273 | template <class FunctorT> |
| 274 | void PostTask(const Location& posted_from, FunctorT&& functor) { |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame^] | 275 | // Allocate at first call, never deallocate. |
| 276 | static auto* const handler = |
| 277 | new rtc_thread_internal::MessageHandlerWithTask; |
| 278 | Post(posted_from, handler, 0, |
| 279 | new rtc_thread_internal::MessageWithFunctor<FunctorT>( |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 280 | std::forward<FunctorT>(functor))); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 283 | // From MessageQueue |
Niels Möller | 8909a63 | 2018-09-06 08:42:44 +0200 | [diff] [blame] | 284 | bool IsProcessingMessagesForTesting() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 285 | void Clear(MessageHandler* phandler, |
| 286 | uint32_t id = MQID_ANY, |
| 287 | MessageList* removed = nullptr) override; |
| 288 | void ReceiveSends() override; |
| 289 | |
| 290 | // ProcessMessages will process I/O and dispatch messages until: |
| 291 | // 1) cms milliseconds have elapsed (returns true) |
| 292 | // 2) Stop() is called (returns false) |
| 293 | bool ProcessMessages(int cms); |
| 294 | |
| 295 | // Returns true if this is a thread that we created using the standard |
| 296 | // constructor, false if it was created by a call to |
| 297 | // ThreadManager::WrapCurrentThread(). The main thread of an application |
| 298 | // is generally not owned, since the OS representation of the thread |
| 299 | // obviously exists before we can get to it. |
| 300 | // You cannot call Start on non-owned threads. |
| 301 | bool IsOwned(); |
| 302 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 303 | // Expose private method IsRunning() for tests. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 304 | // |
| 305 | // DANGER: this is a terrible public API. Most callers that might want to |
| 306 | // call this likely do not have enough control/knowledge of the Thread in |
| 307 | // question to guarantee that the returned value remains true for the duration |
| 308 | // of whatever code is conditionally executing because of the return value! |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 309 | bool RunningForTest() { return IsRunning(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 310 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 311 | // These functions are public to avoid injecting test hooks. Don't call them |
| 312 | // outside of tests. |
| 313 | // This method should be called when thread is created using non standard |
| 314 | // method, like derived implementation of rtc::Thread and it can not be |
| 315 | // started by calling Start(). This will set started flag to true and |
| 316 | // owned to false. This must be called from the current thread. |
| 317 | bool WrapCurrent(); |
| 318 | void UnwrapCurrent(); |
| 319 | |
Karl Wiberg | 3256225 | 2019-02-21 13:38:30 +0100 | [diff] [blame] | 320 | // Sets the per-thread allow-blocking-calls flag to false; this is |
| 321 | // irrevocable. Must be called on this thread. |
| 322 | void DisallowBlockingCalls() { SetAllowBlockingCalls(false); } |
| 323 | |
| 324 | #ifdef WEBRTC_ANDROID |
| 325 | // Sets the per-thread allow-blocking-calls flag to true, sidestepping the |
| 326 | // invariants upheld by DisallowBlockingCalls() and |
| 327 | // ScopedDisallowBlockingCalls. Must be called on this thread. |
| 328 | void DEPRECATED_AllowBlockingCalls() { SetAllowBlockingCalls(true); } |
| 329 | #endif |
| 330 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 331 | protected: |
| 332 | // Same as WrapCurrent except that it never fails as it does not try to |
| 333 | // acquire the synchronization access of the thread. The caller should never |
| 334 | // call Stop() or Join() on this thread. |
| 335 | void SafeWrapCurrent(); |
| 336 | |
| 337 | // Blocks the calling thread until this thread has terminated. |
| 338 | void Join(); |
| 339 | |
| 340 | static void AssertBlockingIsAllowedOnCurrentThread(); |
| 341 | |
| 342 | friend class ScopedDisallowBlockingCalls; |
| 343 | |
| 344 | private: |
| 345 | struct ThreadInit { |
| 346 | Thread* thread; |
| 347 | Runnable* runnable; |
| 348 | }; |
| 349 | |
Karl Wiberg | 3256225 | 2019-02-21 13:38:30 +0100 | [diff] [blame] | 350 | // Sets the per-thread allow-blocking-calls flag and returns the previous |
| 351 | // value. Must be called on this thread. |
| 352 | bool SetAllowBlockingCalls(bool allow); |
| 353 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 354 | #if defined(WEBRTC_WIN) |
| 355 | static DWORD WINAPI PreRun(LPVOID context); |
| 356 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 357 | static void* PreRun(void* pv); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 358 | #endif |
| 359 | |
| 360 | // ThreadManager calls this instead WrapCurrent() because |
| 361 | // ThreadManager::Instance() cannot be used while ThreadManager is |
| 362 | // being created. |
| 363 | // The method tries to get synchronization rights of the thread on Windows if |
| 364 | // |need_synchronize_access| is true. |
| 365 | bool WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 366 | bool need_synchronize_access); |
| 367 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 368 | // Return true if the thread is currently running. |
| 369 | bool IsRunning(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 370 | |
| 371 | // Processes received "Send" requests. If |source| is not null, only requests |
| 372 | // from |source| are processed, otherwise, all requests are processed. |
| 373 | void ReceiveSendsFromThread(const Thread* source); |
| 374 | |
| 375 | // If |source| is not null, pops the first "Send" message from |source| in |
| 376 | // |sendlist_|, otherwise, pops the first "Send" message of |sendlist_|. |
| 377 | // The caller must lock |crit_| before calling. |
| 378 | // Returns true if there is such a message. |
| 379 | bool PopSendMessageFromThread(const Thread* source, _SendMessage* msg); |
| 380 | |
| 381 | void InvokeInternal(const Location& posted_from, MessageHandler* handler); |
| 382 | |
| 383 | std::list<_SendMessage> sendlist_; |
| 384 | std::string name_; |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 385 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 386 | // TODO(tommi): Add thread checks for proper use of control methods. |
| 387 | // Ideally we should be able to just use PlatformThread. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 388 | |
| 389 | #if defined(WEBRTC_POSIX) |
Tommi | 6cea2b0 | 2017-12-04 18:51:16 +0100 | [diff] [blame] | 390 | pthread_t thread_ = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 391 | #endif |
| 392 | |
| 393 | #if defined(WEBRTC_WIN) |
Tommi | 6cea2b0 | 2017-12-04 18:51:16 +0100 | [diff] [blame] | 394 | HANDLE thread_ = nullptr; |
| 395 | DWORD thread_id_ = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 396 | #endif |
| 397 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 398 | // Indicates whether or not ownership of the worker thread lies with |
| 399 | // this instance or not. (i.e. owned_ == !wrapped). |
| 400 | // Must only be modified when the worker thread is not running. |
| 401 | bool owned_ = true; |
| 402 | |
| 403 | // Only touched from the worker thread itself. |
| 404 | bool blocking_calls_allowed_ = true; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 405 | |
| 406 | friend class ThreadManager; |
| 407 | |
| 408 | RTC_DISALLOW_COPY_AND_ASSIGN(Thread); |
| 409 | }; |
| 410 | |
| 411 | // AutoThread automatically installs itself at construction |
| 412 | // uninstalls at destruction, if a Thread object is |
| 413 | // _not already_ associated with the current OS thread. |
| 414 | |
| 415 | class AutoThread : public Thread { |
| 416 | public: |
| 417 | AutoThread(); |
| 418 | ~AutoThread() override; |
| 419 | |
| 420 | private: |
| 421 | RTC_DISALLOW_COPY_AND_ASSIGN(AutoThread); |
| 422 | }; |
| 423 | |
| 424 | // AutoSocketServerThread automatically installs itself at |
| 425 | // construction and uninstalls at destruction. If a Thread object is |
| 426 | // already associated with the current OS thread, it is temporarily |
| 427 | // disassociated and restored by the destructor. |
| 428 | |
| 429 | class AutoSocketServerThread : public Thread { |
| 430 | public: |
| 431 | explicit AutoSocketServerThread(SocketServer* ss); |
| 432 | ~AutoSocketServerThread() override; |
| 433 | |
| 434 | private: |
| 435 | rtc::Thread* old_thread_; |
| 436 | |
| 437 | RTC_DISALLOW_COPY_AND_ASSIGN(AutoSocketServerThread); |
| 438 | }; |
| 439 | |
| 440 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 441 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 442 | #endif // RTC_BASE_THREAD_H_ |