How often is ‘cmpxchg’ used? $ cat vmlinux.asm | grep cmpxchg c01046de: c0105591: c01055d9: c010b895: c010b949: c0129a9f: c0129acf: c012d377: c012d41a: c012d968: c012e568: c012e57a: c012e58a: c012e83f: c012e931: c012ea94: c012ecf4: c012f08e: c012f163: c013cb60: c0148b3c: c0150d0f: c0150d87: c0199c5e: c024b06f: c024b2fe: c024b321: c024b34b: c024b960: f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 f0 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 15 15 15 11 0b 0b 0b 0e 0e 16 2e 2e 2e 13 0a 11 13 4b 11 0e 29 3b 31 0b 0b 51 51 4b 53 3c 99 30 3c 99 30 3c 99 30 18 18 18 18 18 lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock lock cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg cmpxchg %edx,0xc030993c %edx,0xc030993c %edx,0xc030993c %edx,(%ecx) %ecx,(%ebx) %ecx,(%ebx) %ecx,(%ebx) %ecx,(%esi) %ecx,(%esi) %edx,(%esi) %ebp,(%esi) %ebp,(%esi) %ebp,(%esi) %edx,(%ebx) %ecx,(%edx) %edx,(%ecx) %edx,(%ebx) %ecx,0x18(%ebx) %edx,(%ecx) %ecx,(%esi) %ebp,(%ecx) %edi,(%ebx) %esi,(%ecx) %ecx,(%ebx) %ecx,(%ebx) %edx,0x18(%ecx) %edx,0x18(%ecx) %ecx,0x18(%ebx) %edx,0x18(%ebx) Here’s the occurrence that we studied in the ‘rtc_cmos_read()’ kernel-function… …plus 28 other times!
View full slide show




Single Lock thread A thread B thread C Lock.acquire() Read data Lock.release() Lock.acquire() Modify data Lock.release() Lock.acquire() Read data Lock.release() thread D thread E thread F Lock.acquire() Read data Lock.release() Lock.acquire() Read data Lock.release() Lock.acquire() Modify data Lock.release()  Drawbacks of this solution? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 5
View full slide show




Readers/Writers: Example  thread A thread B thread C Lock.acquire() Read data Lock.release() Lock.acquire() Modify data Lock.release() Lock.acquire() Read data Lock.release() thread D thread E thread F Lock.acquire() Read data Lock.release() Lock.acquire() Read data Lock.release() Lock.acquire() Modify data Lock.release() Maximizes concurrency  Great! But how do we implement this? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 7
View full slide show




Time t1 t2 t3 t4 K req S lock a S lock a . . Time t1 t2 t3 t4 K req S lock a S lock a . . Time t1 t2 t3 t4 K req X lock a X lock a . . L . . req S lock a S lock a L . . req X lock a wait L . req X lock a . wait Chapters 21-22 46
View full slide show




Lock Conversions  Two-phase locking with lock conversions: – First Phase:  can acquire a lock-S on item  can acquire a lock-X on item  can convert a lock-S to a lock-X (upgrade) – Second Phase:  can release a lock-S  can release a lock-X  can convert a lock-X to a lock-S (downgrade)  This protocol assures serializability. But still relies on the programmer to insert the various locking instructions. Database System Concepts 3rd Edition 16.10 ©Silberschatz, Korth and Sudarshan
View full slide show




Using Lock Variable acquaire_lock(A)      acquaire_lock(B)  set lock=0   set lock=1 critical_section(B) release_lock(B)  set lock=0 Lock variable    set lock=1 critical_section(A) release_lock(A)   A software solution A single, shared variable (lock) Assume two processes PA and PB   Program in PA tests the variable to see if lock=0  if 0, PA enters the critical region and sets lock=1  if lock=1, it knows that the critical region is occupied by program in P B What is the problem? Spring 2016, arz CS555A – Real-Time Embedded Systems Stevens Institute of Technology 39
View full slide show




Possible Deadlock class B implements Runnable; { private Lock one, two; public(Lock one,Lock two) { this.one = one; this.two = two; } public void run() { try { two.lock(); doSomething(); one.lock(); doSomethingElse(); } finally { two.unlock(); one.unlock(); } } } class A implements Runnable; { private Lock one, two; public(Lock one,Lock two) { this.one = one; this.two = two; } public void run() { try { one.lock(); doSomething(); two.lock(); doSomethingElse(); } finally { two.unlock(); one.unlock(); } } } Deadlock possible depending on timing, but sometimes will not occur
View full slide show




 Rules for granting a lock:   1. If no transactions have any kind of lock on item X then any requested Read or Write lock is granted 2. Suppose transaction A has a read lock (S) on item X  - A request from some distinct transaction B for a read lock will be granted  - A request from some distinct transaction B for a write lock will be denied.  3. Suppose a transaction B has a write lock (X) on item X  A request from some distinct transaction B for a read lock will be denied  A request from some distinct transaction B for a write lock will be denied Chapters 21-22 44
View full slide show




Using TestAndSet   Shared Boolean variable lock, initialized to FALSE=0. Solution below uses two subroutines   enter_region is called to enter critical section (busy waiting) leave_region is called to leave critical section enter_region(); critical_code(); leave_region(); noncritical_code(); The JNE (JUMP if NOT EQUAL) defines the loop. Busy waiting: keep looping until lock is available. enter_region: TSL REGISTER,LOCK CMP REGISTER,#0 JNE enter_region RET leave_region: STORE LOCK,#0 RET Spring 2016, arz // copy lock to register and set lock to 1 // was lock zero? // if it was non zero, lock was set, so loop // return to caller; critical region entered // store a 0 in lock // return to caller CS555A – Real-Time Embedded Systems Stevens Institute of Technology 50
View full slide show




Using TestAndSet   Shared Boolean variable lock, initialized to FALSE=0. Solution below uses two subroutines   enter_region is called to enter critical section (busy waiting) leave_region is called to leave critical section enter_region(); critical_code(); leave_region(); noncritical_code(); The JNE (JUMP if NOT EQUAL) defines the loop. Busy waiting: keep looping until lock is available. enter_region: TSL REGISTER,LOCK CMP REGISTER,#0 JNE enter_region RET leave_region: STORE LOCK,#0 RET Spring 2016, arz // copy lock to register and set lock to 1 // was lock zero? // if it was non zero, lock was set, so loop // return to caller; critical region entered // store a 0 in lock // return to caller CS555A – Real-Time Embedded Systems Stevens Institute of Technology 6
View full slide show