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




Towers of Hanoi • A solution to ToH can be expressed recursively • To move N disks from the original peg to the destination peg: – Move the topmost N-1 disks from the original peg to the extra peg – Move the largest disk from the original peg to the destination peg – Move the N-1 disks from the extra peg to the destination peg • The base case occurs when a peg contains only one disk (N-1 = 1) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase 17 - 37
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




Towers of Hanoi • To move a stack of N disks from the original peg to the destination peg: – Move the topmost N-1 disks from the original peg to the extra peg – Move the largest disk from the original peg to the destination peg – Move the N-1 disks from the extra peg to the destination peg • The base case occurs when a "stack" contains only one disk 1-29 © 2010 Pearson Addison-Wesley. All rights reserved. 1-29
View full slide show




Towers of Hanoi • To move a stack of N disks from the original peg to the destination peg: – Move the topmost N-1 disks from the original peg to the extra peg – Move the largest disk from the original peg to the destination peg – Move the N-1 disks from the extra peg to the destination peg • The base case occurs when a "stack" contains only one disk Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 10-29
View full slide show




The Towers of Hanoi How it works: n=1 Move disk from peg 1 to peg 3. Done. n=2 Move top disk from peg 1 to peg 2. Move remaining disk from peg 1 to peg 3. Move disk from peg 2 to peg 3. Done. Copyright © 2017, 2014 Pearson Education, Inc. 14-27
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




The Tower of Hanoi (continued) Solution: Let {Hn} denote the number of moves needed to solve the Tower of Hanoi Puzzle with n disks. Set up a recurrence relation for the sequence {Hn}. Begin with n disks on peg 1. We can transfer the top n −1 disks, following the rules of the puzzle, to peg 3 using Hn−1 moves. First, we use 1 move to transfer the largest disk to the second peg. Then we transfer the n −1 disks from peg 3 to peg 2 using Hn−1 additional moves. This can not be done in fewer steps. Hence, Hn = 2Hn−1 + 1. The initial condition is H1= 1 since a single disk can be transferred from peg 1 to peg 2 in one move.
View full slide show