From 3f0756ec5eaeb249b6b6cc3515c1bbbb739f9840 Mon Sep 17 00:00:00 2001 From: Nowherechan Date: Wed, 29 Mar 2023 10:31:24 +0800 Subject: [PATCH] lab barrier finished --- notxv6/barrier.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/notxv6/barrier.c b/notxv6/barrier.c index 12793e8..277c3ee 100644 --- a/notxv6/barrier.c +++ b/notxv6/barrier.c @@ -31,6 +31,17 @@ barrier() // then increment bstate.round. // + pthread_mutex_lock(&bstate.barrier_mutex); + bstate.nthread += 1; + if (nthread == bstate.nthread) { + bstate.nthread = 0; + bstate.round += 1; + pthread_mutex_unlock(&bstate.barrier_mutex); + pthread_cond_broadcast(&bstate.barrier_cond); + return; + } + pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex); + pthread_mutex_unlock(&bstate.barrier_mutex); } static void *