1 /*
2 * Copyright (c) 2017, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * ======== QueueP_zephyr.c ========
34 */
35
36 #include <ti/drivers/dpl/HwiP.h>
37 #include "QueueP.h"
38
39 /*
40 * ======== QueueP_init ========
41 */
QueueP_init(QueueP_Obj * obj)42 void QueueP_init(QueueP_Obj *obj)
43 {
44 obj->elem.next = obj->elem.prev = &(obj->elem);
45 }
46
47 /*
48 * ======== QueueP_empty ========
49 */
QueueP_empty(QueueP_Obj * obj)50 bool QueueP_empty(QueueP_Obj *obj)
51 {
52 return (obj->elem.next == &(obj->elem));
53 }
54
55 /*
56 * ======== QueueP_get ========
57 */
QueueP_get(QueueP_Obj * obj)58 uintptr_t QueueP_get(QueueP_Obj *obj)
59 {
60 QueueP_Elem *elem;
61 uintptr_t key;
62
63 key = HwiP_disable();
64
65 elem = obj->elem.next;
66
67 obj->elem.next = elem->next;
68 elem->next->prev = &(obj->elem);
69
70 HwiP_restore(key);
71
72 return ((uintptr_t)elem);
73
74 }
75
76 /*
77 * ======== QueueP_getTail ========
78 */
QueueP_getTail(QueueP_Obj * obj)79 uintptr_t QueueP_getTail(QueueP_Obj *obj)
80 {
81 QueueP_Elem *elem;
82 uintptr_t key;
83
84 key = HwiP_disable();
85
86 elem = obj->elem.prev;
87
88 obj->elem.prev = elem->prev;
89 elem->prev->next = &(obj->elem);
90
91 HwiP_restore(key);
92
93 return ((uintptr_t)elem);
94
95 }
96
97 /*
98 * ======== QueueP_head ========
99 */
QueueP_head(QueueP_Obj * obj)100 uintptr_t QueueP_head(QueueP_Obj *obj)
101 {
102 return ((uintptr_t)(obj->elem.next));
103 }
104
105 /*
106 * ======== elemClear ========
107 */
QueueP_elemClear(QueueP_Elem * qelem)108 void QueueP_elemClear(QueueP_Elem *qelem)
109 {
110 qelem->next = qelem->prev = qelem;
111 }
112
113 /*
114 * ======== insert ========
115 */
QueueP_insert(QueueP_Elem * qelem,QueueP_Elem * elem)116 void QueueP_insert(QueueP_Elem *qelem, QueueP_Elem *elem)
117 {
118 QueueP_put((QueueP_Obj *)qelem, elem);
119 }
120
121 /*
122 * ======== next ========
123 */
QueueP_next(QueueP_Elem * qelem)124 uintptr_t QueueP_next(QueueP_Elem *qelem)
125 {
126 return ((uintptr_t)qelem->next);
127 }
128
129 /*
130 * ======== QueueP_prev ========
131 */
QueueP_prev(QueueP_Elem * qelem)132 uintptr_t QueueP_prev(QueueP_Elem *qelem)
133 {
134 return ((uintptr_t)qelem->prev);
135 }
136
137 /*
138 * ======== QueueP_put ========
139 */
QueueP_put(QueueP_Obj * obj,QueueP_Elem * elem)140 void QueueP_put(QueueP_Obj *obj, QueueP_Elem *elem)
141 {
142 uintptr_t key;
143
144 key = HwiP_disable();
145
146 elem->next = &(obj->elem);
147 elem->prev = obj->elem.prev;
148 obj->elem.prev->next = elem;
149 obj->elem.prev = elem;
150
151 HwiP_restore(key);
152 }
153
154 /*
155 * ======== QueueP_putHead ========
156 */
QueueP_putHead(QueueP_Obj * obj,QueueP_Elem * elem)157 void QueueP_putHead(QueueP_Obj *obj, QueueP_Elem *elem)
158 {
159 uintptr_t key;
160
161 key = HwiP_disable();
162
163 elem->prev = &(obj->elem);
164 elem->next = obj->elem.next;
165 obj->elem.next->prev = elem;
166 obj->elem.next = elem;
167
168 HwiP_restore(key);
169 }
170
171 /*
172 * ======== QueueP_remove ========
173 */
QueueP_remove(QueueP_Elem * qelem)174 void QueueP_remove(QueueP_Elem *qelem)
175 {
176 #if defined(__IAR_SYSTEMS_ICC__)
177 QueueP_Elem *temp;
178 temp = qelem->next;
179 qelem->prev->next = temp;
180 temp = qelem->prev;
181 qelem->next->prev = temp;
182 #else
183 qelem->prev->next = qelem->next;
184 qelem->next->prev = qelem->prev;
185 #endif
186 }
187
188 /*
189 * ======== isQueued ========
190 */
QueueP_isQueued(QueueP_Elem * qelem)191 bool QueueP_isQueued(QueueP_Elem *qelem)
192 {
193 if ((qelem->prev == qelem) && (qelem->next == qelem)) {
194 return (false);
195 }
196 else {
197 return (true);
198 }
199 }
200