summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-03-15 13:50:45 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-03-15 21:33:45 +0100
commitd2f11d48b29de49265f1a0d997548074f80fe431 (patch)
treeeef11475b33f20c5530338af71f2a0ac991f312a
parent32ae3483d635aa50a541ea215c7cb8c88ec4f9d8 (diff)
downloadpintos-d2f11d48b29de49265f1a0d997548074f80fe431.tar.gz
add rwlock implementation
-rw-r--r--src/threads/synch.c40
-rw-r--r--src/threads/synch.h17
2 files changed, 57 insertions, 0 deletions
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