Synchronization Types of Synchronization Mutual Exclusion CS 740 • Locks November 14, 2003 Event Synchronization • Global or group-based (barriers) Based on TCM/C&S • Point-to-point
Topics • Locks • Barriers • Hardware primitives
–2– CS 740 F’03
Busy Waiting vs. Blocking A Simple Lock
lock: ld register, location Busy-waiting is preferable when: cmp register, #0 • scheduling overhead is larger than expected wait bnz lock time st location, #1 • processor resources are not needed for other ret tasks unlock: st location, #0 • schedule-based blocking is inappropriate (e.g., in OS kernel) ret
–3– CS 740 F’03 –4– CS 740 F’03 Need Atomic Primitive! Test&Set based lock
Test&Set lock: t&s register, location bnz lock Swap ret Fetch&Op unlock: st location, #0 • Fetch&Incr, Fetch&Decr ret Compare&Swap
–5– CS 740 F’03 –6– CS 740 F’03
T&S Lock Performance Test and Test and Set
Code: lock; delay(c); unlock; A: while (lock != free) Same total no. of lock calls as p increases; measure time per transfer if (test&set(lock) == free) { 20 Test&set,c = 0 Test&set, exponential backoff, c = 3.64 18 critical section; Test&set, exponential backoff, c = 0 Ideal 16 } 14 12 else goto A; s) µ 10 Time ( 8
6 (+) spinning happens in cache