thread.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifdef __HAIKU__
  31. #include <kernel/OS.h>
  32. #endif
  33. /*
  34. * On platforms using pthreads, these five priority classes correlate to
  35. * even divisions between the minimum and maximum reported thread priority.
  36. */
  37. #if !defined(_WIN32)
  38. #define THREAD_PRIORITY_LOWEST 0
  39. #define THREAD_PRIORITY_BELOW_NORMAL 1
  40. #define THREAD_PRIORITY_NORMAL 2
  41. #define THREAD_PRIORITY_ABOVE_NORMAL 3
  42. #define THREAD_PRIORITY_HIGHEST 4
  43. #endif
  44. class Thread {
  45. public:
  46. Thread(const std::string &name="");
  47. virtual ~Thread();
  48. DISABLE_CLASS_COPY(Thread)
  49. /*
  50. * Begins execution of a new thread at the pure virtual method Thread::run().
  51. * Execution of the thread is guaranteed to have started after this function
  52. * returns.
  53. */
  54. bool start();
  55. /*
  56. * Requests that the thread exit gracefully.
  57. * Returns immediately; thread execution is guaranteed to be complete after
  58. * a subsequent call to Thread::wait.
  59. */
  60. bool stop();
  61. /*
  62. * Waits for thread to finish.
  63. * Note: This does not stop a thread, you have to do this on your own.
  64. * Returns false immediately if the thread is not started or has been waited
  65. * on before.
  66. */
  67. bool wait();
  68. /*
  69. * Returns true if the calling thread is this Thread object.
  70. */
  71. bool isCurrentThread() { return std::this_thread::get_id() == getThreadId(); }
  72. bool isRunning() { return m_running; }
  73. bool stopRequested() { return m_request_stop; }
  74. std::thread::id getThreadId() { return m_thread_obj->get_id(); }
  75. /*
  76. * Gets the thread return value.
  77. * Returns true if the thread has exited and the return value was available,
  78. * or false if the thread has yet to finish.
  79. */
  80. bool getReturnValue(void **ret);
  81. /*
  82. * Binds (if possible, otherwise sets the affinity of) the thread to the
  83. * specific processor specified by proc_number.
  84. */
  85. bool bindToProcessor(unsigned int proc_number);
  86. /*
  87. * Sets the thread priority to the specified priority.
  88. *
  89. * prio can be one of: THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
  90. * THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST.
  91. * On Windows, any of the other priorites as defined by SetThreadPriority
  92. * are supported as well.
  93. *
  94. * Note that it may be necessary to first set the threading policy or
  95. * scheduling algorithm to one that supports thread priorities if not
  96. * supported by default, otherwise this call will have no effect.
  97. */
  98. bool setPriority(int prio);
  99. /*
  100. * Sets the currently executing thread's name to where supported; useful
  101. * for debugging.
  102. */
  103. static void setName(const std::string &name);
  104. /*
  105. * Returns the number of processors/cores configured and active on this machine.
  106. */
  107. static unsigned int getNumberOfProcessors();
  108. protected:
  109. std::string m_name;
  110. virtual void *run() = 0;
  111. private:
  112. std::thread::native_handle_type getThreadHandle()
  113. { return m_thread_obj->native_handle(); }
  114. static void threadProc(Thread *thr);
  115. void *m_retval = nullptr;
  116. bool m_joinable = false;
  117. std::atomic<bool> m_request_stop;
  118. std::atomic<bool> m_running;
  119. std::mutex m_mutex;
  120. std::mutex m_start_finished_mutex;
  121. std::thread *m_thread_obj = nullptr;
  122. #ifdef _AIX
  123. // For AIX, there does not exist any mapping from pthread_t to tid_t
  124. // available to us, so we maintain one ourselves. This is set on thread start.
  125. tid_t m_kernel_thread_id;
  126. #endif
  127. };