mirror of
https://github.com/hathach/tinyusb.git
synced 2025-01-31 05:52:55 +08:00
Adapt mutexes in fifo.c
This commit is contained in:
parent
2e28861cf8
commit
de1f36f2b0
@ -39,44 +39,26 @@
|
|||||||
// implement mutex lock and unlock
|
// implement mutex lock and unlock
|
||||||
#if CFG_FIFO_MUTEX
|
#if CFG_FIFO_MUTEX
|
||||||
|
|
||||||
static void tu_fifo_lock_wr(tu_fifo_t *f)
|
static void tu_fifo_lock(tu_fifo_mutex_t mutex)
|
||||||
{
|
{
|
||||||
if (f->mutex_wr)
|
if (mutex)
|
||||||
{
|
{
|
||||||
osal_mutex_lock(f->mutex_wr, OSAL_TIMEOUT_WAIT_FOREVER);
|
osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tu_fifo_unlock_wr(tu_fifo_t *f)
|
static void tu_fifo_unlock(tu_fifo_mutex_t mutex)
|
||||||
{
|
{
|
||||||
if (f->mutex_wr)
|
if (mutex)
|
||||||
{
|
{
|
||||||
osal_mutex_unlock(f->mutex_wr);
|
osal_mutex_unlock(mutex);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void tu_fifo_lock_rd(tu_fifo_t *f)
|
|
||||||
{
|
|
||||||
if (f->mutex_rd)
|
|
||||||
{
|
|
||||||
osal_mutex_lock(f->mutex_rd, OSAL_TIMEOUT_WAIT_FOREVER);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void tu_fifo_unlock_rd(tu_fifo_t *f)
|
|
||||||
{
|
|
||||||
if (f->mutex_rd)
|
|
||||||
{
|
|
||||||
osal_mutex_unlock(f->mutex_rd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#define tu_fifo_lock_wr(_ff)
|
#define tu_fifo_lock(_mutex)
|
||||||
#define tu_fifo_unlock_wr(_ff)
|
#define tu_fifo_unlock(_mutex)
|
||||||
#define tu_fifo_lock_rd(_ff)
|
|
||||||
#define tu_fifo_unlock_rd(_ff)
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -93,8 +75,8 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
|
|||||||
{
|
{
|
||||||
if (depth > 0x8000) return false; // Maximum depth is 2^15 items
|
if (depth > 0x8000) return false; // Maximum depth is 2^15 items
|
||||||
|
|
||||||
tu_fifo_lock_wr(f);
|
tu_fifo_lock(f->mutex_wr);
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
|
|
||||||
f->buffer = (uint8_t*) buffer;
|
f->buffer = (uint8_t*) buffer;
|
||||||
f->depth = depth;
|
f->depth = depth;
|
||||||
@ -106,8 +88,8 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
|
|||||||
|
|
||||||
f->rd_idx = f->wr_idx = 0;
|
f->rd_idx = f->wr_idx = 0;
|
||||||
|
|
||||||
tu_fifo_unlock_wr(f);
|
tu_fifo_unlock(f->mutex_wr);
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -401,7 +383,7 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
|
|||||||
{
|
{
|
||||||
if ( n == 0 ) return 0;
|
if ( n == 0 ) return 0;
|
||||||
|
|
||||||
tu_fifo_lock_wr(f);
|
tu_fifo_lock(f->mutex_wr);
|
||||||
|
|
||||||
uint16_t w = f->wr_idx, r = f->rd_idx;
|
uint16_t w = f->wr_idx, r = f->rd_idx;
|
||||||
uint8_t const* buf8 = (uint8_t const*) data;
|
uint8_t const* buf8 = (uint8_t const*) data;
|
||||||
@ -431,14 +413,14 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
|
|||||||
// Advance pointer
|
// Advance pointer
|
||||||
f->wr_idx = advance_pointer(f, w, n);
|
f->wr_idx = advance_pointer(f, w, n);
|
||||||
|
|
||||||
tu_fifo_unlock_wr(f);
|
tu_fifo_unlock(f->mutex_wr);
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo_copy_mode_t copy_mode)
|
static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo_copy_mode_t copy_mode)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
|
|
||||||
// Peek the data
|
// Peek the data
|
||||||
n = _tu_fifo_peek_at_n(f, 0, buffer, n, f->wr_idx, f->rd_idx, copy_mode); // f->rd_idx might get modified in case of an overflow so we can not use a local variable
|
n = _tu_fifo_peek_at_n(f, 0, buffer, n, f->wr_idx, f->rd_idx, copy_mode); // f->rd_idx might get modified in case of an overflow so we can not use a local variable
|
||||||
@ -446,7 +428,7 @@ static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo
|
|||||||
// Advance read pointer
|
// Advance read pointer
|
||||||
f->rd_idx = advance_pointer(f, f->rd_idx, n);
|
f->rd_idx = advance_pointer(f, f->rd_idx, n);
|
||||||
|
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -553,9 +535,9 @@ bool tu_fifo_overflowed(tu_fifo_t* f)
|
|||||||
// Only use in case tu_fifo_overflow() returned true!
|
// Only use in case tu_fifo_overflow() returned true!
|
||||||
void tu_fifo_correct_read_pointer(tu_fifo_t* f)
|
void tu_fifo_correct_read_pointer(tu_fifo_t* f)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
_tu_fifo_correct_read_pointer(f, f->wr_idx);
|
_tu_fifo_correct_read_pointer(f, f->wr_idx);
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@ -576,7 +558,7 @@ void tu_fifo_correct_read_pointer(tu_fifo_t* f)
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
bool tu_fifo_read(tu_fifo_t* f, void * buffer)
|
bool tu_fifo_read(tu_fifo_t* f, void * buffer)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
|
|
||||||
// Peek the data
|
// Peek the data
|
||||||
bool ret = _tu_fifo_peek_at(f, 0, buffer, f->wr_idx, f->rd_idx); // f->rd_idx might get modified in case of an overflow so we can not use a local variable
|
bool ret = _tu_fifo_peek_at(f, 0, buffer, f->wr_idx, f->rd_idx); // f->rd_idx might get modified in case of an overflow so we can not use a local variable
|
||||||
@ -584,7 +566,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * buffer)
|
|||||||
// Advance pointer
|
// Advance pointer
|
||||||
f->rd_idx = advance_pointer(f, f->rd_idx, ret);
|
f->rd_idx = advance_pointer(f, f->rd_idx, ret);
|
||||||
|
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -635,8 +617,8 @@ uint16_t tu_fifo_read_n_const_addr(tu_fifo_t* f, void * buffer, uint16_t n)
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
uint16_t tu_fifo_read_n_into_other_fifo(tu_fifo_t* f, tu_fifo_t* f_target, uint16_t offset, uint16_t n)
|
uint16_t tu_fifo_read_n_into_other_fifo(tu_fifo_t* f, tu_fifo_t* f_target, uint16_t offset, uint16_t n)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
tu_fifo_lock_wr(f_target);
|
tu_fifo_lock(f_target->mutex_wr);
|
||||||
|
|
||||||
// Conduct copy
|
// Conduct copy
|
||||||
n = tu_fifo_peek_n_into_other_fifo(f, f_target, offset, n);
|
n = tu_fifo_peek_n_into_other_fifo(f, f_target, offset, n);
|
||||||
@ -644,8 +626,8 @@ uint16_t tu_fifo_read_n_into_other_fifo(tu_fifo_t* f, tu_fifo_t* f_target, uint1
|
|||||||
// Advance read pointer
|
// Advance read pointer
|
||||||
f->rd_idx = advance_pointer(f, f->rd_idx, n);
|
f->rd_idx = advance_pointer(f, f->rd_idx, n);
|
||||||
|
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
tu_fifo_unlock_wr(f_target);
|
tu_fifo_unlock(f_target->mutex_wr);
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
@ -667,9 +649,9 @@ uint16_t tu_fifo_read_n_into_other_fifo(tu_fifo_t* f, tu_fifo_t* f_target, uint1
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t offset, void * p_buffer)
|
bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t offset, void * p_buffer)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
bool ret = _tu_fifo_peek_at(f, offset, p_buffer, f->wr_idx, f->rd_idx);
|
bool ret = _tu_fifo_peek_at(f, offset, p_buffer, f->wr_idx, f->rd_idx);
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -692,9 +674,9 @@ bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t offset, void * p_buffer)
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
uint16_t tu_fifo_peek_at_n(tu_fifo_t* f, uint16_t offset, void * p_buffer, uint16_t n)
|
uint16_t tu_fifo_peek_at_n(tu_fifo_t* f, uint16_t offset, void * p_buffer, uint16_t n)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
bool ret = _tu_fifo_peek_at_n(f, offset, p_buffer, n, f->wr_idx, f->rd_idx, TU_FIFO_COPY_INC);
|
bool ret = _tu_fifo_peek_at_n(f, offset, p_buffer, n, f->wr_idx, f->rd_idx, TU_FIFO_COPY_INC);
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -741,7 +723,7 @@ uint16_t tu_fifo_peek_n_into_other_fifo (tu_fifo_t* f, tu_fifo_t* f_target, uint
|
|||||||
cnt -= offset;
|
cnt -= offset;
|
||||||
if (cnt < n) n = cnt;
|
if (cnt < n) n = cnt;
|
||||||
|
|
||||||
tu_fifo_lock_wr(f_target); // Lock both read and write pointers - in case of an overwritable FIFO both may be modified
|
tu_fifo_lock(f_target->mutex_wr); // Lock both read and write pointers - in case of an overwritable FIFO both may be modified
|
||||||
|
|
||||||
uint16_t wr_rel_tgt = get_relative_pointer(f_target, f_target->wr_idx, 0);
|
uint16_t wr_rel_tgt = get_relative_pointer(f_target, f_target->wr_idx, 0);
|
||||||
|
|
||||||
@ -779,7 +761,7 @@ uint16_t tu_fifo_peek_n_into_other_fifo (tu_fifo_t* f, tu_fifo_t* f_target, uint
|
|||||||
_tu_fifo_peek_at_n(f, offset + sz, f_target->buffer, n-sz, f_wr_idx, f_rd_idx, TU_FIFO_COPY_INC);
|
_tu_fifo_peek_at_n(f, offset + sz, f_target->buffer, n-sz, f_wr_idx, f_rd_idx, TU_FIFO_COPY_INC);
|
||||||
}
|
}
|
||||||
|
|
||||||
tu_fifo_unlock_wr(f_target);
|
tu_fifo_unlock(f_target->mutex_wr);
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
@ -802,7 +784,7 @@ uint16_t tu_fifo_peek_n_into_other_fifo (tu_fifo_t* f, tu_fifo_t* f_target, uint
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
bool tu_fifo_write(tu_fifo_t* f, const void * data)
|
bool tu_fifo_write(tu_fifo_t* f, const void * data)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_wr(f);
|
tu_fifo_lock(f->mutex_wr);
|
||||||
|
|
||||||
uint16_t w = f->wr_idx;
|
uint16_t w = f->wr_idx;
|
||||||
|
|
||||||
@ -816,7 +798,7 @@ bool tu_fifo_write(tu_fifo_t* f, const void * data)
|
|||||||
// Advance pointer
|
// Advance pointer
|
||||||
f->wr_idx = advance_pointer(f, w, 1);
|
f->wr_idx = advance_pointer(f, w, 1);
|
||||||
|
|
||||||
tu_fifo_unlock_wr(f);
|
tu_fifo_unlock(f->mutex_wr);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -870,13 +852,13 @@ uint16_t tu_fifo_write_n_const_addr(tu_fifo_t* f, const void * data, uint16_t n)
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
bool tu_fifo_clear(tu_fifo_t *f)
|
bool tu_fifo_clear(tu_fifo_t *f)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_wr(f);
|
tu_fifo_lock(f->mutex_wr);
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
f->rd_idx = f->wr_idx = 0;
|
f->rd_idx = f->wr_idx = 0;
|
||||||
f->max_pointer_idx = 2*f->depth-1;
|
f->max_pointer_idx = 2*f->depth-1;
|
||||||
f->non_used_index_space = UINT16_MAX - f->max_pointer_idx;
|
f->non_used_index_space = UINT16_MAX - f->max_pointer_idx;
|
||||||
tu_fifo_unlock_wr(f);
|
tu_fifo_unlock(f->mutex_wr);
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -892,13 +874,13 @@ bool tu_fifo_clear(tu_fifo_t *f)
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable)
|
bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_wr(f);
|
tu_fifo_lock(f->mutex_wr);
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
|
|
||||||
f->overwritable = overwritable;
|
f->overwritable = overwritable;
|
||||||
|
|
||||||
tu_fifo_unlock_wr(f);
|
tu_fifo_unlock(f->mutex_wr);
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1021,9 +1003,9 @@ uint16_t tu_fifo_get_linear_read_info(tu_fifo_t *f, uint16_t offset, void **ptr,
|
|||||||
// Check overflow and correct if required
|
// Check overflow and correct if required
|
||||||
if (cnt > f->depth)
|
if (cnt > f->depth)
|
||||||
{
|
{
|
||||||
tu_fifo_lock_rd(f);
|
tu_fifo_lock(f->mutex_rd);
|
||||||
_tu_fifo_correct_read_pointer(f, w);
|
_tu_fifo_correct_read_pointer(f, w);
|
||||||
tu_fifo_unlock_rd(f);
|
tu_fifo_unlock(f->mutex_rd);
|
||||||
r = f->rd_idx;
|
r = f->rd_idx;
|
||||||
cnt = f->depth;
|
cnt = f->depth;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user