mirror of
https://github.com/markqvist/OpenModem.git
synced 2025-05-07 17:05:28 -04:00
Async io work
This commit is contained in:
parent
42dcd121cc
commit
06d138d66c
11 changed files with 246 additions and 94 deletions
87
util/FIFO16.h
Normal file
87
util/FIFO16.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
#ifndef UTIL_FIFO16_H
|
||||
#define UTIL_FIFO16_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <util/atomic.h>
|
||||
|
||||
typedef struct FIFOBuffer16
|
||||
{
|
||||
size_t *begin;
|
||||
size_t *end;
|
||||
size_t * volatile head;
|
||||
size_t * volatile tail;
|
||||
} FIFOBuffer16;
|
||||
|
||||
inline bool fifo16_isempty(const FIFOBuffer16 *f) {
|
||||
return f->head == f->tail;
|
||||
}
|
||||
|
||||
inline bool fifo16_isfull(const FIFOBuffer16 *f) {
|
||||
return ((f->head == f->begin) && (f->tail == f->end)) || (f->tail == f->head - 1);
|
||||
}
|
||||
|
||||
inline void fifo16_push(FIFOBuffer16 *f, size_t c) {
|
||||
*(f->tail) = c;
|
||||
|
||||
if (f->tail == f->end) {
|
||||
f->tail = f->begin;
|
||||
} else {
|
||||
f->tail++;
|
||||
}
|
||||
}
|
||||
|
||||
inline size_t fifo16_pop(FIFOBuffer16 *f) {
|
||||
if(f->head == f->end) {
|
||||
f->head = f->begin;
|
||||
return *(f->end);
|
||||
} else {
|
||||
return *(f->head++);
|
||||
}
|
||||
}
|
||||
|
||||
inline void fifo16_flush(FIFOBuffer16 *f) {
|
||||
f->head = f->tail;
|
||||
}
|
||||
|
||||
static inline bool fifo16_isempty_locked(const FIFOBuffer16 *f) {
|
||||
bool result;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
result = fifo16_isempty(f);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline bool fifo16_isfull_locked(const FIFOBuffer16 *f) {
|
||||
bool result;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
result = fifo16_isfull(f);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline void fifo16_push_locked(FIFOBuffer16 *f, size_t c) {
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
fifo16_push(f, c);
|
||||
}
|
||||
}
|
||||
|
||||
static inline size_t fifo16_pop_locked(FIFOBuffer16 *f) {
|
||||
size_t c;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
c = fifo16_pop(f);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
inline void fifo16_init(FIFOBuffer16 *f, size_t *buffer, size_t size) {
|
||||
f->head = f->tail = f->begin = buffer;
|
||||
f->end = buffer + (size/sizeof(size_t)) - 2;
|
||||
}
|
||||
|
||||
inline size_t fifo16_len(FIFOBuffer16 *f) {
|
||||
return ((f->end - f->begin))/sizeof(size_t);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue