Atomic Operations
[Utility Library]

Collaboration diagram for Atomic Operations:

Data Structures

struct  atomic_object
 Implementation-specific structure holding an atomic object. More...

Defines

#define atomic_inc(ptr)   atomic_add(ptr, 1)
 Increment the memory object at ptr atomically.
#define atomic_dec(ptr)   atomic_sub(ptr, 1)
 Decrement the memory object at ptr atomically.

Typedefs

typedef struct atomic_object atomic_t
 An atomic object representing a value which is manipulated atomically.
typedef uint8_t atomic_value_t
 A value held by an atomic object.

Functions

static atomic_value_t atomic_read (atomic_t *ptr)
 Return the value of the atomic object at ptr.
static void atomic_write (atomic_t *ptr, atomic_value_t value)
 Write value to the atomic object at ptr.
static atomic_t atomic_add (atomic_t *ptr, atomic_value_t value)
 Atomically add value to the value stored at ptr.
static atomic_t atomic_sub (atomic_t *ptr, atomic_value_t value)
 Atomically subtract value from the value stored at ptr.

Detailed Description

Atomic operations allow you to do certain arithmetic read-modify-write operations atomically. The atomic operations guarantee that the memory object being modified will not be altered by any other code while the operation is being carried out.

Normally, when incrementing a variable stored in memory, the processor will have to load the current value from memory into a register, increment the value in the register and store the result back into memory. If for example an interrupt comes along while this happens, and the interrupt handler modifies the same variable, the result will usually be wrong since the value stored by the interrupt handler will be ignored. If the variable is incremented using an atomic operation, the read-modify-write operation will happen in its entirety either before or after the interrupt handler runs, thus avoiding this problem.

One common way to implement atomic operations is to disable interrupts around the operation. However, some architectures may have specialized instructions which allow interrupts to be enabled while the read-modify-write operation is being carried out. It is therefore recommended to use atomic operations instead of disabling interrupts "manually" wherever possible.


Define Documentation

#define atomic_dec ( ptr   )     atomic_sub(ptr, 1)

Decrement the memory object at ptr atomically.

Returns:
The new value stored at ptr

Definition at line 82 of file atomic.h.

#define atomic_inc ( ptr   )     atomic_add(ptr, 1)

Increment the memory object at ptr atomically.

Returns:
The new value stored at ptr

Definition at line 77 of file atomic.h.

Referenced by msc_submit_write_data_req().


Typedef Documentation

An atomic object representing a value which is manipulated atomically.

A value held by an atomic object.

This type will provide the best performance of atomic operations. Currently, it's the only type which can be manipulated atomically.

Definition at line 52 of file atomic.h.


Function Documentation

atomic_t atomic_add ( atomic_t ptr,
atomic_value_t  value 
) [inline, static]

Atomically add value to the value stored at ptr.

Returns:
The result of the addition

Definition at line 78 of file atomic.h.

References cpu_irq_restore(), cpu_irq_save(), and atomic_object::value.

Referenced by msc_submit_read_buffers().

atomic_value_t atomic_read ( atomic_t ptr  )  [inline, static]

Return the value of the atomic object at ptr.

Definition at line 66 of file atomic.h.

References barrier, and atomic_object::value.

Referenced by msc_read_worker(), and msc_write_worker().

atomic_t atomic_sub ( atomic_t ptr,
atomic_value_t  value 
) [inline, static]

Atomically subtract value from the value stored at ptr.

Returns:
The result of the subtraction

Definition at line 91 of file atomic.h.

References cpu_irq_restore(), cpu_irq_save(), and atomic_object::value.

Referenced by msc_submit_read_buffers().

void atomic_write ( atomic_t ptr,
atomic_value_t  value 
) [inline, static]

Write value to the atomic object at ptr.

Definition at line 72 of file atomic.h.

References barrier, and atomic_object::value.

Referenced by msc_do_read().

Generated on Thu Apr 29 14:10:34 2010 for xplain-bc by  doxygen 1.6.3