class Lock Lock int locked 0 ThreadQueue q void Locklock intrDisable if locked locked 1 else qadd currentThread ID: 1015903
Download Presentation The PPT/PDF document "Uniprocessor Lock Implementation" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
1. Uniprocessor Lock Implementationclass Lock { Lock() {} int locked = 0; ThreadQueue q;};void Lock::lock() { intrDisable(); if (!locked) { locked = 1; } else { q.add(currentThread); blockThread(); } intrEnable();}void Lock::unlock() { intrDisable(); if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } intrEnable();}CS 111 Lecture Notes: Lock ImplementationSlide 1
2. Uniprocessor Lock Implementation??class Lock { Lock() {} int locked = 0; ThreadQueue q;};void Lock::lock() { intrDisable(); if (!locked) { locked = 1; intrEnable(); } else { q.add(currentThread); intrEnable(); blockThread(); }}void Lock::unlock() { intrDisable(); if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } intrEnable();}CS 111 Lecture Notes: Lock ImplementationSlide 2
3. Locks for Multi-Core, v1class Lock { Lock() {} std::atomic<int> locked(0);};void Lock::lock() { while (locked.exchange(1)) { /* Do nothing */ }}void Lock::unlock() { locked = 0;}CS 111 Lecture Notes: Lock ImplementationSlide 3
4. Locks for Multi-Core, v2class Lock { Lock() {} std::atomic<int> locked(0); ThreadQueue q;};void Lock::lock() { if (locked.exchange(1)) { q.add(currentThread); blockThread(); }}void Lock::unlock() { if (q.empty() { locked = 0; } else { unblockThread(q.remove()); }}CS 111 Lecture Notes: Lock ImplementationSlide 4
5. Locks for Multi-Core, v3class Lock { Lock() {} int locked = 0; ThreadQueue q; std::atomic<int> spinlock;};void Lock::lock() { while (spinlock.exchange(1)) { /* Do nothing */ } if (!locked) { locked = 1; spinlock = 0; } else { q.add(currentThread); spinlock = 0; blockThread(); }}void Lock::unlock() { while (spinlock.exchange(1)) { /* Do nothing */ } if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } spinlock = 0;}CS 111 Lecture Notes: Lock ImplementationSlide 5
6. Locks for Multi-Core, v4class Lock { Lock() {} int locked = 0; ThreadQueue q; std::atomic<int> spinlock;};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(); }}void Lock::unlock() { while (spinlock.exchange(1)) { /* Do nothing */ } if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } spinlock = 0;}CS 111 Lecture Notes: Lock ImplementationSlide 6
7. Locks for Multi-Core, v5class Lock { Lock() {} int locked = 0; ThreadQueue q; std::atomic<int> spinlock;};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();}void Lock::unlock() { intrDisable(); while (spinlock.exchange(1)) { /* Do nothing */ } if (q.empty() { locked = 0; } else { unblockThread(q.remove()); } spinlock = 0; intrEnable();}CS 111 Lecture Notes: Lock ImplementationSlide 7