CSC/ECE 506 Spring 2012/9a mm: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 9: Line 9:


====Atomic Primitives====
====Atomic Primitives====
These instructions are used directly by compilers and operating systems but are also used in library functions in higher-level languages to implement locking.   
These instructions are used directly by compilers and operating systems but are also used in library functions in higher-level languages to implement locking.  These include operations such as atomic read-write, atomic swap, test-and-set, fetch-and-add, compare-and-swap, and Load-Link/Store-Conditional (LL/SC).  For example, a C function that implements compare-and-swap might look like this:
 
                                  ''int compare_and_swap (int* reg, int oldval, int newval)''
                                    ''{''
                                      ''int old_reg_val = *reg;''
                                      ''if (old_reg_val == oldval)''
                                        ''*reg = newval;''
                                      ''return old_reg_val;''
                                    ''}''
 
This function shows the logic for the swap but does not yet address correctness because it does not guarantee atomicity.


===Monitors===
===Monitors===
Line 16: Line 26:


==Biased Locks==
==Biased Locks==
==See Also==
==References==

Revision as of 17:30, 1 April 2012

Reducing Locking Overhead

Thread synchronization in a multiprocessor environment seeks to ensure memory consistency during concurrent execution. Several mechanisms exist to address synchronization and central to the solution is typically the provision of atomicity. When a thread performs a set of operations that are seen by other threads and the rest of the system as instantaneous, atomicity has been provided. The operations themselves of course are not physically instantaneous and rely upon locking other processes out of protected memory locations while critical accesses by a single thread occur. The overhead associated with providing thread synchronization can be significant, and the aim of this article is to discuss atomicity via locks and optimizations that improve locking performance.

Locks

Locks are a mechanism for providing concurrency control among concurrently executing processes. Locks are important when multiple processes share data, unless in a read only manner, and also when a set of operations executed by a process must be accomplished atomically. A multiprocessor environment that fails to provide concurrency control can suffer correctness problems as demonstrated in the figure below.

No concurrency control

Consider two processes that access and write a shared variable MyVar with three processor instructions: Read MyVar into a register, increment MyVar, and write the new value back to the memory location. The diagram shows two processes attempting to concurrently increment MyVar and the incorrectness can follow when the three operations are not executed atomically. Only the Thread A update is preserved in the execution sequence shown as it overwrites the results of Thread B.

Locks are ususally constructed with hardware support although methods such as Peterson's or Dekker's algorithms have been developed for environments where hardware primitives are not available.

Atomic Primitives

These instructions are used directly by compilers and operating systems but are also used in library functions in higher-level languages to implement locking. These include operations such as atomic read-write, atomic swap, test-and-set, fetch-and-add, compare-and-swap, and Load-Link/Store-Conditional (LL/SC). For example, a C function that implements compare-and-swap might look like this:

                                 int compare_and_swap (int* reg, int oldval, int newval) 
                                   {
                                     int old_reg_val = *reg;
                                     if (old_reg_val == oldval) 
                                       *reg = newval;
                                     return old_reg_val;
                                   }

This function shows the logic for the swap but does not yet address correctness because it does not guarantee atomicity.

Monitors

Thin Locks

Biased Locks

See Also

References