typedef struct _buf_t{
uint8_t ready;
int len;
char * buf;
struct _buf_t * next;
} buf_t;
buf_t * buf_list = NULL;
/*
count块数量,len单块buf长度
*/
void init_buf_list(int count){
buf_t * t = NULL;
for(int i=0;i<count;i++){
if(t == NULL){
t = (buf_t *)malloc(sizeof(struct _buf_t));
buf_list = t;
}
else {
t->next = (buf_t *)malloc(sizeof(struct _buf_t));
t = t->next;
}
t->ready = 0;
t->len = 256;
t->buf = malloc(t->len);
t->next = buf_list;
}
}
static void read_task(void *arg)
{
buf_t * t = buf_list;
void(*read_func)(void *, int, size_t *)=arg;
while(true){
//wait buffer ready
while(t->ready == 1){
printf("drop\n");
}
size_t bytes_read = 0;
if(read_func != NULL){
read_func(t->buf,
t->len, // the doc says bytes, but its elements.
&bytes_read);
}
t->ready = 1;
t = t->next;
}
}
static void wirte_task(void *arg)
{
buf_t * t = buf_list;
void(*write_func)(void *,int)=arg;
while(true){
//wait buffer ready
while(t->ready == 0);
if(write_func != NULL)
write_func(t->buf,t->len);
t->ready = 0;
t = t->next;
}
}
环形缓冲C代码
最新推荐文章于 2022-12-05 10:47:11 发布