summaryrefslogtreecommitdiff
path: root/lib/frr_pthread.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2019-06-21 10:58:02 +0200
committerDavid Lamparter <equinox@diac24.net>2019-09-03 17:15:17 +0200
commit00dffa8cde7661e00245ebe1b1eea248b8dd6802 (patch)
treea1e698d6b613b63407b3ad786565d09c7e7b7382 /lib/frr_pthread.c
parent48373d46f1e82fb0413831fa85304cea5c1db766 (diff)
lib: add frr_with_mutex() block-wrapper
frr_with_mutex(...) { ... } locks and automatically unlocks the listed mutex(es) when the block is exited. This adds a bit of safety against forgetting the unlock in error paths & co. and makes the code a slight bit more readable. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/frr_pthread.c')
-rw-r--r--lib/frr_pthread.c24
1 files changed, 6 insertions, 18 deletions
diff --git a/lib/frr_pthread.c b/lib/frr_pthread.c
index bdb6c2a397..21dfc9256f 100644
--- a/lib/frr_pthread.c
+++ b/lib/frr_pthread.c
@@ -49,21 +49,17 @@ static struct list *frr_pthread_list;
void frr_pthread_init(void)
{
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
frr_pthread_list = list_new();
frr_pthread_list->del = (void (*)(void *))&frr_pthread_destroy;
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
}
void frr_pthread_finish(void)
{
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
list_delete(&frr_pthread_list);
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
}
struct frr_pthread *frr_pthread_new(struct frr_pthread_attr *attr,
@@ -94,11 +90,9 @@ struct frr_pthread *frr_pthread_new(struct frr_pthread_attr *attr,
pthread_mutex_init(fpt->running_cond_mtx, NULL);
pthread_cond_init(fpt->running_cond, NULL);
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
listnode_add(frr_pthread_list, fpt);
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
return fpt;
}
@@ -162,23 +156,19 @@ int frr_pthread_run(struct frr_pthread *fpt, const pthread_attr_t *attr)
void frr_pthread_wait_running(struct frr_pthread *fpt)
{
- pthread_mutex_lock(fpt->running_cond_mtx);
- {
+ frr_with_mutex(fpt->running_cond_mtx) {
while (!fpt->running)
pthread_cond_wait(fpt->running_cond,
fpt->running_cond_mtx);
}
- pthread_mutex_unlock(fpt->running_cond_mtx);
}
void frr_pthread_notify_running(struct frr_pthread *fpt)
{
- pthread_mutex_lock(fpt->running_cond_mtx);
- {
+ frr_with_mutex(fpt->running_cond_mtx) {
fpt->running = true;
pthread_cond_signal(fpt->running_cond);
}
- pthread_mutex_unlock(fpt->running_cond_mtx);
}
int frr_pthread_stop(struct frr_pthread *fpt, void **result)
@@ -190,14 +180,12 @@ int frr_pthread_stop(struct frr_pthread *fpt, void **result)
void frr_pthread_stop_all(void)
{
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
struct listnode *n;
struct frr_pthread *fpt;
for (ALL_LIST_ELEMENTS_RO(frr_pthread_list, n, fpt))
frr_pthread_stop(fpt, NULL);
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
}
/*