From aa2fe8ccbfcb117efa207d10229eeeac5d0f97c7 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 6 Feb 2023 17:11:30 +0100 Subject: Adding upstream version 1.38.0. Signed-off-by: Daniel Baumann --- libnetdata/completion/completion.c | 30 ++++++++++++++++++++++++++++++ libnetdata/completion/completion.h | 5 +++++ 2 files changed, 35 insertions(+) (limited to 'libnetdata/completion') diff --git a/libnetdata/completion/completion.c b/libnetdata/completion/completion.c index b5ac86e4f..6257e0299 100644 --- a/libnetdata/completion/completion.c +++ b/libnetdata/completion/completion.c @@ -5,6 +5,7 @@ void completion_init(struct completion *p) { p->completed = 0; + p->completed_jobs = 0; fatal_assert(0 == uv_cond_init(&p->cond)); fatal_assert(0 == uv_mutex_init(&p->mutex)); } @@ -32,3 +33,32 @@ void completion_mark_complete(struct completion *p) uv_cond_broadcast(&p->cond); uv_mutex_unlock(&p->mutex); } + +unsigned completion_wait_for_a_job(struct completion *p, unsigned completed_jobs) +{ + uv_mutex_lock(&p->mutex); + while (0 == p->completed && p->completed_jobs <= completed_jobs) { + uv_cond_wait(&p->cond, &p->mutex); + } + completed_jobs = p->completed_jobs; + uv_mutex_unlock(&p->mutex); + + return completed_jobs; +} + +void completion_mark_complete_a_job(struct completion *p) +{ + uv_mutex_lock(&p->mutex); + p->completed_jobs++; + uv_cond_broadcast(&p->cond); + uv_mutex_unlock(&p->mutex); +} + +bool completion_is_done(struct completion *p) +{ + bool ret; + uv_mutex_lock(&p->mutex); + ret = p->completed; + uv_mutex_unlock(&p->mutex); + return ret; +} diff --git a/libnetdata/completion/completion.h b/libnetdata/completion/completion.h index 667360a42..723f73688 100644 --- a/libnetdata/completion/completion.h +++ b/libnetdata/completion/completion.h @@ -9,6 +9,7 @@ struct completion { uv_mutex_t mutex; uv_cond_t cond; volatile unsigned completed; + volatile unsigned completed_jobs; }; void completion_init(struct completion *p); @@ -19,4 +20,8 @@ void completion_wait_for(struct completion *p); void completion_mark_complete(struct completion *p); +unsigned completion_wait_for_a_job(struct completion *p, unsigned completed_jobs); +void completion_mark_complete_a_job(struct completion *p); +bool completion_is_done(struct completion *p); + #endif /* NETDATA_COMPLETION_H */ -- cgit v1.2.3