From 97b64c67fbb3dd1ca61f2545e3ed47f0dab34e50 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 8 Nov 2021 11:06:30 +0800 Subject: [PATCH] sf_synchronize_counter_xxx use mutex lock all --- src/sf_func.h | 27 +++++++++++++++++++++++---- src/sf_types.h | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/sf_func.h b/src/sf_func.h index 688c31b..0ec7188 100644 --- a/src/sf_func.h +++ b/src/sf_func.h @@ -76,18 +76,37 @@ static inline int sf_synchronize_ctx_init(SFSynchronizeContext *sctx) return init_pthread_lock_cond_pair(&sctx->lcp); } -static inline void sf_synchronize_counter_notify(SFSynchronizeContext *sctx, - const int count) +static inline void sf_synchronize_counter_add( + SFSynchronizeContext *sctx, const int count) { - if (__sync_sub_and_fetch(&sctx->waiting_count, count) == 0) { + PTHREAD_MUTEX_LOCK(&sctx->lcp.lock); + sctx->waiting_count += count; + PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); +} + +static inline void sf_synchronize_counter_sub( + SFSynchronizeContext *sctx, const int count) +{ + PTHREAD_MUTEX_LOCK(&sctx->lcp.lock); + sctx->waiting_count -= count; + PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); +} + +static inline void sf_synchronize_counter_notify( + SFSynchronizeContext *sctx, const int count) +{ + PTHREAD_MUTEX_LOCK(&sctx->lcp.lock); + sctx->waiting_count -= count; + if (sctx->waiting_count == 0) { pthread_cond_signal(&sctx->lcp.cond); } + PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); } static inline void sf_synchronize_counter_wait(SFSynchronizeContext *sctx) { PTHREAD_MUTEX_LOCK(&sctx->lcp.lock); - while (FC_ATOMIC_GET(sctx->waiting_count) != 0) { + while (sctx->waiting_count != 0) { pthread_cond_wait(&sctx->lcp.cond, &sctx->lcp.lock); } PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); diff --git a/src/sf_types.h b/src/sf_types.h index bbe199c..9b935c5 100644 --- a/src/sf_types.h +++ b/src/sf_types.h @@ -240,7 +240,7 @@ typedef struct sf_synchronize_context { union { bool finished; int result; - volatile int waiting_count; + int waiting_count; }; } SFSynchronizeContext;