-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
167 lines (147 loc) · 5.88 KB
/
queue.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#if !defined( __QUEUE_H__ )
#define __QUEUE_H__
typedef spinlock_t OS_SPIN_LOCK;
#define osFreeSpinLock(pLock) while(0)
void osAcquireSpinLock(OS_SPIN_LOCK *pLock);
void osReleaseSpinLock(OS_SPIN_LOCK *pLock);
void osAllocateSpinLock(OS_SPIN_LOCK *pLock);
//#define osAcquireSpinLock(pLock) spin_lock(pLock)
//#define osReleaseSpinLock(pLock) spin_unlock(pLock)
//#define osAllocateSpinLock(pLock) spin_lock_init(pLock)
typedef struct _TI_LIST_ENTRY {
struct _TI_LIST_ENTRY *Flink;
struct _TI_LIST_ENTRY *Blink;
} TI_LIST_ENTRY, *PTI_LIST_ENTRY, TQE, *PTQE;
typedef struct _TIATM_LIST_ENTRY
{
TI_LIST_ENTRY Link;
} TIATM_LIST_ENTRY, *PTIATM_LIST_ENTRY;
//-------------------------------------------------------------------------
// QueueInitList -- Macro which will initialize a queue to NULL.
//-------------------------------------------------------------------------
#define QueueInitList(_L) (_L)->Link.Flink = (_L)->Link.Blink = (PTI_LIST_ENTRY)0;
//-------------------------------------------------------------------------
// QueueEmpty -- Macro which checks to see if a queue is empty.
//-------------------------------------------------------------------------
#define QueueEmpty(_L) (QueueGetHead((_L)) == (PTIATM_LIST_ENTRY)0)
//-------------------------------------------------------------------------
// QueueGetHead -- Macro which returns the head of the queue, but does not
// remove the head from the queue.
//-------------------------------------------------------------------------
#define QueueGetHead(_L) ((PTIATM_LIST_ENTRY)((_L)->Link.Flink))
#define QueueGetNext(Elem) ((PTIATM_LIST_ENTRY)((Elem)->Link.Flink))
//-------------------------------------------------------------------------
// QueuePushHead -- Macro which puts an element at the head of the queue.
//-------------------------------------------------------------------------
#define QueuePushHead(_L,_E) \
if (!((_E)->Link.Flink = (_L)->Link.Flink)) \
{ \
(_L)->Link.Blink = (PTI_LIST_ENTRY)(_E); \
} \
(_L)->Link.Flink = (PTI_LIST_ENTRY)(_E);
//-------------------------------------------------------------------------
// QueueRemoveHead -- Macro which removes the head of the head of queue.
//-------------------------------------------------------------------------
#define QueueRemoveHead(_L) \
{ \
PTIATM_LIST_ENTRY ListElem; \
if (ListElem = (PTIATM_LIST_ENTRY)(_L)->Link.Flink) \
{ \
if(!((_L)->Link.Flink = ListElem->Link.Flink)) \
(_L)->Link.Blink = (PTI_LIST_ENTRY) 0; \
} \
}
//-------------------------------------------------------------------------
// QueuePutTail -- Macro which puts an element at the tail (end) of the queue.
//-------------------------------------------------------------------------
#define QueuePutTail(_L,_E) \
{ \
if ((_L)->Link.Blink) \
{ \
((PTIATM_LIST_ENTRY)(_L)->Link.Blink)->Link.Flink = (PTI_LIST_ENTRY)(_E); \
(_L)->Link.Blink = (PTI_LIST_ENTRY)(_E); \
} \
else \
{ \
(_L)->Link.Flink = \
(_L)->Link.Blink = (PTI_LIST_ENTRY)(_E); \
} \
(_E)->Link.Flink = (PTI_LIST_ENTRY)0; \
}
//-------------------------------------------------------------------------
// QueuePutTailWithLock -- Macro which puts an element at the tail (end) of
// the queue, using spin lock.
//-------------------------------------------------------------------------
#define QueuePutTailWithLock(_L,_E, pLock) \
{ \
osAcquireSpinLock(pLock); \
if ((_L)->Link.Blink) \
{ \
((PTIATM_LIST_ENTRY)(_L)->Link.Blink)->Link.Flink = (PTI_LIST_ENTRY)(_E); \
(_L)->Link.Blink = (PTI_LIST_ENTRY)(_E); \
} \
else \
{ \
(_L)->Link.Flink = \
(_L)->Link.Blink = (PTI_LIST_ENTRY)(_E); \
} \
(_E)->Link.Flink = (PTI_LIST_ENTRY)0; \
osReleaseSpinLock(pLock); \
}
//-------------------------------------------------------------------------
// QueueGetTail -- Macro which returns the tail of the queue, but does not
// remove the tail from the queue.
//-------------------------------------------------------------------------
#define QueueGetTail(_L) ((PTIATM_LIST_ENTRY)((_L)->Link.Blink))
//-------------------------------------------------------------------------
// QueuePopHead -- Macro which will pop the head off of a queue (list), and
// return it (this differs only from queueremovehead only in
// the 1st line)
//-------------------------------------------------------------------------
#define QueuePopHead(_L) \
(PTIATM_LIST_ENTRY) (_L)->Link.Flink; QueueRemoveHead(_L);
#define QueueRemoveTail(_L) \
{ \
PTIATM_LIST_ENTRY ListElem; \
ListElem = (PTIATM_LIST_ENTRY)(_L)->Link.Flink; \
if(ListElem == (PTIATM_LIST_ENTRY)(_L)->Link.Blink) \
{ \
(_L)->Link.Flink = (_L)->Link.Blink = (PTI_LIST_ENTRY) 0; \
} \
else \
{ \
while(ListElem->Link.Flink != (PTI_LIST_ENTRY)(_L)->Link.Blink) \
{ \
ListElem = (PTIATM_LIST_ENTRY)ListElem->Link.Flink; \
} \
(_L)->Link.Blink = (PTI_LIST_ENTRY) ListElem; \
ListElem->Link.Flink = (PTI_LIST_ENTRY)0; \
} \
}
#define QueueRemoveItem(_L, Elem) \
{ \
PTIATM_LIST_ENTRY ListElem; \
ListElem = (PTIATM_LIST_ENTRY)(_L)->Link.Flink; \
if(ListElem == Elem) \
{ \
QueueRemoveHead(_L); \
} \
else \
{ \
while(ListElem) \
{ \
if(Elem == (PTIATM_LIST_ENTRY)ListElem->Link.Flink) \
{ \
ListElem->Link.Flink = ((PTIATM_LIST_ENTRY)Elem)->Link.Flink; \
if(Elem == (PTIATM_LIST_ENTRY)(_L)->Link.Blink) \
(_L)->Link.Blink = (PTI_LIST_ENTRY) 0; \
break; \
} \
ListElem = (PTIATM_LIST_ENTRY)ListElem->Link.Flink; \
}\
} \
((PTIATM_LIST_ENTRY)Elem)->Link.Flink = (PTI_LIST_ENTRY) 0; \
}
#define QueuePopTail(_L) \
((PTIATM_LIST_ENTRY)((_L)->Link.Blink)); QueueRemoveTail(_L);
#endif