summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2023-10-07 14:26:23 +0200
committerSander Vrijders <sander@ouroboros.rocks>2023-10-25 09:53:27 +0200
commit5706bf3efa8d8262982bbed15fb041e536f56cf2 (patch)
tree3cd9db132bb5756bdaa64982b63ad9b9dbe4aaad
parent180e92c5f13b99ed171e8efe11058eb943bc6506 (diff)
downloadouroboros-5706bf3efa8d8262982bbed15fb041e536f56cf2.tar.gz
ouroboros-5706bf3efa8d8262982bbed15fb041e536f56cf2.zip
lib: Wrap pthread_cond_timedwait for NULL abstime
We often have the pattern where we NULL-check abstime for pthread_cond_timedwait to call pthread_cond_wait if it is. Added a __timedwait function to wrap this. Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks> Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
-rw-r--r--include/ouroboros/pthread.h10
-rw-r--r--src/irmd/reg/flow.c11
-rw-r--r--src/irmd/reg/name.c18
-rw-r--r--src/irmd/reg/proc.c6
-rw-r--r--src/irmd/reg/proc.h2
-rw-r--r--src/lib/frct.c7
-rw-r--r--src/lib/shm_flow_set.c13
-rw-r--r--src/lib/shm_rbuff_ll.c14
-rw-r--r--src/lib/shm_rbuff_pthr.c14
-rw-r--r--src/lib/shm_rdrbuff.c8
10 files changed, 30 insertions, 73 deletions
diff --git a/include/ouroboros/pthread.h b/include/ouroboros/pthread.h
index 917c078b..05821dd1 100644
--- a/include/ouroboros/pthread.h
+++ b/include/ouroboros/pthread.h
@@ -25,6 +25,16 @@
#include <pthread.h>
+static int __attribute__((unused)) __timedwait(pthread_cond_t * cond,
+ pthread_mutex_t * mtx,
+ const struct timespec * abstime)
+{
+ if (abstime == NULL)
+ return pthread_cond_wait(cond, mtx);
+
+ return pthread_cond_timedwait(cond, mtx, abstime);
+}
+
/* various cleanup functions for pthread_cleanup_push */
static void __attribute__((unused)) __cleanup_rwlock_unlock(void * rwlock)
{
diff --git a/src/irmd/reg/flow.c b/src/irmd/reg/flow.c
index 30b9c504..898f2604 100644
--- a/src/irmd/reg/flow.c
+++ b/src/irmd/reg/flow.c
@@ -197,15 +197,8 @@ int reg_flow_wait_state(struct reg_flow * f,
while (!(f->state == state ||
f->state == FLOW_DESTROY ||
f->state == FLOW_DEALLOC_PENDING) &&
- ret != -ETIMEDOUT) {
- if (dl != NULL)
- ret = -pthread_cond_timedwait(&f->cond,
- &f->mtx,
- dl);
- else
- ret = -pthread_cond_wait(&f->cond,
- &f->mtx);
- }
+ ret != -ETIMEDOUT)
+ ret = -__timedwait(&f->cond, &f->mtx, dl);
if (f->state == FLOW_DESTROY ||
f->state == FLOW_DEALLOC_PENDING ||
diff --git a/src/irmd/reg/name.c b/src/irmd/reg/name.c
index 7e13e888..4d26c762 100644
--- a/src/irmd/reg/name.c
+++ b/src/irmd/reg/name.c
@@ -415,15 +415,17 @@ int reg_name_leave_state(struct reg_name * name,
enum name_state state,
struct timespec * timeout)
{
- struct timespec abstime;
- int ret = 0;
+ struct timespec ts;
+ struct timespec * abstime = NULL;
+ int ret;
assert(name);
assert(state != NAME_DESTROY);
if (timeout != NULL) {
- clock_gettime(PTHREAD_COND_CLOCK, &abstime);
- ts_add(&abstime, timeout, &abstime);
+ clock_gettime(PTHREAD_COND_CLOCK, &ts);
+ ts_add(&ts, timeout, &ts);
+ abstime = &ts;
}
pthread_mutex_lock(&name->mtx);
@@ -431,13 +433,7 @@ int reg_name_leave_state(struct reg_name * name,
pthread_cleanup_push(__cleanup_mutex_unlock, &name->mtx);
while (name->state == state && ret != -ETIMEDOUT)
- if (timeout)
- ret = -pthread_cond_timedwait(&name->cond,
- &name->mtx,
- &abstime);
- else
- ret = -pthread_cond_wait(&name->cond,
- &name->mtx);
+ ret = -__timedwait(&name->cond,&name->mtx, abstime);
if (name->state == NAME_DESTROY) {
ret = -1;
diff --git a/src/irmd/reg/proc.c b/src/irmd/reg/proc.c
index 1aae789d..8a016afe 100644
--- a/src/irmd/reg/proc.c
+++ b/src/irmd/reg/proc.c
@@ -211,11 +211,7 @@ int reg_proc_sleep(struct reg_proc * proc,
pthread_cleanup_push(cancel_reg_proc, proc);
while (proc->state == PROC_SLEEP && ret != -ETIMEDOUT)
- if (dl != NULL)
- ret = -pthread_cond_timedwait(&proc->cond,
- &proc->lock, dl);
- else
- ret = -pthread_cond_wait(&proc->cond, &proc->lock);
+ ret = -__timedwait(&proc->cond, &proc->lock, dl);
pthread_cleanup_pop(false);
diff --git a/src/irmd/reg/proc.h b/src/irmd/reg/proc.h
index fb11f34f..4c4ace24 100644
--- a/src/irmd/reg/proc.h
+++ b/src/irmd/reg/proc.h
@@ -28,7 +28,7 @@
#include "utils.h"
#include <unistd.h>
-#include <pthread.h>
+#include <ouroboros/pthread.h>
enum proc_state {
PROC_NULL = 0,
diff --git a/src/lib/frct.c b/src/lib/frct.c
index a3408481..7218837a 100644
--- a/src/lib/frct.c
+++ b/src/lib/frct.c
@@ -571,12 +571,7 @@ static int __frcti_window_wait(struct frcti * frcti,
pthread_cleanup_push(__cleanup_mutex_unlock, &frcti->mtx);
- if (abstime != NULL)
- ret = -pthread_cond_timedwait(&frcti->cond,
- &frcti->mtx,
- abstime);
- else
- ret = -pthread_cond_wait(&frcti->cond, &frcti->mtx);
+ ret = -__timedwait(&frcti->cond, &frcti->mtx, abstime);
pthread_cleanup_pop(false);
diff --git a/src/lib/shm_flow_set.c b/src/lib/shm_flow_set.c
index 9eb9bb65..75cf4b72 100644
--- a/src/lib/shm_flow_set.c
+++ b/src/lib/shm_flow_set.c
@@ -344,18 +344,11 @@ ssize_t shm_flow_set_wait(const struct shm_flow_set * set,
pthread_cleanup_push(__cleanup_mutex_unlock, set->lock);
while (set->heads[idx] == 0 && ret != -ETIMEDOUT) {
- if (abstime != NULL) {
- ret = -pthread_cond_timedwait(set->conds + idx,
- set->lock,
- abstime);
+ ret = -__timedwait(set->conds + idx, set->lock, abstime);
#ifdef HAVE_CANCEL_BUG
- if (ret == -ETIMEDOUT)
- pthread_testcancel();
+ if (ret == -ETIMEDOUT)
+ pthread_testcancel();
#endif
- } else {
- ret = -pthread_cond_wait(set->conds + idx,
- set->lock);
- }
#ifdef HAVE_ROBUST_MUTEX
if (ret == -EOWNERDEAD)
pthread_mutex_consistent(set->lock);
diff --git a/src/lib/shm_rbuff_ll.c b/src/lib/shm_rbuff_ll.c
index 4fab0df4..dc7e9f06 100644
--- a/src/lib/shm_rbuff_ll.c
+++ b/src/lib/shm_rbuff_ll.c
@@ -108,12 +108,7 @@ int shm_rbuff_write_b(struct shm_rbuff * rb,
pthread_cleanup_push(__cleanup_mutex_unlock, rb->lock);
while (!shm_rbuff_free(rb) && ret != -ETIMEDOUT) {
- if (abstime != NULL)
- ret = -pthread_cond_timedwait(rb->add,
- rb->lock,
- abstime);
- else
- ret = -pthread_cond_wait(rb->add, rb->lock);
+ ret = -__timedwait(rb->add, rb->lock, abstime);
#ifdef HAVE_ROBUST_MUTEX
if (ret == -EOWNERDEAD)
pthread_mutex_consistent(rb->lock);
@@ -187,12 +182,7 @@ ssize_t shm_rbuff_read_b(struct shm_rbuff * rb,
pthread_cleanup_push(__cleanup_mutex_unlock, rb->lock);
while (shm_rbuff_empty(rb) && (idx != -ETIMEDOUT)) {
- if (abstime != NULL)
- idx = -pthread_cond_timedwait(rb->add,
- rb->lock,
- abstime);
- else
- idx = -pthread_cond_wait(rb->add, rb->lock);
+ idx = -__timedwait(rb->add, rb->lock, abstime);
#ifdef HAVE_ROBUST_MUTEX
if (idx == -EOWNERDEAD)
pthread_mutex_consistent(rb->lock);
diff --git a/src/lib/shm_rbuff_pthr.c b/src/lib/shm_rbuff_pthr.c
index b1b9bd55..f630d344 100644
--- a/src/lib/shm_rbuff_pthr.c
+++ b/src/lib/shm_rbuff_pthr.c
@@ -114,12 +114,7 @@ int shm_rbuff_write_b(struct shm_rbuff * rb,
while (!shm_rbuff_free(rb)
&& ret != -ETIMEDOUT
&& !(*rb->acl & ACL_FLOWDOWN)) {
- if (abstime != NULL)
- ret = -pthread_cond_timedwait(rb->del,
- rb->lock,
- abstime);
- else
- ret = -pthread_cond_wait(rb->del, rb->lock);
+ ret = -__timedwait(rb->del, rb->lock, abstime);
#ifdef HAVE_ROBUST_MUTEX
if (ret == -EOWNERDEAD)
pthread_mutex_consistent(rb->lock);
@@ -204,12 +199,7 @@ ssize_t shm_rbuff_read_b(struct shm_rbuff * rb,
while (shm_rbuff_empty(rb) &&
idx != -ETIMEDOUT &&
check_rb_acl(rb) == -EAGAIN) {
- if (abstime != NULL)
- idx = -pthread_cond_timedwait(rb->add,
- rb->lock,
- abstime);
- else
- idx = -pthread_cond_wait(rb->add, rb->lock);
+ idx = -__timedwait(rb->add, rb->lock, abstime);
#ifdef HAVE_ROBUST_MUTEX
if (idx == -EOWNERDEAD)
pthread_mutex_consistent(rb->lock);
diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c
index 56ae484e..a8c346ae 100644
--- a/src/lib/shm_rdrbuff.c
+++ b/src/lib/shm_rdrbuff.c
@@ -402,13 +402,7 @@ ssize_t shm_rdrbuff_alloc_b(struct shm_rdrbuff * rdrb,
#else
while (!shm_rdrb_free(rdrb, 1) && ret != ETIMEDOUT) {
#endif
- if (abstime != NULL)
- ret = pthread_cond_timedwait(rdrb->healthy,
- rdrb->lock,
- abstime);
- else
- ret = pthread_cond_wait(rdrb->healthy, rdrb->lock);
-
+ ret = __timedwait(rdrb->healthy, rdrb->lock, abstime);
#ifdef SHM_RDRB_MULTI_BLOCK
if (blocks + *rdrb->head > (SHM_BUFFER_SIZE))
padblocks = (SHM_BUFFER_SIZE) - *rdrb->head;