| Mutex::Mutex() |
| 6 | { |
| 7 | ::pthread_mutex_init(&m_mutex, nullptr); |
| 8 | } |
| 9 |
| 10 | Mutex::~Mutex() |
| 11 | { |
| 12 | ::pthread_mutex_destroy(&m_mutex); |
| 13 | } |
| 14 |
| 15 | void Mutex::Lock() |
| 16 | { |
| 17 | ::pthread_mutex_lock(&m_mutex); |
| 18 | } |
| 19 |
| 20 | void Mutex::Unlock() |
| 21 | { |
| 22 | ::pthread_mutex_unlock(&m_mutex); |
| 23 | } |
| 24 |
| 25 | pthread_mutex_t* Mutex::NativeHandle() |
| 26 | { |
| 27 | return &m_mutex; |
| 28 | } |
| 29 |
| 30 | Thread::Thread(std::function<void()> entry) |
| 31 | : m_entry(std::move(entry)) |
| 32 | { |
| 33 | } |
| 34 |
| 35 | Thread::~Thread() = default; |
| 36 |
| 37 | void Thread::Start() |
| 38 | { |
| 39 | if (::pthread_create(&m_thread, nullptr, Thread::ActualEntryPoint, this) == 0) |
| 40 | { |
| 41 | m_valid = true; |
| 42 | } |
| 43 | } |
| 44 |
| 45 | void Thread::Join() |
| 46 | { |
| 47 | if (m_valid) |
| 48 | ::pthread_join(m_thread, nullptr); |
| 49 | } |
| 50 |
| 51 | void Thread::SetName(const char* name) |
| 52 | { |
| 53 | m_name = name; |
| 54 | } |
| 55 |
| 56 | void Thread::SetMaxPriority() |
| 57 | { |
| 58 | #ifdef __APPLE__ |
| 59 | m_maxPriority = true; |
| 60 | #endif |
| 61 | } |
| 62 |
| 63 | void Thread::Sleep(double seconds) |
| 64 | { |
| 65 | ::usleep(static_cast<useconds_t>(seconds * 1000 * 1000.0)); |
| 66 | } |
| 67 |
| 68 | bool Thread::IsCurrent() |
| 69 | { |
| 70 | return ::pthread_equal(m_thread, pthread_self()) != 0; |
| 71 | } |
| 72 |
| 73 | void* Thread::ActualEntryPoint(void* arg) |
| 74 | { |
| 75 | Thread* self = reinterpret_cast<Thread*>(arg); |
| 76 | if (self->m_name) |
| 77 | { |
| 78 | #if !defined(__APPLE__) && !defined(__gnu_hurd__) |
| 79 | ::pthread_setname_np(self->m_thread, self->m_name); |
| 80 | #elif !defined(__gnu_hurd__) |
| 81 | pthread_setname_np(self->name); |
| 82 | if (self->m_maxPriority) |
| 83 | { |
| 84 | DarwinSpecific::SetCurrentThreadPriority(DarwinSpecific::THREAD_PRIO_USER_INTERACTIVE); |
| 85 | } |
| 86 | #endif |
| 87 | } |
| 88 | self->m_entry(); |
| 89 | return nullptr; |
| 90 | } |
| 91 |
| 92 | #ifdef __APPLE__ |
| 93 |
| 94 | Semaphore::Semaphore(unsigned int maxCount, unsigned int initValue) |
| 95 | { |
| 96 | m_sem = dispatch_semaphore_create(initValue); |
| 97 | } |
| 98 |
| 99 | Semaphore::~Semaphore() |
| 100 | { |
| 101 | #if !__has_feature(objc_arc) |
| 102 | dispatch_release(m_sem); |
| 103 | #endif |
| 104 | } |
| 105 |
| 106 | void Semaphore::Acquire() |
| 107 | { |
| 108 | dispatch_semaphore_wait(m_sem, DISPATCH_TIME_FOREVER); |
| 109 | } |
| 110 |
| 111 | void Semaphore::Release() |
| 112 | { |
| 113 | dispatch_semaphore_signal(m_sem); |
| 114 | } |
| 115 |
| 116 | void Semaphore::Acquire(int count) |
| 117 | { |
| 118 | for (int i = 0; i < count; i++) |
| 119 | Acquire(); |
| 120 | } |
| 121 |
| 122 | void Semaphore::Release(int count) |
| 123 | { |
| 124 | for (int i = 0; i < count; i++) |
| 125 | Release(); |
| 126 | } |
| 127 |
| 128 | #else |
| 129 |
| 130 | Semaphore::Semaphore(unsigned int maxCount, unsigned int initValue) |
| 131 | { |
| 132 | ::sem_init(&m_sem, 0, initValue); |
| 133 | } |
| 134 |
| 135 | Semaphore::~Semaphore() |
| 136 | { |
| 137 | ::sem_destroy(&m_sem); |
| 138 | } |
| 139 |
| 140 | void Semaphore::Acquire() |
| 141 | { |
| 142 | ::sem_wait(&m_sem); |
| 143 | } |
| 144 |
| 145 | void Semaphore::Release() |
| 146 | { |
| 147 | ::sem_post(&m_sem); |
| 148 | } |
| 149 |
| 150 | void Semaphore::Acquire(int count) |
| 151 | { |
| 152 | for (int i = 0; i < count; ++i) |
| 153 | Acquire(); |
| 154 | } |
| 155 |
| 156 | void Semaphore::Release(int count) |
| 157 | { |
| 158 | for (int i = 0; i < count; ++i) |
| 159 | Release(); |
| 160 | } |
| 161 |
| 162 | MutexGuard::MutexGuard(Mutex& mutex) |
| 163 | : m_mutex(mutex) |
| 164 | { |
| 165 | mutex.Lock(); |
| 166 | } |
| 167 |
| 168 | MutexGuard::~MutexGuard() |
| 169 | { |
| 170 | m_mutex.Unlock(); |
| 171 | } |
| 172 |
| 173 | #endif |
| 174 |