diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/thread.cpp | 35 | ||||
| -rw-r--r-- | src/common/thread.h | 20 |
2 files changed, 14 insertions, 41 deletions
diff --git a/src/common/thread.cpp b/src/common/thread.cpp index 9e207118f..5144c0d9f 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp | |||
| @@ -25,23 +25,6 @@ | |||
| 25 | 25 | ||
| 26 | namespace Common { | 26 | namespace Common { |
| 27 | 27 | ||
| 28 | int CurrentThreadId() { | ||
| 29 | #ifdef _MSC_VER | ||
| 30 | return GetCurrentThreadId(); | ||
| 31 | #elif defined __APPLE__ | ||
| 32 | return mach_thread_self(); | ||
| 33 | #else | ||
| 34 | return 0; | ||
| 35 | #endif | ||
| 36 | } | ||
| 37 | |||
| 38 | #ifdef _WIN32 | ||
| 39 | // Supporting functions | ||
| 40 | void SleepCurrentThread(int ms) { | ||
| 41 | Sleep(ms); | ||
| 42 | } | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #ifdef _MSC_VER | 28 | #ifdef _MSC_VER |
| 46 | 29 | ||
| 47 | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) { | 30 | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) { |
| @@ -62,7 +45,7 @@ void SwitchCurrentThread() { | |||
| 62 | 45 | ||
| 63 | // This is implemented much nicer in upcoming msvc++, see: | 46 | // This is implemented much nicer in upcoming msvc++, see: |
| 64 | // http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx | 47 | // http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx |
| 65 | void SetCurrentThreadName(const char* szThreadName) { | 48 | void SetCurrentThreadName(const char* name) { |
| 66 | static const DWORD MS_VC_EXCEPTION = 0x406D1388; | 49 | static const DWORD MS_VC_EXCEPTION = 0x406D1388; |
| 67 | 50 | ||
| 68 | #pragma pack(push, 8) | 51 | #pragma pack(push, 8) |
| @@ -75,7 +58,7 @@ void SetCurrentThreadName(const char* szThreadName) { | |||
| 75 | #pragma pack(pop) | 58 | #pragma pack(pop) |
| 76 | 59 | ||
| 77 | info.dwType = 0x1000; | 60 | info.dwType = 0x1000; |
| 78 | info.szName = szThreadName; | 61 | info.szName = name; |
| 79 | info.dwThreadID = -1; // dwThreadID; | 62 | info.dwThreadID = -1; // dwThreadID; |
| 80 | info.dwFlags = 0; | 63 | info.dwFlags = 0; |
| 81 | 64 | ||
| @@ -107,10 +90,6 @@ void SetCurrentThreadAffinity(u32 mask) { | |||
| 107 | } | 90 | } |
| 108 | 91 | ||
| 109 | #ifndef _WIN32 | 92 | #ifndef _WIN32 |
| 110 | void SleepCurrentThread(int ms) { | ||
| 111 | usleep(1000 * ms); | ||
| 112 | } | ||
| 113 | |||
| 114 | void SwitchCurrentThread() { | 93 | void SwitchCurrentThread() { |
| 115 | usleep(1000 * 1); | 94 | usleep(1000 * 1); |
| 116 | } | 95 | } |
| @@ -118,15 +97,15 @@ void SwitchCurrentThread() { | |||
| 118 | 97 | ||
| 119 | // MinGW with the POSIX threading model does not support pthread_setname_np | 98 | // MinGW with the POSIX threading model does not support pthread_setname_np |
| 120 | #if !defined(_WIN32) || defined(_MSC_VER) | 99 | #if !defined(_WIN32) || defined(_MSC_VER) |
| 121 | void SetCurrentThreadName(const char* szThreadName) { | 100 | void SetCurrentThreadName(const char* name) { |
| 122 | #ifdef __APPLE__ | 101 | #ifdef __APPLE__ |
| 123 | pthread_setname_np(szThreadName); | 102 | pthread_setname_np(name); |
| 124 | #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) | 103 | #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) |
| 125 | pthread_set_name_np(pthread_self(), szThreadName); | 104 | pthread_set_name_np(pthread_self(), name); |
| 126 | #elif defined(__NetBSD__) | 105 | #elif defined(__NetBSD__) |
| 127 | pthread_setname_np(pthread_self(), "%s", (void*)szThreadName); | 106 | pthread_setname_np(pthread_self(), "%s", (void*)name); |
| 128 | #else | 107 | #else |
| 129 | pthread_setname_np(pthread_self(), szThreadName); | 108 | pthread_setname_np(pthread_self(), name); |
| 130 | #endif | 109 | #endif |
| 131 | } | 110 | } |
| 132 | #endif | 111 | #endif |
diff --git a/src/common/thread.h b/src/common/thread.h index 6cbdb96a3..2cf74452d 100644 --- a/src/common/thread.h +++ b/src/common/thread.h | |||
| @@ -13,15 +13,8 @@ | |||
| 13 | 13 | ||
| 14 | namespace Common { | 14 | namespace Common { |
| 15 | 15 | ||
| 16 | int CurrentThreadId(); | ||
| 17 | |||
| 18 | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask); | ||
| 19 | void SetCurrentThreadAffinity(u32 mask); | ||
| 20 | |||
| 21 | class Event { | 16 | class Event { |
| 22 | public: | 17 | public: |
| 23 | Event() : is_set(false) {} | ||
| 24 | |||
| 25 | void Set() { | 18 | void Set() { |
| 26 | std::lock_guard<std::mutex> lk(mutex); | 19 | std::lock_guard<std::mutex> lk(mutex); |
| 27 | if (!is_set) { | 20 | if (!is_set) { |
| @@ -53,14 +46,14 @@ public: | |||
| 53 | } | 46 | } |
| 54 | 47 | ||
| 55 | private: | 48 | private: |
| 56 | bool is_set; | 49 | bool is_set = false; |
| 57 | std::condition_variable condvar; | 50 | std::condition_variable condvar; |
| 58 | std::mutex mutex; | 51 | std::mutex mutex; |
| 59 | }; | 52 | }; |
| 60 | 53 | ||
| 61 | class Barrier { | 54 | class Barrier { |
| 62 | public: | 55 | public: |
| 63 | explicit Barrier(std::size_t count_) : count(count_), waiting(0), generation(0) {} | 56 | explicit Barrier(std::size_t count_) : count(count_) {} |
| 64 | 57 | ||
| 65 | /// Blocks until all "count" threads have called Sync() | 58 | /// Blocks until all "count" threads have called Sync() |
| 66 | void Sync() { | 59 | void Sync() { |
| @@ -80,12 +73,13 @@ public: | |||
| 80 | private: | 73 | private: |
| 81 | std::condition_variable condvar; | 74 | std::condition_variable condvar; |
| 82 | std::mutex mutex; | 75 | std::mutex mutex; |
| 83 | const std::size_t count; | 76 | std::size_t count; |
| 84 | std::size_t waiting; | 77 | std::size_t waiting = 0; |
| 85 | std::size_t generation; // Incremented once each time the barrier is used | 78 | std::size_t generation = 0; // Incremented once each time the barrier is used |
| 86 | }; | 79 | }; |
| 87 | 80 | ||
| 88 | void SleepCurrentThread(int ms); | 81 | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask); |
| 82 | void SetCurrentThreadAffinity(u32 mask); | ||
| 89 | void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms | 83 | void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms |
| 90 | void SetCurrentThreadName(const char* name); | 84 | void SetCurrentThreadName(const char* name); |
| 91 | 85 | ||