From edd523ffcb39c1c57944796fabfc71c70a10ce2e Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Fri, 16 Feb 2024 11:29:36 +0800 Subject: lab thread: finish --- notxv6/barrier.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'notxv6/barrier.c') diff --git a/notxv6/barrier.c b/notxv6/barrier.c index 12793e8..b7737a6 100644 --- a/notxv6/barrier.c +++ b/notxv6/barrier.c @@ -25,12 +25,20 @@ barrier_init(void) static void barrier() { - // YOUR CODE HERE - // // Block until all threads have called barrier() and // then increment bstate.round. - // - + pthread_mutex_lock(&bstate.barrier_mutex); + bstate.nthread++; + if(bstate.nthread != nthread) { + pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex); + } else { + pthread_cond_broadcast(&bstate.barrier_cond); + // All threads have reached barrier. + // reset and increase round + bstate.nthread = 0; + bstate.round++; + } + pthread_mutex_unlock(&bstate.barrier_mutex); } static void * -- cgit v1.2.3