Thread.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // Thread.h
  3. //
  4. // $Id: //poco/svn/Foundation/include/Poco/Thread.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Thread
  9. //
  10. // Definition of the Thread class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_Thread_INCLUDED
  38. #define Foundation_Thread_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/Mutex.h"
  41. #if defined(POCO_OS_FAMILY_WINDOWS)
  42. #include "Poco/Thread_WIN32.h"
  43. #else
  44. #include "Poco/Thread_POSIX.h"
  45. #endif
  46. namespace Poco {
  47. class Runnable;
  48. class ThreadLocalStorage;
  49. class Foundation_API Thread: private ThreadImpl
  50. /// This class implements a platform-independent
  51. /// wrapper to an operating system thread.
  52. ///
  53. /// Every Thread object gets a unique (within
  54. /// its process) numeric thread ID.
  55. /// Furthermore, a thread can be assigned a name.
  56. /// The name of a thread can be changed at any time.
  57. {
  58. public:
  59. using ThreadImpl::Callable;
  60. enum Priority
  61. /// Thread priorities.
  62. {
  63. PRIO_LOWEST = PRIO_LOWEST_IMPL, /// The lowest thread priority.
  64. PRIO_LOW = PRIO_LOW_IMPL, /// A lower than normal thread priority.
  65. PRIO_NORMAL = PRIO_NORMAL_IMPL, /// The normal thread priority.
  66. PRIO_HIGH = PRIO_HIGH_IMPL, /// A higher than normal thread priority.
  67. PRIO_HIGHEST = PRIO_HIGHEST_IMPL /// The highest thread priority.
  68. };
  69. Thread();
  70. /// Creates a thread. Call start() to start it.
  71. Thread(const std::string& name);
  72. /// Creates a named thread. Call start() to start it.
  73. ~Thread();
  74. /// Destroys the thread.
  75. int id() const;
  76. /// Returns the unique thread ID of the thread.
  77. std::string name() const;
  78. /// Returns the name of the thread.
  79. std::string getName() const;
  80. /// Returns teh name of the thread.
  81. void setName(const std::string& name);
  82. /// Sets the name of the thread.
  83. void setPriority(Priority prio);
  84. /// Sets the thread's priority.
  85. ///
  86. /// Some platform only allow changing a thread's priority
  87. /// if the process has certain privileges.
  88. Priority getPriority() const;
  89. /// Returns the thread's priority.
  90. void setOSPriority(int prio);
  91. /// Sets the thread's priority, using an operating system specific
  92. /// priority value. Use getMinOSPriority() and getMaxOSPriority() to
  93. /// obtain mininum and maximum priority values.
  94. int getOSPriority() const;
  95. /// Returns the thread's priority, expressed as an operating system
  96. /// specific priority value.
  97. static int getMinOSPriority();
  98. /// Returns the mininum operating system-specific priority value,
  99. /// which can be passed to setOSPriority().
  100. static int getMaxOSPriority();
  101. /// Returns the maximum operating system-specific priority value,
  102. /// which can be passed to setOSPriority().
  103. void setStackSize(int size);
  104. /// Sets the thread's stack size in bytes.
  105. /// Setting the stack size to 0 will use the default stack size.
  106. /// Typically, the real stack size is rounded up to the nearest
  107. /// page size multiple.
  108. int getStackSize() const;
  109. /// Returns the thread's stack size in bytes.
  110. /// If the default stack size is used, 0 is returned.
  111. void start(Runnable& target);
  112. /// Starts the thread with the given target.
  113. void start(Callable target, void* pData = 0);
  114. /// Starts the thread with the given target and parameter.
  115. void join();
  116. /// Waits until the thread completes execution.
  117. /// If multiple threads try to join the same
  118. /// thread, the result is undefined.
  119. void join(long milliseconds);
  120. /// Waits for at most the given interval for the thread
  121. /// to complete. Throws a TimeoutException if the thread
  122. /// does not complete within the specified time interval.
  123. bool tryJoin(long milliseconds);
  124. /// Waits for at most the given interval for the thread
  125. /// to complete. Returns true if the thread has finished,
  126. /// false otherwise.
  127. bool isRunning() const;
  128. /// Returns true if the thread is running.
  129. static void sleep(long milliseconds);
  130. /// Suspends the current thread for the specified
  131. /// amount of time.
  132. static void yield();
  133. /// Yields cpu to other threads.
  134. static Thread* current();
  135. /// Returns the Thread object for the currently active thread.
  136. /// If the current thread is the main thread, 0 is returned.
  137. protected:
  138. ThreadLocalStorage& tls();
  139. /// Returns a reference to the thread's local storage.
  140. void clearTLS();
  141. /// Clears the thread's local storage.
  142. std::string makeName();
  143. /// Creates a unique name for a thread.
  144. static int uniqueId();
  145. /// Creates and returns a unique id for a thread.
  146. private:
  147. Thread(const Thread&);
  148. Thread& operator = (const Thread&);
  149. int _id;
  150. std::string _name;
  151. ThreadLocalStorage* _pTLS;
  152. mutable FastMutex _mutex;
  153. friend class ThreadLocalStorage;
  154. friend class PooledThread;
  155. };
  156. //
  157. // inlines
  158. //
  159. inline int Thread::id() const
  160. {
  161. return _id;
  162. }
  163. inline std::string Thread::name() const
  164. {
  165. FastMutex::ScopedLock lock(_mutex);
  166. return _name;
  167. }
  168. inline std::string Thread::getName() const
  169. {
  170. FastMutex::ScopedLock lock(_mutex);
  171. return _name;
  172. }
  173. inline bool Thread::isRunning() const
  174. {
  175. return isRunningImpl();
  176. }
  177. inline void Thread::sleep(long milliseconds)
  178. {
  179. sleepImpl(milliseconds);
  180. }
  181. inline void Thread::yield()
  182. {
  183. yieldImpl();
  184. }
  185. inline Thread* Thread::current()
  186. {
  187. return static_cast<Thread*>(currentImpl());
  188. }
  189. inline void Thread::setOSPriority(int prio)
  190. {
  191. setOSPriorityImpl(prio);
  192. }
  193. inline int Thread::getOSPriority() const
  194. {
  195. return getOSPriorityImpl();
  196. }
  197. inline int Thread::getMinOSPriority()
  198. {
  199. return ThreadImpl::getMinOSPriorityImpl();
  200. }
  201. inline int Thread::getMaxOSPriority()
  202. {
  203. return ThreadImpl::getMaxOSPriorityImpl();
  204. }
  205. inline void Thread::setStackSize(int size)
  206. {
  207. setStackSizeImpl(size);
  208. }
  209. inline int Thread::getStackSize() const
  210. {
  211. return getStackSizeImpl();
  212. }
  213. } // namespace Poco
  214. #endif // Foundation_Thread_INCLUDED