modm API documentation
modm::atomic::Container< T > Class Template Reference

Atomic access to objects. More...

#include <modm/architecture/driver/atomic/container.hpp>

Public Member Functions

 Container (const T &data=T ())
 
void set (T value)
 
get ()
 
swap (T value)
 Write a new value to the wrapped object and return the old value. More...
 
T & directAccess ()
 Get direct access to the object. More...
 

Detailed Description

template<typename T>
class modm::atomic::Container< T >

Atomic access to objects.

The atomic container wraps objects and provides atomic access to them. This comes in handy when simple objects are accessed by an interrupt and the main program. The container provides secure access without much work in this case.

Warning
This class should be used with precaution because the objects are copied for every access.

Example:

modm::atomic::Containter<uint32_t> data;
// interrupt routine
ISR() {
data.set(123);
}
function() {
uint32_t localData = data.get();
...
}

If the interrupt routine is not interruptible (this is the default for the ATmega, but not for the ATxmega!) no atomic access is needed then:

modm::atomic::Containter<uint32_t> data;
ISR() {
data.directAccess() = 123;
}

This will slightly faster because no lock needs to be created.

If the object is big or a lot of accessed one after another are done, consider dropping this class and create critical sections for the access by yourself with modm::atomic::Lock.

This can be a lot faster because the object don't need to be copied as it is the case here!

Author
Fabian Greif

Member Function Documentation

template<typename T >
T& modm::atomic::Container< T >::directAccess ( )
inline

Get direct access to the object.

Returns
Reference to the object
Warning
If the object is accessed through this function the operations are not atomic! This might be useful inside a interrupt context where no atomic access is necessary.
template<typename T >
T modm::atomic::Container< T >::swap ( value)
inline

Write a new value to the wrapped object and return the old value.

This function is needed quite often when exchanging flags between interrupt routines and the main-loop.

ISR() {
...
isrCounter.directAccess() += 1;
}
function() {
static uint16_t counter;
counter += isrCounter.swap(0);
...
}

The same behavior for boolean values is provide by modm::atomic::Flag()


The documentation for this class was generated from the following file: