diff --git a/src/sf_func.h b/src/sf_func.h index 6a8cfe2..7844318 100644 --- a/src/sf_func.h +++ b/src/sf_func.h @@ -117,23 +117,32 @@ static inline void sf_synchronize_counter_wait(SFSynchronizeContext *sctx) PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); } +#define sf_synchronize_finished_notify_no_lock(sctx, err_no) \ + (sctx)->finished = true; \ + (sctx)->result = err_no; \ + pthread_cond_signal(&(sctx)->lcp.cond) + static inline void sf_synchronize_finished_notify( - SFSynchronizeContext *sctx) + SFSynchronizeContext *sctx, const int result) { PTHREAD_MUTEX_LOCK(&sctx->lcp.lock); - sctx->finished = true; - pthread_cond_signal(&sctx->lcp.cond); + sf_synchronize_finished_notify_no_lock(sctx, result); PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); } -static inline void sf_synchronize_finished_wait(SFSynchronizeContext *sctx) +static inline int sf_synchronize_finished_wait(SFSynchronizeContext *sctx) { + int result; + PTHREAD_MUTEX_LOCK(&sctx->lcp.lock); while (!sctx->finished && SF_G_CONTINUE_FLAG) { pthread_cond_wait(&sctx->lcp.cond, &sctx->lcp.lock); } + result = sctx->result; sctx->finished = false; //for next notify PTHREAD_MUTEX_UNLOCK(&sctx->lcp.lock); + + return result; } #ifdef __cplusplus diff --git a/src/sf_types.h b/src/sf_types.h index 7805da6..66d7baf 100644 --- a/src/sf_types.h +++ b/src/sf_types.h @@ -237,9 +237,9 @@ typedef struct sf_cluster_config { typedef struct sf_synchronize_context { pthread_lock_cond_pair_t lcp; + int result; union { bool finished; - int result; int waiting_count; }; } SFSynchronizeContext;