thread.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common_types.h"
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <thread>
  9. #include <condition_variable>
  10. #include <mutex>
  11. // This may not be defined outside _WIN32
  12. #ifndef _WIN32
  13. #ifndef INFINITE
  14. #define INFINITE 0xffffffff
  15. #endif
  16. //for gettimeofday and struct time(spec|val)
  17. #include <time.h>
  18. #include <sys/time.h>
  19. #include <unistd.h>
  20. #endif
  21. // Support for C++11's thread_local keyword was surprisingly spotty in compilers until very
  22. // recently. Fortunately, thread local variables have been well supported for compilers for a while,
  23. // but with semantics supporting only POD types, so we can use a few defines to get some amount of
  24. // backwards compat support.
  25. // WARNING: This only works correctly with POD types.
  26. #if defined(__clang__)
  27. # if !__has_feature(cxx_thread_local)
  28. # define thread_local __thread
  29. # endif
  30. #elif defined(__GNUC__)
  31. # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
  32. # define thread_local __thread
  33. # endif
  34. #elif defined(_MSC_VER)
  35. # if _MSC_VER < 1900
  36. # define thread_local __declspec(thread)
  37. # endif
  38. #endif
  39. namespace Common
  40. {
  41. int CurrentThreadId();
  42. void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask);
  43. void SetCurrentThreadAffinity(u32 mask);
  44. class Event {
  45. public:
  46. Event() : is_set(false) {}
  47. void Set() {
  48. std::lock_guard<std::mutex> lk(m_mutex);
  49. if (!is_set) {
  50. is_set = true;
  51. m_condvar.notify_one();
  52. }
  53. }
  54. void Wait() {
  55. std::unique_lock<std::mutex> lk(m_mutex);
  56. m_condvar.wait(lk, [&]{ return is_set; });
  57. is_set = false;
  58. }
  59. void Reset() {
  60. std::unique_lock<std::mutex> lk(m_mutex);
  61. // no other action required, since wait loops on the predicate and any lingering signal will get cleared on the first iteration
  62. is_set = false;
  63. }
  64. private:
  65. bool is_set;
  66. std::condition_variable m_condvar;
  67. std::mutex m_mutex;
  68. };
  69. class Barrier {
  70. public:
  71. Barrier(size_t count) : m_count(count), m_waiting(0) {}
  72. /// Blocks until all "count" threads have called Sync()
  73. void Sync() {
  74. std::unique_lock<std::mutex> lk(m_mutex);
  75. // TODO: broken when next round of Sync()s
  76. // is entered before all waiting threads return from the notify_all
  77. if (++m_waiting == m_count) {
  78. m_waiting = 0;
  79. m_condvar.notify_all();
  80. } else {
  81. m_condvar.wait(lk, [&]{ return m_waiting == 0; });
  82. }
  83. }
  84. private:
  85. std::condition_variable m_condvar;
  86. std::mutex m_mutex;
  87. const size_t m_count;
  88. size_t m_waiting;
  89. };
  90. void SleepCurrentThread(int ms);
  91. void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms
  92. // Use this function during a spin-wait to make the current thread
  93. // relax while another thread is working. This may be more efficient
  94. // than using events because event functions use kernel calls.
  95. inline void YieldCPU()
  96. {
  97. std::this_thread::yield();
  98. }
  99. void SetCurrentThreadName(const char *name);
  100. } // namespace Common