Waitrwmx i 1 rwmxrelease implementing implementing

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: iving readers? Reader/Writer Reader/Writer Lock: Second Try SharedLock::AcquireWrite() { rwMx.Acquire(); while (i != 0) wCv.Wait(&rwMx); i = -1; rwMx.Release(); } SharedLock::AcquireRead() { rwMx.Acquire(); while (i < 0) rCv.Wait(&rwMx); i += 1; rwMx.Release(); } Implementing Implementing Spinlocks: First Cut class Lock { int held = 0; } void Lock::Acquire() { while (held); held = 1; } void Lock::Release() { held = 0; } “busy-wait” for lock holder to release Spinlocks: Spinlocks: What Went Wrong void Lock::Acquire() { while (held); held = 1; } void Lock::Release() { held = 0; } /* test */ /* set */ Implementing Implementing Locks: Another Try class Lock { } void Lock::Acquire() { disable interrupts; while (held); held = 1; enable interrupts } void Lock::Release() { … } /* test */ /* set */ Implementing Implementing Locks: Another Try (cont’d) class Lock { } void Lock::Acquire() { disable interrupts; while (held) { enable interrupts; disable interrupts; } held = 1; /* set */ enable interrupts; } void Lock::Release() { … } /* test */ The The Need for an Atomic “Toehold” To implement safe mutual exclusion, we need support for some sort of of “magic toehold” for synchronization. • The lock primitives themselves have critical sections to test and/or set the lock flags. • These primitives must somehow be made atomic. uninterruptible a sequence of instructions that executes “all or nothing” Synchronization Synchronization Hardware while (held); /* test */ held = 1; /* set */ Test and modify the content of a word atomically. boolean Test-and-Set (boolean &lock) { boolean TS; TS = lock; lock = true; return TS; } Mutual Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process Pi while (Test-and-Set (lock)); //acquire critical section lock = false; remainder section //release Summarize Summarize Techniques for atomicity disable interrupts (software solution) test-set-lock Next Next Lecture Memory Management Read Chap. 3 MOS Project #1 due on Wednesday...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern