RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
rtservice.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2006-03-16 Bernard the first version
9 * 2006-09-07 Bernard move the kservice APIs to rtthread.h
10 * 2007-06-27 Bernard fix the rt_list_remove bug
11 * 2012-03-22 Bernard rename kservice.h to rtservice.h
12 * 2017-11-15 JasonJia Modify rt_slist_foreach to rt_slist_for_each_entry.
13 * Make code cleanup.
14 * 2024-01-03 Shell add rt_slist_pop()
15 */
16
17#ifndef __RT_SERVICE_H__
18#define __RT_SERVICE_H__
19
20#include <rtdef.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
29
31
36#define rt_container_of(ptr, type, member) \
37 ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
38
39
43#define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
44
50rt_inline void rt_list_init(rt_list_t *l)
51{
52 l->next = l->prev = l;
53}
54
62{
63 l->next->prev = n;
64 n->next = l->next;
65
66 l->next = n;
67 n->prev = l;
68}
69
77{
78 l->prev->next = n;
79 n->prev = l->prev;
80
81 l->prev = n;
82 n->next = l;
83}
84
89rt_inline void rt_list_remove(rt_list_t *n)
90{
91 n->next->prev = n->prev;
92 n->prev->next = n->next;
93
94 n->next = n->prev = n;
95}
96
101rt_inline int rt_list_isempty(const rt_list_t *l)
102{
103 return l->next == l;
104}
105
110rt_inline unsigned int rt_list_len(const rt_list_t *l)
111{
112 unsigned int len = 0;
113 const rt_list_t *p = l;
114 while (p->next != l)
115 {
116 p = p->next;
117 len ++;
118 }
119
120 return len;
121}
122
129#define rt_list_entry(node, type, member) \
130 rt_container_of(node, type, member)
131
137#define rt_list_for_each(pos, head) \
138 for (pos = (head)->next; pos != (head); pos = pos->next)
139
146#define rt_list_for_each_safe(pos, n, head) \
147 for (pos = (head)->next, n = pos->next; pos != (head); \
148 pos = n, n = pos->next)
149
156#define rt_list_for_each_entry(pos, head, member) \
157 for (pos = rt_list_entry((head)->next, rt_typeof(*pos), member); \
158 &pos->member != (head); \
159 pos = rt_list_entry(pos->member.next, rt_typeof(*pos), member))
160
168#define rt_list_for_each_entry_safe(pos, n, head, member) \
169 for (pos = rt_list_entry((head)->next, rt_typeof(*pos), member), \
170 n = rt_list_entry(pos->member.next, rt_typeof(*pos), member); \
171 &pos->member != (head); \
172 pos = n, n = rt_list_entry(n->member.next, rt_typeof(*n), member))
173
182#define rt_list_first_entry(ptr, type, member) \
183 rt_list_entry((ptr)->next, type, member)
184
185#define RT_SLIST_OBJECT_INIT(object) { RT_NULL }
186
192rt_inline void rt_slist_init(rt_slist_t *l)
193{
194 l->next = RT_NULL;
195}
196
198{
199 struct rt_slist_node *node;
200
201 node = l;
202 while (node->next) node = node->next;
203
204 /* append the node to the tail */
205 node->next = n;
206 n->next = RT_NULL;
207}
208
210{
211 n->next = l->next;
212 l->next = n;
213}
214
215rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
216{
217 unsigned int len = 0;
218 const rt_slist_t *list = l->next;
219 while (list != RT_NULL)
220 {
221 list = list->next;
222 len ++;
223 }
224
225 return len;
226}
227
229{
230 struct rt_slist_node *node = l;
231
232 /* remove node */
233 node = node->next;
234 if (node != (rt_slist_t *)0)
235 {
236 ((struct rt_slist_node *)l)->next = node->next;
237 }
238
239 return node;
240}
241
243{
244 /* remove slist head */
245 struct rt_slist_node *node = l;
246 while (node->next && node->next != n) node = node->next;
247
248 /* remove node */
249 if (node->next != (rt_slist_t *)0) node->next = node->next->next;
250
251 return l;
252}
253
255{
256 return l->next;
257}
258
260{
261 while (l->next) l = l->next;
262
263 return l;
264}
265
267{
268 return n->next;
269}
270
272{
273 return l->next == RT_NULL;
274}
275
282#define rt_slist_entry(node, type, member) \
283 rt_container_of(node, type, member)
284
290#define rt_slist_for_each(pos, head) \
291 for (pos = (head)->next; pos != RT_NULL; pos = pos->next)
292
299#define rt_slist_for_each_entry(pos, head, member) \
300 for (pos = ((head)->next == (RT_NULL) ? (RT_NULL) : rt_slist_entry((head)->next, rt_typeof(*pos), member)); \
301 pos != (RT_NULL) && &pos->member != (RT_NULL); \
302 pos = (pos->member.next == (RT_NULL) ? (RT_NULL) : rt_slist_entry(pos->member.next, rt_typeof(*pos), member)))
303
312#define rt_slist_first_entry(ptr, type, member) \
313 rt_slist_entry((ptr)->next, type, member)
314
323#define rt_slist_tail_entry(ptr, type, member) \
324 rt_slist_entry(rt_slist_tail(ptr), type, member)
325
327
328#ifdef __cplusplus
329}
330#endif
331
332#endif
rt_inline void rt_list_remove(rt_list_t *n)
remove node from list.
rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)
rt_inline void rt_slist_init(rt_slist_t *l)
initialize a single list
rt_inline rt_slist_t * rt_slist_tail(rt_slist_t *l)
rt_inline rt_slist_t * rt_slist_first(rt_slist_t *l)
rt_inline int rt_list_isempty(const rt_list_t *l)
tests whether a list is empty
rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
insert a node before a list
rt_inline rt_slist_t * rt_slist_pop(rt_slist_t *l)
rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
rt_inline void rt_list_init(rt_list_t *l)
initialize a list
rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
insert a node after a list
rt_inline rt_slist_t * rt_slist_next(rt_slist_t *n)
rt_inline rt_slist_t * rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
rt_inline int rt_slist_isempty(rt_slist_t *l)
rt_inline void rt_slist_append(rt_slist_t *l, rt_slist_t *n)
rt_inline unsigned int rt_list_len(const rt_list_t *l)
get the list length
struct rt_list_node rt_list_t
#define RT_NULL
struct rt_slist_node rt_slist_t
struct rt_list_node * next
struct rt_list_node * prev
struct rt_slist_node * next