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