From f2aba977b4a78a024549004978fcda4171b7d0e1 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Wed, 4 Dec 2024 11:17:46 +0000 Subject: [PATCH] fix: extract supplemental and shared_files initialisation --- src/threads/init.c | 2 ++ src/threads/thread.c | 6 +----- src/vm/page.c | 23 ++++++++++++++++++++++- src/vm/page.h | 11 +++-------- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/threads/init.c b/src/threads/init.c index 69ac6af..a292b91 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -31,6 +31,7 @@ #else #include "tests/threads/tests.h" #endif +#include "vm/page.h" #ifdef VM #include "vm/frame.h" #include "devices/swap.h" @@ -121,6 +122,7 @@ main (void) exception_init (); syscall_init (); #endif + shared_files_init (); /* Start thread scheduler and enable interrupts. */ thread_start (); diff --git a/src/threads/thread.c b/src/threads/thread.c index 8487ff8..ed37d2f 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -134,10 +134,6 @@ thread_start (void) t)) PANIC ("Failed to initialise child results table for main thread."); - /* Initialise the shared files table and lock. */ - if (!hash_init (&shared_files, shared_file_hash, shared_file_less, NULL)) - PANIC ("Failed to initialise shared pages table."); - lock_init (&shared_files_lock); /* Create the idle thread. */ @@ -272,7 +268,7 @@ thread_create (const char *name, int priority, if (!hash_init (&t->open_files, fd_hash, fd_less, NULL) || !hash_init (&t->child_results, process_result_hash, process_result_less, t) - || !hash_init (&t->pages, page_hash, page_less, NULL)) + || !init_pages (t)) { palloc_free_page (t); free (t->result); diff --git a/src/vm/page.c b/src/vm/page.c index 0470e37..cf5f1f7 100644 --- a/src/vm/page.c +++ b/src/vm/page.c @@ -9,17 +9,29 @@ #include "userprog/process.h" #include "vm/frame.h" +static unsigned page_hash (const struct hash_elem *e, void *aux UNUSED); +static bool page_less (const struct hash_elem *a_, const struct hash_elem *b_, + void *aux UNUSED); static struct shared_page_entry *shared_page_insert (struct file *file, void *upage, void *frame); static struct shared_file_entry *shared_file_get (struct file *file); +static struct shared_page_entry *shared_page_get (struct file *file, + void *upage); static unsigned shared_page_hash (const struct hash_elem *e, void *aux UNUSED); static bool shared_page_less (const struct hash_elem *a_, const struct hash_elem *b_, void *aux UNUSED); +/* Initialise a thread's supplemental pages table. */ +bool +init_pages (struct thread *t) +{ + return hash_init (&t->pages, page_hash, page_less, NULL); +} + /* Hashing function needed for the SPT table. Returns a hash for an entry, based on its upage. */ unsigned -page_hash (const struct hash_elem *e, UNUSED void *aux) +page_hash (const struct hash_elem *e, void *aux UNUSED) { struct page_entry *page = hash_entry (e, struct page_entry, elem); return hash_ptr(page->upage); @@ -167,6 +179,15 @@ page_cleanup (struct hash_elem *e, void *aux UNUSED) free (page); } +/* Initialise the shared files and table and lock. */ +bool +shared_files_init () +{ + lock_init (&shared_files_lock); + if (!hash_init (&shared_files, shared_file_hash, shared_file_less, NULL)) + PANIC ("Failed to initialise shared_files table."); +} + /* Hashing function needed for the shared_file table. Returns a hash for an entry based on its file pointer. */ unsigned diff --git a/src/vm/page.h b/src/vm/page.h index 9b7254e..c6fb36c 100644 --- a/src/vm/page.h +++ b/src/vm/page.h @@ -43,20 +43,15 @@ struct shared_page_entry { struct hash_elem elem; }; -unsigned page_hash (const struct hash_elem *e, void *aux); -bool page_less (const struct hash_elem *a_, const struct hash_elem *b_, - void *aux); +bool init_pages (struct thread *t); struct page_entry *page_insert (struct file *file, off_t ofs, void *upage, uint32_t read_bytes, uint32_t zero_bytes, bool writable, enum page_type type); struct page_entry *page_get (void *upage); bool page_load (struct page_entry *page); -void page_cleanup (struct hash_elem *e, void *aux); +void page_cleanup (struct hash_elem *e, void *aux UNUSED); -unsigned shared_file_hash (const struct hash_elem *e, void *aux); -bool shared_file_less (const struct hash_elem *a_, const struct hash_elem *b_, - void *aux); -struct shared_page_entry *shared_page_get (struct file *file, void *upage); +void shared_files_init (); void page_set_swap (struct thread *, void *, size_t); size_t page_get_swap (struct thread *, void *);