summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/tusb_fifo.c16
-rw-r--r--src/common/tusb_fifo.h1
-rw-r--r--src/common/tusb_private.h4
3 files changed, 21 insertions, 0 deletions
diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c
index e97b6ccce..28086be0b 100644
--- a/src/common/tusb_fifo.c
+++ b/src/common/tusb_fifo.c
@@ -95,6 +95,22 @@ void tu_fifo_clear(tu_fifo_t *f) {
ff_unlock(f->mutex_rd);
}
+// synchronously count then clear fifo, returning the count
+uint16_t tu_fifo_count_and_clear(tu_fifo_t *f) {
+ ff_lock(f->mutex_wr);
+ ff_lock(f->mutex_rd);
+
+ uint16_t cnt = tu_fifo_count(f);
+
+ f->rd_idx = 0;
+ f->wr_idx = 0;
+
+ ff_unlock(f->mutex_wr);
+ ff_unlock(f->mutex_rd);
+
+ return cnt;
+}
+
// Change the fifo overwritable mode
void tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable) {
if (f->overwritable == overwritable) {
diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h
index 2e2a0db6f..75eeded01 100644
--- a/src/common/tusb_fifo.h
+++ b/src/common/tusb_fifo.h
@@ -159,6 +159,7 @@ typedef enum {
bool tu_fifo_config(tu_fifo_t *f, void *buffer, uint16_t depth, uint16_t item_size, bool overwritable);
void tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable);
void tu_fifo_clear(tu_fifo_t *f);
+uint16_t tu_fifo_count_and_clear(tu_fifo_t *f);
#if OSAL_MUTEX_REQUIRED
TU_ATTR_ALWAYS_INLINE static inline
diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h
index 10e12c2af..0c1709d49 100644
--- a/src/common/tusb_private.h
+++ b/src/common/tusb_private.h
@@ -144,6 +144,10 @@ TU_ATTR_ALWAYS_INLINE static inline void tu_edpt_stream_clear(tu_edpt_stream_t *
tu_fifo_clear(&s->ff);
}
+TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_stream_count_and_clear(tu_edpt_stream_t *s) {
+ return tu_fifo_count_and_clear(&s->ff);
+}
+
TU_ATTR_ALWAYS_INLINE static inline bool tu_edpt_stream_empty(tu_edpt_stream_t *s) {
return tu_fifo_empty(&s->ff);
}