target/adi_v5_swd: move setting of do_reconnect one level up
[openocd.git] / contrib / list_example.c
blob4fcfcdf348a8f30b5f0c91ab4835b154314d89af
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (C) 2021 by Andreas Fritiofson <andreas.fritiofson@gmail.com> */
4 /*
5 * Simple example of using a circular doubly linked list through list.h
7 * gcc -I ../src/ list_example.c -o list_example
8 */
10 #include <stdint.h>
11 #include <stdbool.h>
12 #include <assert.h>
13 #include <helper/list.h>
15 static LIST_HEAD(threads);
17 struct thread {
18 int id;
19 uint64_t tcb_address;
20 struct list_head lh;
23 void insert(struct thread *t)
25 list_add_tail(&t->lh, &threads);
28 void remove(struct thread *t)
30 list_del(&t->lh);
33 struct thread *lookup_id(int id)
35 struct thread *t;
36 list_for_each_entry(t, &threads, lh) {
37 if (t->id == id)
38 return t;
40 return NULL;
43 struct thread *lookup_tcb(uint64_t addr)
45 struct thread *t;
46 list_for_each_entry(t, &threads, lh) {
47 if (t->tcb_address == addr)
48 return t;
50 return NULL;
53 int main(void)
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 };
59 insert(&t1);
60 insert(&t2);
61 assert(lookup_id(1) == &t1);
62 assert(lookup_tcb(111111111) == &t1);
63 assert(lookup_id(2) == &t2);
64 assert(lookup_id(42) == NULL);
65 remove(&t1);
66 assert(lookup_id(1) == NULL);
67 insert(&t3);
68 remove(&t2);
69 assert(lookup_id(3) == &t3);
70 assert(lookup_tcb(333333333) == &t3);
71 assert(lookup_id(2) == NULL);
72 remove(&t3);
73 assert(list_empty(&threads));