Uniprocessor Lock Implementation

Download Presenatation
Uniprocessor Lock Implementation
Slide Note
Embed
Share

The content discusses the implementation of a uniprocessor lock through different classes and methods. It covers various lock implementations for multi-core processors as well. The slides illustrate the locking mechanisms, thread handling, and atomic operations involved in maintaining mutual exclusion. Different versions of lock classes with thread queues, atomic variables, and spinlocks are explored.

  • Lock Implementation
  • Uniprocessor
  • Multi-Core
  • Thread Handling
  • Atomic Operations

Uploaded on Feb 27, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Uniprocessor Lock Implementation class Lock { Lock() {} int locked = 0; ThreadQueue q; }; void Lock::unlock() { intrDisable(); if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } intrEnable(); } void Lock::lock() { intrDisable(); if (!locked) { locked = 1; } else { q.add(currentThread); blockThread(); } intrEnable(); } CS 111 Lecture Notes: Lock Implementation Slide 1

  2. Uniprocessor Lock Implementation?? class Lock { Lock() {} int locked = 0; ThreadQueue q; }; void Lock::unlock() { intrDisable(); if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } intrEnable(); } void Lock::lock() { intrDisable(); if (!locked) { locked = 1; intrEnable(); } else { q.add(currentThread); intrEnable(); blockThread(); } } CS 111 Lecture Notes: Lock Implementation Slide 2

  3. Locks for Multi-Core, v1 class Lock { Lock() {} std::atomic<int> locked(0); }; void Lock::unlock() { locked = 0; } void Lock::lock() { while (locked.exchange(1)) { /* Do nothing */ } } CS 111 Lecture Notes: Lock Implementation Slide 3

  4. Locks for Multi-Core, v2 class Lock { Lock() {} std::atomic<int> locked(0); ThreadQueue q; }; void Lock::unlock() { if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } } void Lock::lock() { if (locked.exchange(1)) { q.add(currentThread); blockThread(); } } CS 111 Lecture Notes: Lock Implementation Slide 4

  5. Locks for Multi-Core, v3 class Lock { Lock() {} int locked = 0; ThreadQueue q; std::atomic<int> spinlock; }; void Lock::unlock() { while (spinlock.exchange(1)) { /* Do nothing */ } if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } spinlock = 0; } void Lock::lock() { while (spinlock.exchange(1)) { /* Do nothing */ } if (!locked) { locked = 1; spinlock = 0; } else { q.add(currentThread); spinlock = 0; blockThread(); } } CS 111 Lecture Notes: Lock Implementation Slide 5

  6. Locks for Multi-Core, v4 class Lock { Lock() {} int locked = 0; ThreadQueue q; std::atomic<int> spinlock; }; void Lock::unlock() { while (spinlock.exchange(1)) { /* Do nothing */ } if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } spinlock = 0; } void Lock::lock() { while (spinlock.exchange(1)) { /* Do nothing */ } if (!locked) { locked = 1; spinlock = 0; } else { q.add(currentThread); currentThread->state = BLOCKED; spinlock = 0; reschedule(); } } CS 111 Lecture Notes: Lock Implementation Slide 6

  7. Locks for Multi-Core, v5 class Lock { Lock() {} int locked = 0; ThreadQueue q; std::atomic<int> spinlock; }; void Lock::unlock() { intrDisable(); while (spinlock.exchange(1)) { /* Do nothing */ } if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } spinlock = 0; intrEnable(); } void Lock::lock() { intrDisable(); while (spinlock.exchange(1)) { /* Do nothing */ } if (!locked) { locked = 1; spinlock = 0; } else { q.add(currentThread); currentThread->state = BLOCKED; spinlock = 0; reschedule(); } intrEnable(); } CS 111 Lecture Notes: Lock Implementation Slide 7

More Related Content