-
Notifications
You must be signed in to change notification settings - Fork 19
/
spin_cycle_queue.c
42 lines (39 loc) · 1.42 KB
/
spin_cycle_queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include"spin_cycle_queue.h"
static const uchar SPIN_QUEUE_SIZE = 20;
static cycle_queue_t pdata local_cycle_queue[2];
static bool pdata queue_index;
static uchar pdata local_queue_content[2][20];//SPIN_QUEUE_SIZE
static bool cycle_queue_full(cycle_queue_t *cycle_queue){
if(cycle_queue->cur_rd - cycle_queue->cur_wrt==1)
return true;
else return false;
}
static bool cycle_queue_empty(cycle_queue_t *cycle_queue){
if(cycle_queue->cur_wrt == cycle_queue->cur_rd)
return true;
else return false;
}
uchar cycle_queue_init(){
local_cycle_queue[queue_index].queue_content=local_queue_content[queue_index];
local_cycle_queue->head = 0;
local_cycle_queue->tail = SPIN_QUEUE_SIZE;
local_cycle_queue->cur_wrt = local_cycle_queue->cur_rd = 0;
return queue_index;
}
//先判断pop函数返回值,正常返回,content才有效
uchar cycle_queue_pop(uchar fd){
local_cycle_queue[fd].cur_rd%=local_cycle_queue[fd].tail;
if(cycle_queue_empty(local_cycle_queue+fd))
return false;
else
return local_cycle_queue[fd].queue_content[local_cycle_queue[fd].cur_rd++];
}
bool cycle_queue_push(uchar fd,uchar content){
local_cycle_queue[fd].cur_wrt%= local_cycle_queue[fd].tail;
if(cycle_queue_full(local_cycle_queue+fd))
return false;
else
local_cycle_queue[fd].queue_content[local_cycle_queue[fd].cur_wrt++] = content;
content = local_cycle_queue[fd].queue_content[local_cycle_queue[fd].cur_wrt-1];
return true;
}