Thread.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/util/StdTypes.h>
  7. #include <anki/util/Array.h>
  8. #include <anki/util/NonCopyable.h>
  9. #include <atomic>
  10. namespace anki
  11. {
  12. /// @addtogroup util_thread
  13. /// @{
  14. /// Thread implementation
  15. class Thread : public NonCopyable
  16. {
  17. public:
  18. using Id = U64;
  19. /// It holds some information to be passed to the thread's callback
  20. class Info
  21. {
  22. public:
  23. void* m_userData;
  24. const char* m_threadName;
  25. };
  26. /// The type of the tread callback
  27. using Callback = Error (*)(Info&);
  28. /// Create a thread with or without a name
  29. /// @param[in] name The name of the new thread. Can be nullptr.
  30. Thread(const char* name);
  31. ~Thread();
  32. /// Start the thread
  33. /// @param userData The user data of the thread callback
  34. /// @param callback The thread callback that will be executed
  35. /// @param pinToCore Pin the thread to a core.
  36. void start(void* userData, Callback callback, I pinToCore = -1);
  37. /// Wait for the thread to finish
  38. /// @return The error code of the thread's callback
  39. ANKI_USE_RESULT Error join();
  40. /// Identify the current thread
  41. static Id getCurrentThreadId();
  42. anki_internal:
  43. const char* getName() const
  44. {
  45. return &m_name[0];
  46. }
  47. void* getUserData() const
  48. {
  49. return m_userData;
  50. }
  51. Callback getCallback() const
  52. {
  53. return m_callback;
  54. }
  55. private:
  56. void* m_impl = nullptr; ///< The system native type
  57. Array<char, 32> m_name; ///< The name of the thread
  58. Callback m_callback = nullptr; ///< The callback
  59. void* m_userData = nullptr; ///< The user date to pass to the callback
  60. #if ANKI_ASSERTIONS
  61. Bool8 m_started = false;
  62. #endif
  63. };
  64. /// Mutual exclusion primitive.
  65. class Mutex : public NonCopyable
  66. {
  67. friend class ConditionVariable;
  68. public:
  69. Mutex();
  70. ~Mutex();
  71. /// Lock
  72. void lock();
  73. /// Try lock
  74. /// @return True if it was locked successfully
  75. Bool tryLock();
  76. /// Unlock
  77. void unlock();
  78. private:
  79. void* m_impl = nullptr; ///< The system native type
  80. };
  81. /// Condition variable.
  82. class ConditionVariable : public NonCopyable
  83. {
  84. public:
  85. ConditionVariable();
  86. ~ConditionVariable();
  87. /// Signal one thread
  88. void notifyOne();
  89. /// Signal all threads
  90. void notifyAll();
  91. /// Bock until signaled.
  92. /// @param mtx The mutex.
  93. void wait(Mutex& mtx);
  94. private:
  95. void* m_impl = nullptr; ///< The system native type
  96. };
  97. /// Mutual exclusion primitive. Like Mutex. It's better than Mutex only if the
  98. /// critical section will be executed in a very short period of time.
  99. class SpinLock : public NonCopyable
  100. {
  101. public:
  102. /// Lock
  103. void lock()
  104. {
  105. while(m_lock.test_and_set(std::memory_order_acquire))
  106. {
  107. }
  108. }
  109. /// Unlock
  110. void unlock()
  111. {
  112. m_lock.clear(std::memory_order_release);
  113. }
  114. private:
  115. std::atomic_flag m_lock = ATOMIC_FLAG_INIT;
  116. };
  117. /// Lock guard. When constructed it locks a TMutex and unlocks it when it gets
  118. /// destroyed.
  119. /// @tparam TMutex Can be Mutex or SpinLock.
  120. template<typename TMutex>
  121. class LockGuard
  122. {
  123. public:
  124. LockGuard(TMutex& mtx)
  125. : m_mtx(&mtx)
  126. {
  127. m_mtx->lock();
  128. }
  129. ~LockGuard()
  130. {
  131. m_mtx->unlock();
  132. }
  133. private:
  134. TMutex* m_mtx;
  135. };
  136. /// A barrier for thread synchronization. It works almost like boost::barrier.
  137. class Barrier : public NonCopyable
  138. {
  139. public:
  140. Barrier(U32 count);
  141. ~Barrier();
  142. /// Wait until all threads call wait().
  143. Bool wait();
  144. private:
  145. void* m_impl = nullptr;
  146. };
  147. /// @}
  148. } // end namespace anki