From d2f11d48b29de49265f1a0d997548074f80fe431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 15 Mar 2021 13:50:45 +0100 Subject: add rwlock implementation --- src/threads/synch.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/threads/synch.h | 17 +++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/threads/synch.c b/src/threads/synch.c index 317c68a..e714ce0 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -336,3 +336,43 @@ cond_broadcast (struct condition *cond, struct lock *lock) while (!list_empty (&cond->waiters)) cond_signal (cond, lock); } + +void +rwlock_init (struct rwlock *rwlock) +{ + sema_init (&rwlock->resource, 1); + lock_init (&rwlock->rmutex); + rwlock->readcount = 0; +} + +void +rwlock_write_p (struct rwlock *rwlock) +{ + sema_down (&rwlock->resource); +} + +void +rwlock_write_v (struct rwlock *rwlock) +{ + sema_up (&rwlock->resource); +} + +void +rwlock_read_p (struct rwlock *rwlock) +{ + lock_acquire (&rwlock->rmutex); + rwlock->readcount ++; + if (rwlock->readcount == 1) + sema_down (&rwlock->resource); + lock_release (&rwlock->rmutex); +} + +void +rwlock_read_v (struct rwlock *rwlock) +{ + lock_acquire (&rwlock->rmutex); + rwlock->readcount --; + if (rwlock->readcount == 0) + sema_up (&rwlock->resource); + lock_release (&rwlock->rmutex); +} diff --git a/src/threads/synch.h b/src/threads/synch.h index a19e88b..a76eecc 100644 --- a/src/threads/synch.h +++ b/src/threads/synch.h @@ -41,6 +41,23 @@ void cond_wait (struct condition *, struct lock *); void cond_signal (struct condition *, struct lock *); void cond_broadcast (struct condition *, struct lock *); +/* Readers-writers lock. + + Implementation of "First readers-writers problem" from + https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem. */ +struct rwlock + { + struct semaphore resource; + struct lock rmutex; + unsigned readcount; + }; + +void rwlock_init (struct rwlock *); +void rwlock_write_p (struct rwlock *); +void rwlock_write_v (struct rwlock *); +void rwlock_read_p (struct rwlock *); +void rwlock_read_v (struct rwlock *); + /* Optimization barrier. The compiler will not reorder operations across an -- cgit v1.2.1