thread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <condition_variable>
  6. #include <cstddef>
  7. #include <mutex>
  8. #include <thread>
  9. #include "common/common_types.h"
  10. // Support for C++11's thread_local keyword was surprisingly spotty in compilers until very
  11. // recently. Fortunately, thread local variables have been well supported for compilers for a while,
  12. // but with semantics supporting only POD types, so we can use a few defines to get some amount of
  13. // backwards compat support.
  14. // WARNING: This only works correctly with POD types.
  15. #if defined(__clang__)
  16. #if !__has_feature(cxx_thread_local)
  17. #define thread_local __thread
  18. #endif
  19. #elif defined(__GNUC__)
  20. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
  21. #define thread_local __thread
  22. #endif
  23. #elif defined(_MSC_VER)
  24. #if _MSC_VER < 1900
  25. #define thread_local __declspec(thread)
  26. #endif
  27. #endif
  28. namespace Common {
  29. int CurrentThreadId();
  30. void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask);
  31. void SetCurrentThreadAffinity(u32 mask);
  32. class Event {
  33. public:
  34. Event() : is_set(false) {}
  35. void Set() {
  36. std::lock_guard<std::mutex> lk(mutex);
  37. if (!is_set) {
  38. is_set = true;
  39. condvar.notify_one();
  40. }
  41. }
  42. void Wait() {
  43. std::unique_lock<std::mutex> lk(mutex);
  44. condvar.wait(lk, [&] { return is_set; });
  45. is_set = false;
  46. }
  47. void Reset() {
  48. std::unique_lock<std::mutex> lk(mutex);
  49. // no other action required, since wait loops on the predicate and any lingering signal will
  50. // get cleared on the first iteration
  51. is_set = false;
  52. }
  53. private:
  54. bool is_set;
  55. std::condition_variable condvar;
  56. std::mutex mutex;
  57. };
  58. class Barrier {
  59. public:
  60. explicit Barrier(size_t count_) : count(count_), waiting(0), generation(0) {}
  61. /// Blocks until all "count" threads have called Sync()
  62. void Sync() {
  63. std::unique_lock<std::mutex> lk(mutex);
  64. const size_t current_generation = generation;
  65. if (++waiting == count) {
  66. generation++;
  67. waiting = 0;
  68. condvar.notify_all();
  69. } else {
  70. condvar.wait(lk,
  71. [this, current_generation] { return current_generation != generation; });
  72. }
  73. }
  74. private:
  75. std::condition_variable condvar;
  76. std::mutex mutex;
  77. const size_t count;
  78. size_t waiting;
  79. size_t generation; // Incremented once each time the barrier is used
  80. };
  81. void SleepCurrentThread(int ms);
  82. void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms
  83. // Use this function during a spin-wait to make the current thread
  84. // relax while another thread is working. This may be more efficient
  85. // than using events because event functions use kernel calls.
  86. inline void YieldCPU() {
  87. std::this_thread::yield();
  88. }
  89. void SetCurrentThreadName(const char* name);
  90. } // namespace Common