1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19047: sys/bitfield: add atomic variants of write functions r=benpicco a=benpicco



Co-authored-by: Benjamin Valentin <benjamin.valentin@ml-pa.com>
Co-authored-by: Benjamin Valentin <benjamin.valentin@bht-berlin.de>
This commit is contained in:
bors[bot] 2022-12-13 23:27:56 +00:00 committed by GitHub
commit 8459b68654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 170 additions and 9 deletions

View File

@ -8,6 +8,8 @@
#ifndef VIC_H
#define VIC_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -77,9 +79,6 @@ extern "C" {
#define VECT_ADDR_INDEX 0x100
#define VECT_CNTL_INDEX 0x200
#include <stdbool.h>
#include "cpu.h"
bool cpu_install_irq(int IntNumber, void *HandlerAddr, int Priority);
#ifdef __cplusplus

View File

@ -22,8 +22,6 @@
#define CPU_CONF_H
#include "cpu_conf_common.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {

View File

@ -32,6 +32,8 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "irq.h"
#ifdef __cplusplus
extern "C" {
@ -56,6 +58,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 +82,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 +106,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 +152,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 +196,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 +240,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 +283,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
*
@ -193,7 +320,7 @@ int bf_get_unset(uint8_t field[], size_t len);
* @brief Get the index of the first set bit in the field
*
* @param[in] field The bitfield
* @param[in] size The size of the bitfield
* @param[in] size The number of bits in the bitfield
*
* @return number of the first set bit
* @return -1 if no bit is set
@ -204,7 +331,7 @@ int bf_find_first_set(const uint8_t field[], size_t size);
* @brief Get the index of the zero bit in the field
*
* @param[in] field The bitfield
* @param[in] size The size of the bitfield
* @param[in] size The number of bits in the bitfield
*
* @return number of the first unset bit
* @return -1 if all bits are set
@ -215,15 +342,52 @@ int bf_find_first_unset(const uint8_t field[], size_t size);
* @brief Set all bits in the bitfield to 1
*
* @param[in] field The bitfield
* @param[in] size The size of the bitfield
* @param[in] size The number of bits in the bitfield
*/
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 number of bits in 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 Clear all bits in the bitfield to 0
*
* @param[in] field The bitfield
* @param[in] size The number of bits in the bitfield
*/
static inline void bf_clear_all(uint8_t field[], size_t size)
{
memset(field, 0, (size + 7) / 8);
}
/**
* @brief Atomically clear all bits in the bitfield to 0
*
* @param[in] field The bitfield
* @param[in] size The number of bits in the bitfield
*/
static inline void bf_clear_all_atomic(uint8_t field[], size_t size)
{
unsigned state = irq_disable();
bf_clear_all(field, size);
irq_restore(state);
}
/**
* @brief Count set bits in the bitfield
*
* @param[in] field The bitfield
* @param[in] size The size of the bitfield
* @param[in] size The number of bits in the bitfield
*
* @return number of '1' bits in the bitfield
*/