diff --git a/src/sf_func.h b/src/sf_func.h index 8f64738..a3536e3 100644 --- a/src/sf_func.h +++ b/src/sf_func.h @@ -19,6 +19,7 @@ #define _SF_FUNC_H #include "fastcommon/pthread_func.h" +#include "fastcommon/fc_atomic.h" #include "sf_types.h" #include "sf_global.h" @@ -71,9 +72,19 @@ static inline void sf_binlog_buffer_destroy(SFBinlogBuffer *buffer) static inline int sf_synchronize_ctx_init(SFSynchronizeContext *sctx) { + sctx->waiting_count = 0; return init_pthread_lock_cond_pair(&sctx->lcp); } +static inline void sf_synchronize_counter_wait(SFSynchronizeContext *sctx) +{ + PTHREAD_MUTEX_LOCK(&sctx->lcp.lock); + while (FC_ATOMIC_GET(sctx->waiting_count) != 0) { + pthread_cond_wait(&sctx->lcp.cond, &sctx->lcp.lock); + } + PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); +} + #ifdef __cplusplus } #endif diff --git a/src/sf_types.h b/src/sf_types.h index 9d1a489..6597cf4 100644 --- a/src/sf_types.h +++ b/src/sf_types.h @@ -240,6 +240,7 @@ typedef struct sf_synchronize_context { union { bool done; int result; + volatile int waiting_count; }; } SFSynchronizeContext;