librfn
An ad-hoc utility library
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
tests
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. */
23
pt_state_t
empty_thread
(
pt_t
*pt)
24
{
25
PT_BEGIN
(pt);
26
PT_END
();
27
}
28
29
/* The yield thread should return PT_YIELDED, then PT_EXITED. */
30
pt_state_t
yield_thread
(
pt_t
*pt)
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. */
38
pt_state_t
wait_thread
(
pt_t
*pt)
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 */
51
pt_state_t
exit_thread
(
pt_t
*pt)
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
*/
72
pt_state_t
spawn_thread
(
pt_t
*pt)
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);
91
verify
(
PT_EXITED
==
empty_thread
(&t));
92
93
PT_INIT
(&t);
94
verify
(
PT_EXITED
==
empty_thread
(&t));
95
96
PT_INIT
(&t);
97
verify
(
PT_YIELDED
==
yield_thread
(&t));
98
verify
(
PT_EXITED
==
yield_thread
(&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);
116
verify
(
PT_YIELDED
==
spawn_thread
(&t));
117
verify
(
PT_WAITING
==
spawn_thread
(&t));
118
verify
(
PT_WAITING
==
spawn_thread
(&t));
119
verify
(
PT_YIELDED
==
spawn_thread
(&t));
120
verify
(
PT_EXITED
==
spawn_thread
(&t));
121
122
return
0;
123
}
PT_EXIT
#define PT_EXIT()
Definition:
protothreads.h:132
PT_YIELDED
Definition:
protothreads.h:81
PT_SPAWN
#define PT_SPAWN(child, thread)
Call a child thread.
Definition:
protothreads.h:149
PT_WAIT_UNTIL
#define PT_WAIT_UNTIL(c)
Definition:
protothreads.h:116
PT_BEGIN
#define PT_BEGIN(pt)
Definition:
protothreads.h:93
PT_YIELD
#define PT_YIELD()
Definition:
protothreads.h:124
pt_t
uint16_t pt_t
Definition:
protothreads.h:86
PT_END
#define PT_END()
Definition:
protothreads.h:100
empty_thread
pt_state_t empty_thread(pt_t *pt)
Definition:
protothreadstest.c:23
wait_thread
pt_state_t wait_thread(pt_t *pt)
Definition:
protothreadstest.c:38
PT_WAITING
Definition:
protothreads.h:82
verify
#define verify(x)
Definition:
util.h:55
exit_thread
pt_state_t exit_thread(pt_t *pt)
Definition:
protothreadstest.c:51
pt_state_t
pt_state_t
Definition:
protothreads.h:80
PT_EXITED
Definition:
protothreads.h:83
spawn_thread
pt_state_t spawn_thread(pt_t *pt)
Definition:
protothreadstest.c:72
main
int main()
Definition:
protothreadstest.c:86
librfn.h
yield_thread
pt_state_t yield_thread(pt_t *pt)
Definition:
protothreadstest.c:30
PT_INIT
#define PT_INIT(pt)
Definition:
protothreads.h:88
Generated on Sat Feb 6 2016 21:25:32 for librfn by
1.8.10