ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_atomic.h File Reference
#include <stdbool.h>
#include <stdint.h>
#include "ruuvi_driver_enabled_modules.h"

Go to the source code of this file.

Macros

#define RI_ATOMIC_FLAG_INIT   0
 Enable implementation selected by application. More...
 

Typedefs

typedef volatile uint32_t ri_atomic_t
 define atomic type - not portable to 8-bit. More...
 

Functions

bool ri_atomic_flag (ri_atomic_t *const flag, const bool set)
 Atomic flag check and set/clear function. More...
 

Detailed Description

Author
Otso Jousimaa otso@.nosp@m.ojou.nosp@m.sima..nosp@m.net
Date
2019-07-23

Interface for basic atomic operations.

Definition in file ruuvi_interface_atomic.h.

Macro Definition Documentation

◆ RI_ATOMIC_FLAG_INIT

#define RI_ATOMIC_FLAG_INIT   0

Enable implementation selected by application.

Initial value for atomic flag.

Definition at line 29 of file ruuvi_interface_atomic.h.

Typedef Documentation

◆ ri_atomic_t

typedef volatile uint32_t ri_atomic_t

define atomic type - not portable to 8-bit.

Definition at line 31 of file ruuvi_interface_atomic.h.

Function Documentation

◆ ri_atomic_flag()

bool ri_atomic_flag ( ri_atomic_t *const  flag,
const bool  set 
)

Atomic flag check and set/clear function.

Uses whatever mechanism underlying platform provides to check and set or clear flag. When implementing mutex, check-and-set flag to reserve a mutex and check-and-clear to free it.

Generally used like this:

static ri_atomic_t readlock;
if(!ri_atomic_flag(&readlock, true)) { return RUUVI_LIBRARY_ERROR_CONCURRENCY; }
do_some_critical_stuff();
if(!ri_atomic_flag(&readlock, false)){ return RUUVI_LIBRARY_ERROR_FATAL; }
bool ri_atomic_flag(ri_atomic_t *const flag, const bool set)
Atomic flag check and set/clear function.
volatile uint32_t ri_atomic_t
define atomic type - not portable to 8-bit.

It's important to return if lock can't be had rather than busylooping: if the interrupt level which fails to get lock is higher than the call which has the lock program will deadlock in the busyloop. Likewise failure to release the lock will cause deadlock in the next execution, fail immediately.

Parameters
[in]flaguint32_t address of bitfield to check.
[in]settrue to set flag, false to clear flag.
Returns
true if operation was successful. false otherwise.