add mutex support (optional) for tu_fifo

This commit is contained in:
hathach 2018-11-02 17:28:07 +07:00
parent 2708632a6a
commit f6076b0e06
3 changed files with 68 additions and 63 deletions

View File

@ -66,7 +66,6 @@
#include "tusb_verify.h" #include "tusb_verify.h"
#include "binary.h" #include "binary.h"
#include "tusb_error.h" #include "tusb_error.h"
#include "tusb_fifo.h"
#include "tusb_timeout.h" #include "tusb_timeout.h"
#include "tusb_types.h" #include "tusb_types.h"

View File

@ -36,24 +36,51 @@
*/ */
/**************************************************************************/ /**************************************************************************/
#include "tusb_fifo.h" #include <string.h>
#include "common/tusb_verify.h" // for ASSERT
#include "osal/osal.h"
#include "tusb_fifo.h"
// implement mutex lock and unlock
// For OSAL_NONE: if mutex is locked by other, function return immediately (since there is no task context)
// For Real RTOS: fifo lock is a blocking API
#if CFG_FIFO_MUTEX #if CFG_FIFO_MUTEX
#define mutex_lock_if_needed(_ff) if (_ff->mutex) tu_fifo_mutex_lock(_ff->mutex) static bool tu_fifo_lock(tu_fifo_t *f)
#define mutex_unlock_if_needed(_ff) if (_ff->mutex) tu_fifo_mutex_unlock(_ff->mutex) {
if (f->mutex)
{
#if CFG_TUSB_OS == OPT_OS_NONE
// There is no subtask context for blocking mutex, we will check and return if cannot lock the mutex
if ( !osal_mutex_lock_notask(f->mutex) ) return false;
#else
uint32_t err;
(void) err;
osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER, &err);
#endif
}
return true;
}
static void tu_fifo_unlock(tu_fifo_t *f)
{
if (f->mutex)
{
osal_mutex_unlock(f->mutex);
}
}
#else #else
#define mutex_lock_if_needed(_ff) #define tu_fifo_lock(_ff) true
#define mutex_unlock_if_needed(_ff) #define tu_fifo_unlock(_ff)
#endif #endif
void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable) bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
{ {
mutex_lock_if_needed(f); if ( !tu_fifo_lock(f) ) return false;
f->buffer = (uint8_t*) buffer; f->buffer = (uint8_t*) buffer;
f->depth = depth; f->depth = depth;
@ -62,7 +89,9 @@ void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
f->rd_idx = f->wr_idx = f->count = 0; f->rd_idx = f->wr_idx = f->count = 0;
mutex_unlock_if_needed(f); tu_fifo_unlock(f);
return true;
} }
@ -86,7 +115,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
{ {
if( tu_fifo_empty(f) ) return false; if( tu_fifo_empty(f) ) return false;
mutex_lock_if_needed(f); if ( !tu_fifo_lock(f) ) return false;
memcpy(p_buffer, memcpy(p_buffer,
f->buffer + (f->rd_idx * f->item_size), f->buffer + (f->rd_idx * f->item_size),
@ -94,7 +123,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
f->rd_idx = (f->rd_idx + 1) % f->depth; f->rd_idx = (f->rd_idx + 1) % f->depth;
f->count--; f->count--;
mutex_unlock_if_needed(f); tu_fifo_unlock(f);
return true; return true;
} }
@ -122,8 +151,6 @@ uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count)
/* Limit up to fifo's count */ /* Limit up to fifo's count */
if ( count > f->count ) count = f->count; if ( count > f->count ) count = f->count;
mutex_lock_if_needed(f);
/* Could copy up to 2 portions marked as 'x' if queue is wrapped around /* Could copy up to 2 portions marked as 'x' if queue is wrapped around
* case 1: ....RxxxxW....... * case 1: ....RxxxxW.......
* case 2: xxxxxW....Rxxxxxx * case 2: xxxxxW....Rxxxxxx
@ -138,8 +165,6 @@ uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count)
p_buf += f->item_size; p_buf += f->item_size;
} }
mutex_unlock_if_needed(f);
return len; return len;
} }
@ -189,10 +214,9 @@ bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t pos, void * p_buffer)
/******************************************************************************/ /******************************************************************************/
bool tu_fifo_write (tu_fifo_t* f, const void * p_data) bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
{ {
// if ( tu_fifo_full(f) && !f->overwritable ) return false; if ( tu_fifo_full(f) && !f->overwritable ) return false;
TU_ASSERT( !(tu_fifo_full(f) && !f->overwritable) );
mutex_lock_if_needed(f); if ( !tu_fifo_lock(f) ) return false;
memcpy( f->buffer + (f->wr_idx * f->item_size), memcpy( f->buffer + (f->wr_idx * f->item_size),
p_data, p_data,
@ -209,7 +233,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
f->count++; f->count++;
} }
mutex_unlock_if_needed(f); tu_fifo_unlock(f);
return true; return true;
} }
@ -233,7 +257,7 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count)
{ {
if ( count == 0 ) return 0; if ( count == 0 ) return 0;
uint8_t* p_buf = (uint8_t*) p_data; uint8_t const* p_buf = (uint8_t const*) p_data;
uint16_t len = 0; uint16_t len = 0;
while( (len < count) && tu_fifo_write(f, p_buf) ) while( (len < count) && tu_fifo_write(f, p_buf) )
@ -253,11 +277,13 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count)
Pointer to the FIFO buffer to manipulate Pointer to the FIFO buffer to manipulate
*/ */
/******************************************************************************/ /******************************************************************************/
void tu_fifo_clear(tu_fifo_t *f) bool tu_fifo_clear(tu_fifo_t *f)
{ {
mutex_lock_if_needed(f); if ( !tu_fifo_lock(f) ) return false;
f->rd_idx = f->wr_idx = f->count = 0; f->rd_idx = f->wr_idx = f->count = 0;
mutex_unlock_if_needed(f); tu_fifo_unlock(f);
return true;
} }

View File

@ -43,42 +43,17 @@
#ifndef _TUSB_FIFO_H_ #ifndef _TUSB_FIFO_H_
#define _TUSB_FIFO_H_ #define _TUSB_FIFO_H_
#define CFG_FIFO_MUTEX 0 #define CFG_FIFO_MUTEX 1
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#if CFG_FIFO_MUTEX #if CFG_FIFO_MUTEX
#define tu_fifo_mutex_t osal_mutex_t
#include "osal/osal.h"
#if CFG_TUSB_OS == OPT_OS_NONE
// Since all fifo read/write is done in thread mode, there should be
// no conflict except for osal queue which will be address seperatedly.
// Therefore there may be no need for mutex with internal use of fifo
#define _ff_mutex_def(mutex)
#else
#define tu_fifo_mutex_t struct os_mutex
#define tu_fifo_mutex_lock(m) os_mutex_pend(m, OS_TIMEOUT_NEVER)
#define tu_fifo_mutex_unlock(m) os_mutex_release(m)
/* Internal use only */
#define _mutex_declare(m) .mutex = m
#endif
#else
#define _mutex_declare(m)
#endif #endif
@ -98,24 +73,29 @@ typedef struct
bool overwritable ; bool overwritable ;
#if CFG_FIFO_MUTEX #if CFG_FIFO_MUTEX
tu_fifo_mutex_t * const mutex; tu_fifo_mutex_t mutex;
#endif #endif
} tu_fifo_t; } tu_fifo_t;
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) /*, irq_mutex)*/ \ #define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
uint8_t _name##_buf[_depth*sizeof(_type)];\ uint8_t _name##_buf[_depth*sizeof(_type)]; \
tu_fifo_t _name = {\ tu_fifo_t _name = { \
.buffer = _name##_buf,\ .buffer = _name##_buf, \
.depth = _depth,\ .depth = _depth, \
.item_size = sizeof(_type),\ .item_size = sizeof(_type), \
.overwritable = _overwritable,\ .overwritable = _overwritable, \
/*.irq = irq_mutex*/\
_mutex_declare(_mutex)\
} }
void tu_fifo_clear(tu_fifo_t *f); bool tu_fifo_clear(tu_fifo_t *f);
void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable); bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
#if CFG_FIFO_MUTEX
static inline void tu_fifo_config_mutex(tu_fifo_t *f, tu_fifo_mutex_t mutex_hdl)
{
f->mutex = mutex_hdl;
}
#endif
bool tu_fifo_write (tu_fifo_t* f, void const * p_data); bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t count); uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t count);