Skip to content

P21: Implementation of Hardware Spinlocks and Basic Concurrency#214

Merged
codenet merged 1 commit intocodenet:p21-spinlocksfrom
illuminati2285:p21
Mar 14, 2026
Merged

P21: Implementation of Hardware Spinlocks and Basic Concurrency#214
codenet merged 1 commit intocodenet:p21-spinlocksfrom
illuminati2285:p21

Conversation

@illuminati2285
Copy link
Copy Markdown

@illuminati2285 illuminati2285 commented Mar 12, 2026

Note: I have targeted the p20-exec branch for this PR because the p21 branch does not yet exist in the upstream repo. Once the p21 branch is created, the base branch of this PR can be changed to p21 for merging.

Overview

This PR implements the P21 requirements by introducing basic concurrency controls. I have added hardware spinlocks and protected all core OS data structures to match the xv6-public standard, preventing single-processor interrupt race conditions.

Key Implementations:

  • Core Lock Mechanism: Created spinlock.h and implemented acquire/release logic utilizing the existing pushcli and popcli interrupt controls.
  • Protecting Core OS Components: Carefully applied locks across the kernel, specifically avoiding deadlocks during disk I/O:
    • kalloc.c: Protected the physical memory allocator (kmem.lock).
    • proc.c: Protected the process table (ptable.lock) and implemented forkret to properly release the scheduler lock for newly allocated processes.
    • bio.c: Protected the block buffer cache (bcache.lock) across all return paths.
    • file.c: Protected the open file table (ftable.lock).
    • fs.c: Protected the inode cache (icache.lock), ensuring the lock is temporarily released before disk I/O in iput to prevent deadlocks during itrunc.
    • ide.c: Protected the disk request queue (idelock) while safely keeping the wait loop lock-free.
    • console.c, trap.c, log.c: Secured the console (cons.lock), timer ticks (tickslock), and log headers (log.lock via log_write).

(Note: fileread, filewrite, and consoleread are intentionally left lock-free as they rely on Sleep Locks, which are planned for P22).

@codenet codenet self-requested a review March 14, 2026 05:46
@codenet codenet changed the base branch from p20-exec to p21-spinlocks March 14, 2026 06:05
Copy link
Copy Markdown
Owner

@codenet codenet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks very clean. Thanks.

But looks like it is missing locks. Can you carefully check it again?

consoleintr(int (*getc)(void))
{
int c, doprocdump=0;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing locks

{
uint target;
int c;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing locks

@codenet
Copy link
Copy Markdown
Owner

codenet commented Mar 14, 2026

Actually, let me merge it so other students can fix missing locks etc.

@codenet codenet merged commit 500e859 into codenet:p21-spinlocks Mar 14, 2026
@illuminati2285
Copy link
Copy Markdown
Author

Done, I have merged p22-sleeplocks into my branch and manually resolved all the merge conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants