Download presentation
Presentation is loading. Please wait.
1
Userspace Synchronization
Chris Gill, Brian Kocoloski CSE 422S - Operating Systems Organization Washington University in St. Louis St. Louis, MO 63143
2
CSE 422S – Operating Systems Organization
Atomic Operations Today, we will be exploring atomic hardware operations and synchronization in more detail Important atomic functions: __atomic_compare_exchange() – set a variable to a new value if its old value is something we expect it to be __atomic_add_fetch() – add to a variable and return its new value __atomic_sub_fetch() – subtract from a variable and return its new value CSE 422S – Operating Systems Organization
3
CSE 422S – Operating Systems Organization
Compare/Exchange Syntax: cmp_exchg(ptr, expected, desired) Parameters ptr : pointer to an integer valued expected: value that ptr should have now desired: value that we want to set ptr to Semantics: (of course, this is executed atomically) if (*ptr == expected) { *ptr = desired; return true; } else { return false; } CSE 422S – Operating Systems Organization
4
Spinlock via Compare/Exchange
Syntax: cmp_exchg(ptr, expected, desired) Lock: Repeatedly invoke cmp_exchg(ptr, UNLOCKED, LOCKED) If it fails, that means the value of ptr is not unlocked, so need to try again If it succeeds, value is set to LOCKED to prevent concurrent access Unlock: Invoke cmp_exchg(ptr, LOCKED, UNLOCKED) Has to succeed – if not, the lock was never acquired CSE 422S – Operating Systems Organization
5
CSE 422S – Operating Systems Organization
Spinlocks vs Futexes Spinlocks via cmp/xchg function correctly, but waste CPU cycles via spinning In cases of longer critical sections, it is desirable to be able to put processes/threads to sleep Similar to kernel-level mutexes/semaphores Fast userspace mutexes (futexes) are a new feature to improve lock performance CSE 422S – Operating Systems Organization
6
CSE 422S – Operating Systems Organization
Futexes Two components: (1) an atomic integer variable in userspace (2) a system call to sleep/wake up contending processes Can be used to synchronize multiple processes or threads In multiprocess settings, processes must explicitly share memory via something like mmap() In multithread settings, threads implicitly share memory In their simplest form, futexes offer primitive operations on which other (sleep) lock mechanisms can be built Semaphores, condition variables, readers/writer locks, etc. CSE 422S – Operating Systems Organization
7
Futex Example <On whiteboard>
CSE 422S – Operating Systems Organization
8
Futex Example <On whiteboard> Takeaways:
Common case code path – (no lock contention) – is optimized by not requiring any system calls Uncommon case - (lock contention) – does not waste CPU cycles by spinning, but rather sleeps/wakes up via system calls CSE 422S – Operating Systems Organization
9
CSE 422S – Operating Systems Organization
Today’s Studio Implement userspace spinlocks and mutexes Some Programming Considerations Volatile variables are often important Inform compiler (and someone reading the code) that a variable’s value may change unexpectedly) Read type declarations right-to-left in C and C++ E.g., volatile int * is a pointer to an int that’s volatile (the int value, not the pointer, may change) OpenMP is for parallel programming Pragmas built into gcc, to support concurrent execution of functions, barrier synchronization before and after parallel-for loops, etc. CSE 422S – Operating Systems Organization
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.