mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03: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
Notes:
sideshowbarker
2024-07-18 20:19:39 +09:00
Author: https://github.com/gunnarbeutner Commit: https://github.com/SerenityOS/serenity/commit/32794e00a16 Pull-request: https://github.com/SerenityOS/serenity/pull/6332 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/tomuta
@ -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…
Reference in New Issue
Block a user