summaryrefslogtreecommitdiff
path: root/notxv6/barrier.c
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2024-02-16 11:29:36 +0800
committerMole Shang <[email protected]>2024-02-16 11:31:23 +0800
commitedd523ffcb39c1c57944796fabfc71c70a10ce2e (patch)
treea37eaf56bbee9509f32a775793a61738b3d4bbac /notxv6/barrier.c
parenta98c56a811142e5ede3332a7a444cca45f628769 (diff)
downloadxv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.tar.gz
xv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.tar.bz2
xv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.zip
lab thread: finish
Diffstat (limited to 'notxv6/barrier.c')
-rw-r--r--notxv6/barrier.c16
1 files changed, 12 insertions, 4 deletions
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 *