86 lines
1.7 KiB
C
86 lines
1.7 KiB
C
#ifndef _DS_RINGBUFFER_H_
|
|
#define _DS_RINGBUFFER_H_
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
|
|
#include "list.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef void (*ds_rb_lock_ptr_t)(void *, int);
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t *data;
|
|
ssize_t size;
|
|
ssize_t pread;
|
|
ssize_t pwrite;
|
|
|
|
int flag;
|
|
#define DS_RB_FLAG_READY 0x1
|
|
#define DS_RB_FLAG_BLOCK 0x10
|
|
#define DS_RB_FLAG_OVERFLOW 0x100
|
|
#define DS_RB_FLAG_EXIT 0x1000
|
|
|
|
pthread_cond_t cond;
|
|
pthread_mutex_t lock;
|
|
|
|
/*block queue*/
|
|
struct list_head queue;
|
|
pthread_mutex_t queue_lock;
|
|
|
|
int initialized;
|
|
|
|
ds_rb_lock_ptr_t lock_g_ptr;
|
|
void *g;
|
|
} ds_rb_t;
|
|
|
|
#define DS_RB_IS_READY(_rb) ((_rb)->data)
|
|
|
|
int ds_rb_init_lock(ds_rb_t *rb, ds_rb_lock_ptr_t lock_g_ptr, void *g);
|
|
|
|
int ds_rb_init(ds_rb_t *rb, size_t size);
|
|
|
|
int ds_rb_deinit(ds_rb_t *rb);
|
|
|
|
int ds_rb_empty(ds_rb_t *rb);
|
|
|
|
ssize_t ds_rb_free(ds_rb_t *rb);
|
|
|
|
ssize_t ds_rb_avail(ds_rb_t *rb);
|
|
|
|
ssize_t ds_rb_read(ds_rb_t *rb, uint8_t *buf, size_t len);
|
|
|
|
ssize_t ds_rb_write(ds_rb_t *rb, const uint8_t *buf, size_t len);
|
|
|
|
/*permanently add the flag*/
|
|
int ds_rb_set_flag(ds_rb_t *rb, int flag);
|
|
|
|
int ds_rb_get_flag_timeout(ds_rb_t *rb, int timeout_ms);
|
|
#define ds_rb_get_flag(_rb) ds_rb_get_flag_timeout((_rb), 0)
|
|
|
|
/*
|
|
block data helper
|
|
|
|
Producer:
|
|
1. add the data in ringbuffer
|
|
2. queue the block token(size)
|
|
|
|
Consumer:
|
|
1. dequeue the block token(size)
|
|
2. read the data block from ringbuffer
|
|
*/
|
|
int ds_rb_queue_block(ds_rb_t *rb, size_t bs);
|
|
int ds_rb_dequeue_block(ds_rb_t *rb, size_t *bs);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|