1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-17 18:43:50 +01:00
RIOT/sys/isrpipe/isrpipe.c
Joshua DeWeese 4d39d6e2f3 sys/isrpipe: fix init of mutex
The mutex used to sync the reader and writer of the pipe is initialized
as unlocked. This results in a bit of wasted CPU cycles the first time a
read blocks. This patch inits the mutex in a locked state so that the
first blocking read blocks immediately.
2025-04-08 20:17:04 -04:00

58 lines
1.2 KiB
C

/*
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys
* @{
* @file
* @brief ISR -> userspace pipe implementation
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include "isrpipe.h"
void isrpipe_init(isrpipe_t *isrpipe, uint8_t *buf, size_t bufsize)
{
isrpipe->mutex = (mutex_t)MUTEX_INIT_LOCKED;
tsrb_init(&isrpipe->tsrb, buf, bufsize);
}
int isrpipe_write_one(isrpipe_t *isrpipe, uint8_t c)
{
int res = tsrb_add_one(&isrpipe->tsrb, c);
/* `res` is either 0 on success or -1 when the buffer is full. Either way,
* unlocking the mutex is fine.
*/
mutex_unlock(&isrpipe->mutex);
return res;
}
int isrpipe_write(isrpipe_t *isrpipe, const uint8_t *buf, size_t n)
{
int res = tsrb_add(&isrpipe->tsrb, buf, n);
mutex_unlock(&isrpipe->mutex);
return res;
}
int isrpipe_read(isrpipe_t *isrpipe, uint8_t *buffer, size_t count)
{
int res;
while (!(res = tsrb_get(&isrpipe->tsrb, buffer, count))) {
mutex_lock(&isrpipe->mutex);
}
return res;
}