librfn
An ad-hoc utility library
protothreadstest.c
Go to the documentation of this file.
1 /*
2  * protothreadstest.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2013 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #undef NDEBUG
15 
16 #include <assert.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 #include <librfn.h>
21 
22 /* The empty thread should return PT_EXITED every time. */
24 {
25  PT_BEGIN(pt);
26  PT_END();
27 }
28 
29 /* The yield thread should return PT_YIELDED, then PT_EXITED. */
31 {
32  PT_BEGIN(pt);
33  PT_YIELD();
34  PT_END();
35 }
36 
37 /* The wait thread should cycle through PT_WAITING, PT_WAITING, PT_EXITED. */
39 {
40  static int count = 0;
41 
42  PT_BEGIN(pt);
43  PT_WAIT_UNTIL(count++ >= 1);
44  assert(count >= 1);
45  PT_WAIT_UNTIL(--count < 1);
46  assert(count == 0);
47  PT_END();
48 }
49 
50 /* The exit through should return PT_YIELDED then PT_EXITED */
52 {
53  PT_BEGIN(pt);
54 
55  while (1) {
56  PT_YIELD();
57  PT_EXIT();
58  }
59 
60  PT_WAIT_UNTIL(false);
61  PT_END();
62 }
63 
64 /* The spawn thread runs though all the other threads so it should
65  * return:
66  * Nothing due to empty_thread
67  * PT_YIELDED due to yield_thread
68  * PT_WAITING, PT_WAITING due to wait_thread
69  * PT_YIELDED due to exit_thread
70  * PT_EXITED as it completes.
71  */
73 {
74  static pt_t child;
75 
76  PT_BEGIN(pt);
77 
78  PT_SPAWN(&child, empty_thread(&child));
79  PT_SPAWN(&child, yield_thread(&child));
80  PT_SPAWN(&child, wait_thread(&child));
81  PT_SPAWN(&child, exit_thread(&child));
82 
83  PT_END();
84 }
85 
86 int main()
87 {
88  pt_t t;
89 
90  PT_INIT(&t);
92 
93  PT_INIT(&t);
95 
96  PT_INIT(&t);
99 
100  PT_INIT(&t);
101  verify(PT_WAITING == wait_thread(&t));
102  verify(PT_WAITING == wait_thread(&t));
103  verify(PT_EXITED == wait_thread(&t));
104 
105  PT_INIT(&t);
106  verify(PT_YIELDED == exit_thread(&t));
107  verify(PT_EXITED == exit_thread(&t));
108 
109 #if 0
110  PT_INIT(&t);
111  t = -1;
112  verify(PT_CRASHED == exit_thread(&t));
113 #endif
114 
115  PT_INIT(&t);
120  verify(PT_EXITED == spawn_thread(&t));
121 
122  return 0;
123 }
#define PT_EXIT()
Definition: protothreads.h:132
#define PT_SPAWN(child, thread)
Call a child thread.
Definition: protothreads.h:149
#define PT_WAIT_UNTIL(c)
Definition: protothreads.h:116
#define PT_BEGIN(pt)
Definition: protothreads.h:93
#define PT_YIELD()
Definition: protothreads.h:124
uint16_t pt_t
Definition: protothreads.h:86
#define PT_END()
Definition: protothreads.h:100
pt_state_t empty_thread(pt_t *pt)
pt_state_t wait_thread(pt_t *pt)
#define verify(x)
Definition: util.h:55
pt_state_t exit_thread(pt_t *pt)
pt_state_t
Definition: protothreads.h:80
pt_state_t spawn_thread(pt_t *pt)
int main()
pt_state_t yield_thread(pt_t *pt)
#define PT_INIT(pt)
Definition: protothreads.h:88