thread.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. This file is a part of the JThread package, which contains some object-
  3. oriented thread wrappers for different thread implementations.
  4. Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
  5. Permission is hereby granted, free of charge, to any person obtaining a
  6. copy of this software and associated documentation files (the "Software"),
  7. to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. and/or sell copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  16. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. */
  21. #pragma once
  22. #include "util/basic_macros.h"
  23. #include <string>
  24. #include <atomic>
  25. #include <thread>
  26. #include <mutex>
  27. #ifdef _AIX
  28. #include <sys/thread.h> // for tid_t
  29. #endif
  30. /*
  31. * On platforms using pthreads, these five priority classes correlate to
  32. * even divisions between the minimum and maximum reported thread priority.
  33. */
  34. #if !defined(_WIN32)
  35. #define THREAD_PRIORITY_LOWEST 0
  36. #define THREAD_PRIORITY_BELOW_NORMAL 1
  37. #define THREAD_PRIORITY_NORMAL 2
  38. #define THREAD_PRIORITY_ABOVE_NORMAL 3
  39. #define THREAD_PRIORITY_HIGHEST 4
  40. #endif
  41. class Thread {
  42. public:
  43. Thread(const std::string &name="");
  44. virtual ~Thread();
  45. DISABLE_CLASS_COPY(Thread)
  46. /*
  47. * Begins execution of a new thread at the pure virtual method Thread::run().
  48. * Execution of the thread is guaranteed to have started after this function
  49. * returns.
  50. */
  51. bool start();
  52. /*
  53. * Requests that the thread exit gracefully.
  54. * Returns immediately; thread execution is guaranteed to be complete after
  55. * a subsequent call to Thread::wait.
  56. */
  57. bool stop();
  58. /*
  59. * Immediately terminates the thread.
  60. * This should be used with extreme caution, as the thread will not have
  61. * any opportunity to release resources it may be holding (such as memory
  62. * or locks).
  63. */
  64. bool kill();
  65. /*
  66. * Waits for thread to finish.
  67. * Note: This does not stop a thread, you have to do this on your own.
  68. * Returns false immediately if the thread is not started or has been waited
  69. * on before.
  70. */
  71. bool wait();
  72. /*
  73. * Returns true if the calling thread is this Thread object.
  74. */
  75. bool isCurrentThread() { return std::this_thread::get_id() == getThreadId(); }
  76. bool isRunning() { return m_running; }
  77. bool stopRequested() { return m_request_stop; }
  78. std::thread::id getThreadId() { return m_thread_obj->get_id(); }
  79. /*
  80. * Gets the thread return value.
  81. * Returns true if the thread has exited and the return value was available,
  82. * or false if the thread has yet to finish.
  83. */
  84. bool getReturnValue(void **ret);
  85. /*
  86. * Binds (if possible, otherwise sets the affinity of) the thread to the
  87. * specific processor specified by proc_number.
  88. */
  89. bool bindToProcessor(unsigned int proc_number);
  90. /*
  91. * Sets the thread priority to the specified priority.
  92. *
  93. * prio can be one of: THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
  94. * THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST.
  95. * On Windows, any of the other priorites as defined by SetThreadPriority
  96. * are supported as well.
  97. *
  98. * Note that it may be necessary to first set the threading policy or
  99. * scheduling algorithm to one that supports thread priorities if not
  100. * supported by default, otherwise this call will have no effect.
  101. */
  102. bool setPriority(int prio);
  103. /*
  104. * Sets the currently executing thread's name to where supported; useful
  105. * for debugging.
  106. */
  107. static void setName(const std::string &name);
  108. /*
  109. * Returns the number of processors/cores configured and active on this machine.
  110. */
  111. static unsigned int getNumberOfProcessors();
  112. protected:
  113. std::string m_name;
  114. virtual void *run() = 0;
  115. private:
  116. std::thread::native_handle_type getThreadHandle()
  117. { return m_thread_obj->native_handle(); }
  118. static void threadProc(Thread *thr);
  119. void *m_retval = nullptr;
  120. bool m_joinable = false;
  121. std::atomic<bool> m_request_stop;
  122. std::atomic<bool> m_running;
  123. std::mutex m_mutex;
  124. std::mutex m_start_finished_mutex;
  125. std::thread *m_thread_obj = nullptr;
  126. #ifdef _AIX
  127. // For AIX, there does not exist any mapping from pthread_t to tid_t
  128. // available to us, so we maintain one ourselves. This is set on thread start.
  129. tid_t m_kernel_thread_id;
  130. #endif
  131. };