cs162_fa08_mt1-6 - Note that our &addr notation...

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

View Full Document Right Arrow Icon
CS 162 Fall 2008 Midterm Exam I October 15, 2008 Page 8/21 Problem 3: Atomic Synchronization Primitives [25 pts] In class, we discussed a number of atomic hardware primitives that are available on modern architectures. In particular, we discussed “test and set” (TSET), SWAP, and “compare and swap” (CAS). They can be defined as follows (let “expr” be an expression, “&addr” be an address of a memory location, and “M[addr]” be the actual memory location at address addr): Test and Set (TSET) Atomic Swap (SWAP) Compare and Swap (CAS) TSET(&addr) { int result = M[addr]; M[addr] = 1; return (result); } SWAP(&addr, expr) { int result = M[addr]; M[addr] = expr; return (result); } CAS(&addr, expr1, expr2) { if (M[addr] == expr1) { M[addr] = expr2; return true; } else { return false; } } Both TSET and SWAP return values (from memory), whereas CAS returns either true or false.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Note that our &addr notation is similar to a reference in c++, and means that the &addr argument must be something that can be stored into (an lvalue). For instance, TSET could be used to implement a spin-lock acquire as follows: int lock = 0; // lock is free // Later: acquire lock while (TSET(lock)); CAS is general enough as an atomic operation that it can be used to implement both TSET and SWAP. For instance, consider the following implementation of TSET with CAS: TSET(&addr) { int temp; do { temp = M[addr]; } while (!CAS(addr,temp,1)); return temp; } Problem 3a[3pts]: Show how to implement a spinlock acquire with a single while loop using CAS instead of TSET. You must only fill in the arguments to CAS below: // Initialization int lock = 0; // Lock is free // acquire lock while ( !CAS( , , ) );...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 162 taught by Professor Kubiatowicz during the Spring '02 term at University of California, Berkeley.

Ask a homework question - tutors are online