1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (C) 2021 by Andreas Fritiofson <andreas.fritiofson@gmail.com> */
5 * Simple example of using a circular doubly linked list through list.h
7 * gcc -I ../src/ list_example.c -o list_example
13 #include <helper/list.h>
15 static LIST_HEAD(threads
);
23 void insert(struct thread
*t
)
25 list_add_tail(&t
->lh
, &threads
);
28 void remove(struct thread
*t
)
33 struct thread
*lookup_id(int id
)
36 list_for_each_entry(t
, &threads
, lh
) {
43 struct thread
*lookup_tcb(uint64_t addr
)
46 list_for_each_entry(t
, &threads
, lh
) {
47 if (t
->tcb_address
== addr
)
55 struct thread t1
= { .id
= 1, .tcb_address
= 111111111 };
56 struct thread t2
= { .id
= 2, .tcb_address
= 222222222 };
57 struct thread t3
= { .id
= 3, .tcb_address
= 333333333 };
61 assert(lookup_id(1) == &t1
);
62 assert(lookup_tcb(111111111) == &t1
);
63 assert(lookup_id(2) == &t2
);
64 assert(lookup_id(42) == NULL
);
66 assert(lookup_id(1) == NULL
);
69 assert(lookup_id(3) == &t3
);
70 assert(lookup_tcb(333333333) == &t3
);
71 assert(lookup_id(2) == NULL
);
73 assert(list_empty(&threads
));