feat(thread): add SSemaphore::Signal

This commit is contained in:
fallenoak 2021-08-09 01:03:37 -05:00
parent 6437796094
commit 34e01acca5
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
2 changed files with 29 additions and 0 deletions

View file

@ -13,3 +13,31 @@ SSemaphore::SSemaphore(uint32_t initialCount, uint32_t maximumCount)
pthread_cond_init(&this->m_cond, 0);
#endif
}
int32_t SSemaphore::Signal(uint32_t count) {
#if defined(WHOA_SYSTEM_WIN)
return ReleaseSemaphore(this->m_opaqueData, count, nullptr);
#endif
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
pthread_mutex_lock(&this->m_mutex);
if (this->m_value1 + count > this->m_value2) {
pthread_mutex_unlock(&this->m_mutex);
return 0;
}
this->m_value1 += count;
if (count <= 1) {
pthread_cond_signal(&this->m_cond);
} else {
pthread_cond_broadcast(&this->m_cond);
}
pthread_mutex_unlock(&this->m_mutex);
return 1;
#endif
}