You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_THREAD_H_
  28. #define TALK_BASE_THREAD_H_
  29. #include <algorithm>
  30. #include <list>
  31. #include <string>
  32. #include <vector>
  33. #ifdef POSIX
  34. #include <pthread.h>
  35. #endif
  36. #include "talk/base/constructormagic.h"
  37. #include "talk/base/messagequeue.h"
  38. #ifdef WIN32
  39. #include "talk/base/win32.h"
  40. #endif
  41. namespace talk_base {
  42. class Thread;
  43. class ThreadManager {
  44. public:
  45. ThreadManager();
  46. ~ThreadManager();
  47. static ThreadManager* Instance();
  48. Thread* CurrentThread();
  49. void SetCurrentThread(Thread* thread);
  50. // Returns a thread object with its thread_ ivar set
  51. // to whatever the OS uses to represent the thread.
  52. // If there already *is* a Thread object corresponding to this thread,
  53. // this method will return that. Otherwise it creates a new Thread
  54. // object whose wrapped() method will return true, and whose
  55. // handle will, on Win32, be opened with only synchronization privileges -
  56. // if you need more privilegs, rather than changing this method, please
  57. // write additional code to adjust the privileges, or call a different
  58. // factory method of your own devising, because this one gets used in
  59. // unexpected contexts (like inside browser plugins) and it would be a
  60. // shame to break it. It is also conceivable on Win32 that we won't even
  61. // be able to get synchronization privileges, in which case the result
  62. // will have a NULL handle.
  63. Thread *WrapCurrentThread();
  64. void UnwrapCurrentThread();
  65. private:
  66. #ifdef POSIX
  67. pthread_key_t key_;
  68. #endif
  69. #ifdef WIN32
  70. DWORD key_;
  71. #endif
  72. DISALLOW_COPY_AND_ASSIGN(ThreadManager);
  73. };
  74. class Thread;
  75. struct _SendMessage {
  76. _SendMessage() {}
  77. Thread *thread;
  78. Message msg;
  79. bool *ready;
  80. };
  81. enum ThreadPriority {
  82. PRIORITY_IDLE = -1,
  83. PRIORITY_NORMAL = 0,
  84. PRIORITY_ABOVE_NORMAL = 1,
  85. PRIORITY_HIGH = 2,
  86. };
  87. class Runnable {
  88. public:
  89. virtual ~Runnable() {}
  90. virtual void Run(Thread* thread) = 0;
  91. protected:
  92. Runnable() {}
  93. private:
  94. DISALLOW_COPY_AND_ASSIGN(Runnable);
  95. };
  96. class Thread : public MessageQueue {
  97. public:
  98. Thread(SocketServer* ss = NULL);
  99. virtual ~Thread();
  100. static inline Thread* Current() {
  101. return ThreadManager::Instance()->CurrentThread();
  102. }
  103. bool IsCurrent() const {
  104. return ThreadManager::Instance()->CurrentThread() == this;
  105. }
  106. // Sleeps the calling thread for the specified number of milliseconds, during
  107. // which time no processing is performed. Returns false if sleeping was
  108. // interrupted by a signal (POSIX only).
  109. static bool SleepMs(int millis);
  110. // Sets the thread's name, for debugging. Must be called before Start().
  111. // If |obj| is non-NULL, its value is appended to |name|.
  112. const std::string& name() const { return name_; }
  113. bool SetName(const std::string& name, const void* obj);
  114. // Sets the thread's priority. Must be called before Start().
  115. ThreadPriority priority() const { return priority_; }
  116. bool SetPriority(ThreadPriority priority);
  117. // Starts the execution of the thread.
  118. bool started() const { return started_; }
  119. bool Start(Runnable* runnable = NULL);
  120. // Used for fire-and-forget threads. Deletes this thread object when the
  121. // Run method returns.
  122. void Release() {
  123. delete_self_when_complete_ = true;
  124. }
  125. // Tells the thread to stop and waits until it is joined.
  126. // Never call Stop on the current thread. Instead use the inherited Quit
  127. // function which will exit the base MessageQueue without terminating the
  128. // underlying OS thread.
  129. virtual void Stop();
  130. // By default, Thread::Run() calls ProcessMessages(kForever). To do other
  131. // work, override Run(). To receive and dispatch messages, call
  132. // ProcessMessages occasionally.
  133. virtual void Run();
  134. virtual void Send(MessageHandler *phandler, uint32 id = 0,
  135. MessageData *pdata = NULL);
  136. // From MessageQueue
  137. virtual void Clear(MessageHandler *phandler, uint32 id = MQID_ANY,
  138. MessageList* removed = NULL);
  139. virtual void ReceiveSends();
  140. // ProcessMessages will process I/O and dispatch messages until:
  141. // 1) cms milliseconds have elapsed (returns true)
  142. // 2) Stop() is called (returns false)
  143. bool ProcessMessages(int cms);
  144. // Returns true if this is a thread that we created using the standard
  145. // constructor, false if it was created by a call to
  146. // ThreadManager::WrapCurrentThread(). The main thread of an application
  147. // is generally not owned, since the OS representation of the thread
  148. // obviously exists before we can get to it.
  149. // You cannot call Start on non-owned threads.
  150. bool IsOwned();
  151. #ifdef WIN32
  152. HANDLE GetHandle() const {
  153. return thread_;
  154. }
  155. DWORD GetId() const {
  156. return thread_id_;
  157. }
  158. #elif POSIX
  159. pthread_t GetPThread() {
  160. return thread_;
  161. }
  162. #endif
  163. // This method should be called when thread is created using non standard
  164. // method, like derived implementation of talk_base::Thread and it can not be
  165. // started by calling Start(). This will set started flag to true and
  166. // owned to false. This must be called from the current thread.
  167. // NOTE: These methods should be used by the derived classes only, added here
  168. // only for testing.
  169. bool WrapCurrent();
  170. void UnwrapCurrent();
  171. protected:
  172. // Blocks the calling thread until this thread has terminated.
  173. void Join();
  174. private:
  175. static void *PreRun(void *pv);
  176. // ThreadManager calls this instead WrapCurrent() because
  177. // ThreadManager::Instance() cannot be used while ThreadManager is
  178. // being created.
  179. bool WrapCurrentWithThreadManager(ThreadManager* thread_manager);
  180. std::list<_SendMessage> sendlist_;
  181. std::string name_;
  182. ThreadPriority priority_;
  183. bool started_;
  184. bool has_sends_;
  185. #ifdef POSIX
  186. pthread_t thread_;
  187. #endif
  188. #ifdef WIN32
  189. HANDLE thread_;
  190. DWORD thread_id_;
  191. #endif
  192. bool owned_;
  193. bool delete_self_when_complete_;
  194. friend class ThreadManager;
  195. DISALLOW_COPY_AND_ASSIGN(Thread);
  196. };
  197. // AutoThread automatically installs itself at construction
  198. // uninstalls at destruction, if a Thread object is
  199. // _not already_ associated with the current OS thread.
  200. class AutoThread : public Thread {
  201. public:
  202. AutoThread(SocketServer* ss = 0);
  203. virtual ~AutoThread();
  204. private:
  205. DISALLOW_COPY_AND_ASSIGN(AutoThread);
  206. };
  207. // Win32 extension for threads that need to use COM
  208. #ifdef WIN32
  209. class ComThread : public Thread {
  210. public:
  211. ComThread() {}
  212. protected:
  213. virtual void Run();
  214. private:
  215. DISALLOW_COPY_AND_ASSIGN(ComThread);
  216. };
  217. #endif
  218. // Provides an easy way to install/uninstall a socketserver on a thread.
  219. class SocketServerScope {
  220. public:
  221. explicit SocketServerScope(SocketServer* ss) {
  222. old_ss_ = Thread::Current()->socketserver();
  223. Thread::Current()->set_socketserver(ss);
  224. }
  225. ~SocketServerScope() {
  226. Thread::Current()->set_socketserver(old_ss_);
  227. }
  228. private:
  229. SocketServer* old_ss_;
  230. DISALLOW_IMPLICIT_CONSTRUCTORS(SocketServerScope);
  231. };
  232. } // namespace talk_base
  233. #endif // TALK_BASE_THREAD_H_