aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/server.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-08-26 06:51:36 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-08-26 07:15:45 +0200
commit03223398b38c550533fb8579b599fa84716d41c4 (patch)
tree923debb1480997f17f353bb40ed6f0557265bb17 /src/server.c
parentworker: allow graceful shutdowns (diff)
downloadcimple-03223398b38c550533fb8579b599fa84716d41c4.tar.gz
cimple-03223398b38c550533fb8579b599fa84716d41c4.zip
fix pthread error handling
pthread functions return positive error codes.
Diffstat (limited to '')
-rw-r--r--src/server.c60
1 files changed, 26 insertions, 34 deletions
diff --git a/src/server.c b/src/server.c
index e285a9e..509d144 100644
--- a/src/server.c
+++ b/src/server.c
@@ -65,16 +65,16 @@ static void *server_scheduler(void *_server)
int ret = 0;
ret = pthread_mutex_lock(&server->scheduler_mtx);
- if (ret < 0) {
- print_errno("pthread_mutex_lock");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_mutex_lock");
goto exit;
}
while (1) {
while (!server_has_runs_and_workers(server)) {
ret = pthread_cond_wait(&server->scheduler_cv, &server->scheduler_mtx);
- if (ret < 0) {
- print_errno("pthread_cond_wait");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_cond_wait");
goto unlock;
}
}
@@ -85,8 +85,7 @@ static void *server_scheduler(void *_server)
}
unlock:
- if (pthread_mutex_unlock(&server->scheduler_mtx))
- print_errno("pthread_mutex_unlock");
+ pthread_check(pthread_mutex_unlock(&server->scheduler_mtx), "pthread_mutex_unlock");
exit:
return NULL;
@@ -105,32 +104,30 @@ int server_create(struct server *server, const struct settings *settings)
ci_queue_create(&server->ci_queue);
ret = pthread_mutex_init(&server->scheduler_mtx, NULL);
- if (ret < 0) {
- print_errno("pthread_mutex_init");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_mutex_init");
goto destroy_tcp_server;
}
ret = pthread_cond_init(&server->scheduler_cv, NULL);
- if (ret < 0) {
- print_errno("pthread_cond_init");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_cond_init");
goto destroy_scheduler_mtx;
}
ret = pthread_create(&server->scheduler, NULL, server_scheduler, server);
- if (ret < 0) {
- print_errno("pthread_create");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_create");
goto destroy_scheduler_cv;
}
return ret;
destroy_scheduler_cv:
- if (pthread_cond_destroy(&server->scheduler_cv))
- print_errno("pthread_cond_destroy");
+ pthread_check(pthread_cond_destroy(&server->scheduler_cv), "pthread_cond_destroy");
destroy_scheduler_mtx:
- if (pthread_mutex_destroy(&server->scheduler_mtx))
- print_errno("pthread_mutex_destroy");
+ pthread_check(pthread_mutex_destroy(&server->scheduler_mtx), "pthread_mutex_destroy");
ci_queue_destroy(&server->ci_queue);
@@ -144,12 +141,9 @@ destroy_tcp_server:
void server_destroy(struct server *server)
{
- if (pthread_join(server->scheduler, NULL))
- print_errno("pthread_join");
- if (pthread_cond_destroy(&server->scheduler_cv))
- print_errno("pthread_cond_destroy");
- if (pthread_mutex_destroy(&server->scheduler_mtx))
- print_errno("pthread_mutex_destroy");
+ pthread_check(pthread_join(server->scheduler, NULL), "pthread_join");
+ pthread_check(pthread_cond_destroy(&server->scheduler_cv), "pthread_cond_destroy");
+ pthread_check(pthread_mutex_destroy(&server->scheduler_mtx), "pthread_mutex_destroy");
ci_queue_destroy(&server->ci_queue);
worker_queue_destroy(&server->worker_queue);
tcp_server_destroy(&server->tcp_server);
@@ -218,8 +212,8 @@ int server_new_worker(struct server *server, int fd)
print_log("Registering a new worker\n");
ret = pthread_mutex_lock(&server->scheduler_mtx);
- if (ret < 0) {
- print_errno("pthread_mutex_lock");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_mutex_lock");
return ret;
}
@@ -230,14 +224,13 @@ int server_new_worker(struct server *server, int fd)
worker_queue_push(&server->worker_queue, entry);
ret = pthread_cond_signal(&server->scheduler_cv);
- if (ret < 0) {
- print_errno("pthread_cond_signal");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_cond_signal");
goto unlock;
}
unlock:
- if (pthread_mutex_unlock(&server->scheduler_mtx))
- print_errno("pthread_mutex_unlock");
+ pthread_check(pthread_mutex_unlock(&server->scheduler_mtx), "pthread_mutex_unlock");
return ret;
}
@@ -250,8 +243,8 @@ int server_ci_run(struct server *server, const char *url, const char *rev)
print_log("Scheduling a new CI run for repository %s\n", url);
ret = pthread_mutex_lock(&server->scheduler_mtx);
- if (ret < 0) {
- print_errno("pthread_mutex_lock");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_mutex_lock");
return ret;
}
@@ -262,14 +255,13 @@ int server_ci_run(struct server *server, const char *url, const char *rev)
ci_queue_push(&server->ci_queue, entry);
ret = pthread_cond_signal(&server->scheduler_cv);
- if (ret < 0) {
- print_errno("pthread_cond_signal");
+ if (ret) {
+ pthread_print_errno(ret, "pthread_cond_signal");
goto unlock;
}
unlock:
- if (pthread_mutex_unlock(&server->scheduler_mtx))
- print_errno("pthread_mutex_unlock");
+ pthread_check(pthread_mutex_unlock(&server->scheduler_mtx), "pthread_mutex_unlock");
return ret;
}