util_thread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_THREAD_H__
  17. #define __UTIL_THREAD_H__
  18. #include <thread>
  19. #include <mutex>
  20. #include <condition_variable>
  21. #include <functional>
  22. #include <queue>
  23. #ifdef _WIN32
  24. # include "util_windows.h"
  25. #else
  26. # include <pthread.h>
  27. #endif
  28. #ifdef __APPLE__
  29. # include <libkern/OSAtomic.h>
  30. #endif
  31. #include "util/util_function.h"
  32. CCL_NAMESPACE_BEGIN
  33. typedef std::mutex thread_mutex;
  34. typedef std::unique_lock<std::mutex> thread_scoped_lock;
  35. typedef std::condition_variable thread_condition_variable;
  36. /* Own thread implementation similar to std::thread, so we can set a
  37. * custom stack size on macOS. */
  38. class thread {
  39. public:
  40. /* NOTE: Node index of -1 means that affinity will be inherited from the
  41. * parent thread and no override on top of that will happen. */
  42. thread(function<void()> run_cb, int node = -1);
  43. ~thread();
  44. static void *run(void *arg);
  45. bool join();
  46. protected:
  47. function<void()> run_cb_;
  48. #ifdef __APPLE__
  49. pthread_t pthread_id;
  50. #else
  51. std::thread std_thread;
  52. #endif
  53. bool joined_;
  54. int node_;
  55. };
  56. /* Own wrapper around pthread's spin lock to make it's use easier. */
  57. class thread_spin_lock {
  58. public:
  59. #ifdef __APPLE__
  60. inline thread_spin_lock()
  61. {
  62. spin_ = OS_SPINLOCK_INIT;
  63. }
  64. inline void lock()
  65. {
  66. OSSpinLockLock(&spin_);
  67. }
  68. inline void unlock()
  69. {
  70. OSSpinLockUnlock(&spin_);
  71. }
  72. #elif defined(_WIN32)
  73. inline thread_spin_lock()
  74. {
  75. const DWORD SPIN_COUNT = 50000;
  76. InitializeCriticalSectionAndSpinCount(&cs_, SPIN_COUNT);
  77. }
  78. inline ~thread_spin_lock()
  79. {
  80. DeleteCriticalSection(&cs_);
  81. }
  82. inline void lock()
  83. {
  84. EnterCriticalSection(&cs_);
  85. }
  86. inline void unlock()
  87. {
  88. LeaveCriticalSection(&cs_);
  89. }
  90. #else
  91. inline thread_spin_lock()
  92. {
  93. pthread_spin_init(&spin_, 0);
  94. }
  95. inline ~thread_spin_lock()
  96. {
  97. pthread_spin_destroy(&spin_);
  98. }
  99. inline void lock()
  100. {
  101. pthread_spin_lock(&spin_);
  102. }
  103. inline void unlock()
  104. {
  105. pthread_spin_unlock(&spin_);
  106. }
  107. #endif
  108. protected:
  109. #ifdef __APPLE__
  110. OSSpinLock spin_;
  111. #elif defined(_WIN32)
  112. CRITICAL_SECTION cs_;
  113. #else
  114. pthread_spinlock_t spin_;
  115. #endif
  116. };
  117. class thread_scoped_spin_lock {
  118. public:
  119. explicit thread_scoped_spin_lock(thread_spin_lock &lock) : lock_(lock)
  120. {
  121. lock_.lock();
  122. }
  123. ~thread_scoped_spin_lock()
  124. {
  125. lock_.unlock();
  126. }
  127. /* TODO(sergey): Implement manual control over lock/unlock. */
  128. protected:
  129. thread_spin_lock &lock_;
  130. };
  131. CCL_NAMESPACE_END
  132. #endif /* __UTIL_THREAD_H__ */