Skip to content

Commit 80141ae

Browse files
committed
Update pm_buffer
Updates pm_buffer. Signed-off-by: Mirko Covizzi <[email protected]>
1 parent 6a702cf commit 80141ae

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

lib/peer_manager/include/modules/pm_buffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern "C" {
3434
#define PM_BUFFER_INIT(p_buffer, n_blocks, block_size, err_code) \
3535
do { \
3636
__ALIGN(4) static uint8_t buffer_memory[(n_blocks) * (block_size)]; \
37-
static NRF_ATFLAGS_DEF(mutex_memory, n_blocks); \
37+
static atomic_t mutex_memory[(n_blocks - 1) / (sizeof(atomic_t) * 8) + 1]; \
3838
err_code = pm_buffer_init((p_buffer), buffer_memory, (n_blocks) * (block_size), \
3939
mutex_memory, (n_blocks), (block_size)); \
4040
} while (0)
@@ -46,7 +46,7 @@ typedef struct {
4646
*/
4747
uint8_t *p_memory;
4848
/** @brief A mutex group with one mutex for each buffer entry. */
49-
nrf_atflags_t *p_mutex;
49+
atomic_t *p_mutex;
5050
/** @brief The number of allocatable blocks in the buffer. */
5151
uint32_t n_blocks;
5252
/** @brief The size of each block in the buffer. */
@@ -69,7 +69,7 @@ typedef struct {
6969
* @retval NRF_ERROR_INVALID_PARAM A parameter was 0 or NULL or a size was too small.
7070
*/
7171
uint32_t pm_buffer_init(pm_buffer_t *p_buffer, uint8_t *p_buffer_memory,
72-
uint32_t buffer_memory_size, nrf_atflags_t *p_mutex_memory,
72+
uint32_t buffer_memory_size, atomic_t *p_mutex_memory,
7373
uint32_t n_blocks, uint32_t block_size);
7474

7575
/**

lib/peer_manager/modules/pm_buffer.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,31 @@
77

88
#include <stdbool.h>
99
#include <string.h>
10+
#include <zephyr/sys/atomic.h>
1011
#include <nrf_error.h>
1112
#include <modules/pm_buffer.h>
1213

1314
#define BUFFER_IS_VALID(p_buffer) \
1415
((p_buffer != NULL) && (p_buffer->p_memory != NULL) && (p_buffer->p_mutex != NULL))
1516

16-
static bool mutex_lock(nrf_atflags_t *p_mutex, uint32_t mutex_id)
17+
static bool mutex_lock(atomic_t *mutex, int index)
1718
{
18-
bool locked = !nrf_atflags_fetch_set(p_mutex, mutex_id);
19-
20-
__DMB();
21-
return locked;
19+
return !atomic_test_and_set_bit(mutex, index);
2220
}
2321

24-
static void mutex_unlock(nrf_atflags_t *p_mutex, uint32_t mutex_id)
22+
static void mutex_unlock(atomic_t *mutex, int index)
2523
{
26-
__DMB();
27-
nrf_atflags_clear(p_mutex, mutex_id);
24+
atomic_clear_bit(mutex, index);
2825
}
2926

30-
static bool mutex_lock_status_get(nrf_atflags_t *p_mutex, uint32_t mutex_id)
27+
static bool mutex_lock_status_get(atomic_t *mutex, int index)
3128
{
32-
__DMB();
33-
return nrf_atflags_get(p_mutex, mutex_id);
29+
return atomic_test_bit(mutex, index);
3430
}
3531

3632
uint32_t pm_buffer_init(pm_buffer_t *p_buffer, uint8_t *p_buffer_memory,
37-
uint32_t buffer_memory_size, nrf_atflags_t *p_mutex_memory,
38-
uint32_t n_blocks, uint32_t block_size)
33+
uint32_t buffer_memory_size, atomic_t *p_mutex_memory,
34+
uint32_t n_blocks, uint32_t block_size)
3935
{
4036
if ((p_buffer != NULL) && (p_buffer_memory != NULL) && (p_mutex_memory != NULL) &&
4137
(buffer_memory_size >= (n_blocks * block_size)) && (n_blocks != 0) &&

0 commit comments

Comments
 (0)