mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sys/bitfield: add atomic variants of write functions
This commit is contained in:
parent
83f1561053
commit
9b09f673fd
@ -32,6 +32,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "irq.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -56,6 +57,19 @@ static inline void bf_set(uint8_t field[], size_t idx)
|
||||
field[idx / 8] |= (1u << (7 - (idx % 8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically set the bit to 1
|
||||
*
|
||||
* @param[in,out] field The bitfield
|
||||
* @param[in] idx The number of the bit to set
|
||||
*/
|
||||
static inline void bf_set_atomic(uint8_t field[], size_t idx)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_set(field, idx);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear the bit
|
||||
*
|
||||
@ -67,6 +81,19 @@ static inline void bf_unset(uint8_t field[], size_t idx)
|
||||
field[idx / 8] &= ~(1u << (7 - (idx % 8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically clear the bit
|
||||
*
|
||||
* @param[in,out] field The bitfield
|
||||
* @param[in] idx The number of the bit to clear
|
||||
*/
|
||||
static inline void bf_unset_atomic(uint8_t field[], size_t idx)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_unset(field, idx);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Toggle the bit
|
||||
*
|
||||
@ -78,6 +105,19 @@ static inline void bf_toggle(uint8_t field[], size_t idx)
|
||||
field[idx / 8] ^= (1u << (7 - (idx % 8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically toggle the bit
|
||||
*
|
||||
* @param[in,out] field The bitfield
|
||||
* @param[in] idx The number of the bit to toggle
|
||||
*/
|
||||
static inline void bf_toggle_atomic(uint8_t field[], size_t idx)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_toggle(field, idx);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the bet is set
|
||||
*
|
||||
@ -111,6 +151,28 @@ static inline void bf_or(uint8_t out[], const uint8_t a[], const uint8_t b[], si
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically perform a bitwise OR operation on two bitfields
|
||||
* `out = a | b`
|
||||
*
|
||||
* @pre The size of @p a, @p b and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The first bitfield
|
||||
* @param[in] b The second bitfield
|
||||
* @param[in] len The number of bits in the bitfields
|
||||
*/
|
||||
static inline void bf_or_atomic(uint8_t out[], const uint8_t a[],
|
||||
const uint8_t b[], size_t len)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_or(out, a, b, len);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform a bitwise AND operation on two bitfields
|
||||
* `out = a & b`
|
||||
@ -133,6 +195,28 @@ static inline void bf_and(uint8_t out[], const uint8_t a[], const uint8_t b[], s
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically perform a bitwise AND operation on two bitfields
|
||||
* `out = a & b`
|
||||
*
|
||||
* @pre The size of @p a, @p b and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The first bitfield
|
||||
* @param[in] b The second bitfield
|
||||
* @param[in] len The number of bits in the bitfields
|
||||
*/
|
||||
static inline void bf_and_atomic(uint8_t out[], const uint8_t a[],
|
||||
const uint8_t b[], size_t len)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_and(out, a, b, len);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform a bitwise XOR operation on two bitfields
|
||||
* `out = a ^ b`
|
||||
@ -155,6 +239,28 @@ static inline void bf_xor(uint8_t out[], const uint8_t a[], const uint8_t b[], s
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically perform a bitwise XOR operation on two bitfields
|
||||
* `out = a ^ b`
|
||||
*
|
||||
* @pre The size of @p a, @p b and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The first bitfield
|
||||
* @param[in] b The second bitfield
|
||||
* @param[in] len The number of bits in the bitfields
|
||||
*/
|
||||
static inline void bf_xor_atomic(uint8_t out[], const uint8_t a[],
|
||||
const uint8_t b[], size_t len)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_xor(out, a, b, len);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform a bitwise NOT operation on a bitfield
|
||||
* `out = ~a`
|
||||
@ -176,6 +282,26 @@ static inline void bf_inv(uint8_t out[], const uint8_t a[], size_t len)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically perform a bitwise NOT operation on a bitfield
|
||||
* `out = ~a`
|
||||
*
|
||||
* @pre The size of @p a and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The bitfield to invert
|
||||
* @param[in] len The number of bits in the bitfield
|
||||
*/
|
||||
static inline void bf_inv_atomic(uint8_t out[], const uint8_t a[], size_t len)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_inv(out, a, len);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically get the number of an unset bit and set it
|
||||
*
|
||||
@ -219,6 +345,19 @@ int bf_find_first_unset(const uint8_t field[], size_t size);
|
||||
*/
|
||||
void bf_set_all(uint8_t field[], size_t size);
|
||||
|
||||
/**
|
||||
* @brief Atomically set all bits in the bitfield to 1
|
||||
*
|
||||
* @param[in] field The bitfield
|
||||
* @param[in] size The size of the bitfield
|
||||
*/
|
||||
static inline void bf_set_all_atomic(uint8_t field[], size_t size)
|
||||
{
|
||||
unsigned state = irq_disable();
|
||||
bf_set_all(field, size);
|
||||
irq_restore(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Count set bits in the bitfield
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user