summaryrefslogtreecommitdiffstats
path: root/lib/stream.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/stream.c
parentlib: add some macro helpers (diff)
downloadfrr-00dffa8cde7661e00245ebe1b1eea248b8dd6802.tar.xz
frr-00dffa8cde7661e00245ebe1b1eea248b8dd6802.zip
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/stream.c')
-rw-r--r--lib/stream.c17
1 files changed, 5 insertions, 12 deletions
diff --git a/lib/stream.c b/lib/stream.c
index dfd13ca18..2e1a0193a 100644
--- a/lib/stream.c
+++ b/lib/stream.c
@@ -28,6 +28,7 @@
#include "network.h"
#include "prefix.h"
#include "log.h"
+#include "frr_pthread.h"
#include "lib_errors.h"
DEFINE_MTYPE_STATIC(LIB, STREAM, "Stream")
@@ -1136,11 +1137,9 @@ void stream_fifo_push(struct stream_fifo *fifo, struct stream *s)
void stream_fifo_push_safe(struct stream_fifo *fifo, struct stream *s)
{
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
stream_fifo_push(fifo, s);
}
- pthread_mutex_unlock(&fifo->mtx);
}
/* Delete first stream from fifo. */
@@ -1170,11 +1169,9 @@ struct stream *stream_fifo_pop_safe(struct stream_fifo *fifo)
{
struct stream *ret;
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
ret = stream_fifo_pop(fifo);
}
- pthread_mutex_unlock(&fifo->mtx);
return ret;
}
@@ -1188,11 +1185,9 @@ struct stream *stream_fifo_head_safe(struct stream_fifo *fifo)
{
struct stream *ret;
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
ret = stream_fifo_head(fifo);
}
- pthread_mutex_unlock(&fifo->mtx);
return ret;
}
@@ -1212,11 +1207,9 @@ void stream_fifo_clean(struct stream_fifo *fifo)
void stream_fifo_clean_safe(struct stream_fifo *fifo)
{
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
stream_fifo_clean(fifo);
}
- pthread_mutex_unlock(&fifo->mtx);
}
size_t stream_fifo_count_safe(struct stream_fifo *fifo)