mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:17:36 +00:00
LibPthread: Improve error handling for the semaphore functions
This patch makes sure we're propagating errors to the caller.
This commit is contained in:
parent
98403eccb0
commit
32794e00a1
1 changed files with 44 additions and 10 deletions
|
@ -36,8 +36,18 @@ int sem_close(sem_t*)
|
|||
|
||||
int sem_destroy(sem_t* sem)
|
||||
{
|
||||
pthread_mutex_destroy(&sem->mtx);
|
||||
pthread_cond_destroy(&sem->cv);
|
||||
auto rc = pthread_mutex_destroy(&sem->mtx);
|
||||
if (rc != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = pthread_cond_destroy(&sem->cv);
|
||||
if (rc != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -58,11 +68,17 @@ int sem_init(sem_t* sem, int shared, unsigned int value)
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(&sem->mtx, nullptr) != 0)
|
||||
auto rc = pthread_mutex_init(&sem->mtx, nullptr);
|
||||
if (rc != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pthread_cond_init(&sem->cv, nullptr) != 0)
|
||||
rc = pthread_cond_init(&sem->cv, nullptr);
|
||||
if (rc != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
sem->value = value;
|
||||
|
||||
|
@ -85,17 +101,29 @@ int sem_post(sem_t* sem)
|
|||
|
||||
sem->value++;
|
||||
|
||||
pthread_cond_signal(&sem->cv);
|
||||
auto rc = pthread_cond_signal(&sem->cv);
|
||||
if (rc != 0) {
|
||||
pthread_mutex_unlock(&sem->mtx);
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&sem->mtx);
|
||||
rc = pthread_mutex_unlock(&sem->mtx);
|
||||
if (errno != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sem_trywait(sem_t* sem)
|
||||
{
|
||||
if (pthread_mutex_lock(&sem->mtx) != 0)
|
||||
auto rc = pthread_mutex_lock(&sem->mtx);
|
||||
if (rc != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sem->value == 0) {
|
||||
pthread_mutex_unlock(&sem->mtx);
|
||||
|
@ -110,17 +138,23 @@ int sem_trywait(sem_t* sem)
|
|||
|
||||
int sem_unlink(const char*)
|
||||
{
|
||||
return ENOSYS;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int sem_wait(sem_t* sem)
|
||||
{
|
||||
if (pthread_mutex_lock(&sem->mtx) != 0)
|
||||
auto rc = pthread_mutex_lock(&sem->mtx);
|
||||
if (errno != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (sem->value == 0) {
|
||||
if (pthread_cond_wait(&sem->cv, &sem->mtx) != 0) {
|
||||
rc = pthread_cond_wait(&sem->cv, &sem->mtx);
|
||||
if (rc != 0) {
|
||||
pthread_mutex_unlock(&sem->mtx);
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue