OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 // If there already *is* a Thread object corresponding to this thread, | 49 // If there already *is* a Thread object corresponding to this thread, |
50 // this method will return that. Otherwise it creates a new Thread | 50 // this method will return that. Otherwise it creates a new Thread |
51 // object whose wrapped() method will return true, and whose | 51 // object whose wrapped() method will return true, and whose |
52 // handle will, on Win32, be opened with only synchronization privileges - | 52 // handle will, on Win32, be opened with only synchronization privileges - |
53 // if you need more privilegs, rather than changing this method, please | 53 // if you need more privilegs, rather than changing this method, please |
54 // write additional code to adjust the privileges, or call a different | 54 // write additional code to adjust the privileges, or call a different |
55 // factory method of your own devising, because this one gets used in | 55 // factory method of your own devising, because this one gets used in |
56 // unexpected contexts (like inside browser plugins) and it would be a | 56 // unexpected contexts (like inside browser plugins) and it would be a |
57 // shame to break it. It is also conceivable on Win32 that we won't even | 57 // shame to break it. It is also conceivable on Win32 that we won't even |
58 // be able to get synchronization privileges, in which case the result | 58 // be able to get synchronization privileges, in which case the result |
59 // will have a NULL handle. | 59 // will have a null handle. |
60 Thread *WrapCurrentThread(); | 60 Thread *WrapCurrentThread(); |
61 void UnwrapCurrentThread(); | 61 void UnwrapCurrentThread(); |
62 | 62 |
63 private: | 63 private: |
64 #if defined(WEBRTC_POSIX) | 64 #if defined(WEBRTC_POSIX) |
65 pthread_key_t key_; | 65 pthread_key_t key_; |
66 #endif | 66 #endif |
67 | 67 |
68 #if defined(WEBRTC_WIN) | 68 #if defined(WEBRTC_WIN) |
69 DWORD key_; | 69 DWORD key_; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 bool IsCurrent() const { | 126 bool IsCurrent() const { |
127 return Current() == this; | 127 return Current() == this; |
128 } | 128 } |
129 | 129 |
130 // Sleeps the calling thread for the specified number of milliseconds, during | 130 // Sleeps the calling thread for the specified number of milliseconds, during |
131 // which time no processing is performed. Returns false if sleeping was | 131 // which time no processing is performed. Returns false if sleeping was |
132 // interrupted by a signal (POSIX only). | 132 // interrupted by a signal (POSIX only). |
133 static bool SleepMs(int millis); | 133 static bool SleepMs(int millis); |
134 | 134 |
135 // Sets the thread's name, for debugging. Must be called before Start(). | 135 // Sets the thread's name, for debugging. Must be called before Start(). |
136 // If |obj| is non-NULL, its value is appended to |name|. | 136 // If |obj| is non-null, its value is appended to |name|. |
137 const std::string& name() const { return name_; } | 137 const std::string& name() const { return name_; } |
138 bool SetName(const std::string& name, const void* obj); | 138 bool SetName(const std::string& name, const void* obj); |
139 | 139 |
140 // Starts the execution of the thread. | 140 // Starts the execution of the thread. |
141 bool Start(Runnable* runnable = NULL); | 141 bool Start(Runnable* runnable = nullptr); |
142 | 142 |
143 // Tells the thread to stop and waits until it is joined. | 143 // Tells the thread to stop and waits until it is joined. |
144 // Never call Stop on the current thread. Instead use the inherited Quit | 144 // Never call Stop on the current thread. Instead use the inherited Quit |
145 // function which will exit the base MessageQueue without terminating the | 145 // function which will exit the base MessageQueue without terminating the |
146 // underlying OS thread. | 146 // underlying OS thread. |
147 virtual void Stop(); | 147 virtual void Stop(); |
148 | 148 |
149 // By default, Thread::Run() calls ProcessMessages(kForever). To do other | 149 // By default, Thread::Run() calls ProcessMessages(kForever). To do other |
150 // work, override Run(). To receive and dispatch messages, call | 150 // work, override Run(). To receive and dispatch messages, call |
151 // ProcessMessages occasionally. | 151 // ProcessMessages occasionally. |
152 virtual void Run(); | 152 virtual void Run(); |
153 | 153 |
154 virtual void Send(const Location& posted_from, | 154 virtual void Send(const Location& posted_from, |
155 MessageHandler* phandler, | 155 MessageHandler* phandler, |
156 uint32_t id = 0, | 156 uint32_t id = 0, |
157 MessageData* pdata = NULL); | 157 MessageData* pdata = nullptr); |
158 | 158 |
159 // Convenience method to invoke a functor on another thread. Caller must | 159 // Convenience method to invoke a functor on another thread. Caller must |
160 // provide the |ReturnT| template argument, which cannot (easily) be deduced. | 160 // provide the |ReturnT| template argument, which cannot (easily) be deduced. |
161 // Uses Send() internally, which blocks the current thread until execution | 161 // Uses Send() internally, which blocks the current thread until execution |
162 // is complete. | 162 // is complete. |
163 // Ex: bool result = thread.Invoke<bool>(RTC_FROM_HERE, | 163 // Ex: bool result = thread.Invoke<bool>(RTC_FROM_HERE, |
164 // &MyFunctionReturningBool); | 164 // &MyFunctionReturningBool); |
165 // NOTE: This function can only be called when synchronous calls are allowed. | 165 // NOTE: This function can only be called when synchronous calls are allowed. |
166 // See ScopedDisallowBlockingCalls for details. | 166 // See ScopedDisallowBlockingCalls for details. |
167 template <class ReturnT, class FunctorT> | 167 template <class ReturnT, class FunctorT> |
168 ReturnT Invoke(const Location& posted_from, const FunctorT& functor) { | 168 ReturnT Invoke(const Location& posted_from, const FunctorT& functor) { |
169 FunctorMessageHandler<ReturnT, FunctorT> handler(functor); | 169 FunctorMessageHandler<ReturnT, FunctorT> handler(functor); |
170 InvokeInternal(posted_from, &handler); | 170 InvokeInternal(posted_from, &handler); |
171 return handler.MoveResult(); | 171 return handler.MoveResult(); |
172 } | 172 } |
173 | 173 |
174 // From MessageQueue | 174 // From MessageQueue |
175 void Clear(MessageHandler* phandler, | 175 void Clear(MessageHandler* phandler, |
176 uint32_t id = MQID_ANY, | 176 uint32_t id = MQID_ANY, |
177 MessageList* removed = NULL) override; | 177 MessageList* removed = nullptr) override; |
178 void ReceiveSends() override; | 178 void ReceiveSends() override; |
179 | 179 |
180 // ProcessMessages will process I/O and dispatch messages until: | 180 // ProcessMessages will process I/O and dispatch messages until: |
181 // 1) cms milliseconds have elapsed (returns true) | 181 // 1) cms milliseconds have elapsed (returns true) |
182 // 2) Stop() is called (returns false) | 182 // 2) Stop() is called (returns false) |
183 bool ProcessMessages(int cms); | 183 bool ProcessMessages(int cms); |
184 | 184 |
185 // Returns true if this is a thread that we created using the standard | 185 // Returns true if this is a thread that we created using the standard |
186 // constructor, false if it was created by a call to | 186 // constructor, false if it was created by a call to |
187 // ThreadManager::WrapCurrentThread(). The main thread of an application | 187 // ThreadManager::WrapCurrentThread(). The main thread of an application |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 // ThreadManager::Instance() cannot be used while ThreadManager is | 248 // ThreadManager::Instance() cannot be used while ThreadManager is |
249 // being created. | 249 // being created. |
250 // The method tries to get synchronization rights of the thread on Windows if | 250 // The method tries to get synchronization rights of the thread on Windows if |
251 // |need_synchronize_access| is true. | 251 // |need_synchronize_access| is true. |
252 bool WrapCurrentWithThreadManager(ThreadManager* thread_manager, | 252 bool WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
253 bool need_synchronize_access); | 253 bool need_synchronize_access); |
254 | 254 |
255 // Return true if the thread was started and hasn't yet stopped. | 255 // Return true if the thread was started and hasn't yet stopped. |
256 bool running() { return running_.Wait(0); } | 256 bool running() { return running_.Wait(0); } |
257 | 257 |
258 // Processes received "Send" requests. If |source| is not NULL, only requests | 258 // Processes received "Send" requests. If |source| is not null, only requests |
259 // from |source| are processed, otherwise, all requests are processed. | 259 // from |source| are processed, otherwise, all requests are processed. |
260 void ReceiveSendsFromThread(const Thread* source); | 260 void ReceiveSendsFromThread(const Thread* source); |
261 | 261 |
262 // If |source| is not NULL, pops the first "Send" message from |source| in | 262 // If |source| is not null, pops the first "Send" message from |source| in |
263 // |sendlist_|, otherwise, pops the first "Send" message of |sendlist_|. | 263 // |sendlist_|, otherwise, pops the first "Send" message of |sendlist_|. |
264 // The caller must lock |crit_| before calling. | 264 // The caller must lock |crit_| before calling. |
265 // Returns true if there is such a message. | 265 // Returns true if there is such a message. |
266 bool PopSendMessageFromThread(const Thread* source, _SendMessage* msg); | 266 bool PopSendMessageFromThread(const Thread* source, _SendMessage* msg); |
267 | 267 |
268 void InvokeInternal(const Location& posted_from, MessageHandler* handler); | 268 void InvokeInternal(const Location& posted_from, MessageHandler* handler); |
269 | 269 |
270 std::list<_SendMessage> sendlist_; | 270 std::list<_SendMessage> sendlist_; |
271 std::string name_; | 271 std::string name_; |
272 Event running_; // Signalled means running. | 272 Event running_; // Signalled means running. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 329 |
330 private: | 330 private: |
331 SocketServer* old_ss_; | 331 SocketServer* old_ss_; |
332 | 332 |
333 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SocketServerScope); | 333 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SocketServerScope); |
334 }; | 334 }; |
335 | 335 |
336 } // namespace rtc | 336 } // namespace rtc |
337 | 337 |
338 #endif // WEBRTC_BASE_THREAD_H_ | 338 #endif // WEBRTC_BASE_THREAD_H_ |
OLD | NEW |