main.c
3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2014 René Kijewski <rene.kijewski@fu-berlin.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @ingroup tests
* @{
* @file
* @brief Test for module pipe.
* @author René Kijewski <rene.kijewski@fu-berlin.de>
* @}
*/
#include <stdio.h>
#include <inttypes.h>
#include "thread.h"
#include "pipe.h"
#include "pipe.h"
#define BYTES_TOTAL (26)
static char stacks[2][THREAD_STACKSIZE_MAIN];
static char pipe_bufs[2][6];
static ringbuffer_t rbs[2];
static pipe_t pipes[2];
static void *run_middle(void *arg)
{
(void) arg;
unsigned read_total = 0;
while (read_total < BYTES_TOTAL) {
char buf[4];
unsigned read = pipe_read(&pipes[0], buf, sizeof (buf));
unsigned read_start = read_total;
read_total += read;
printf("Middle read: <%.*s> [%u:%u]\n", read, buf,
read_start, read_total);
unsigned written_total = 0;
while (written_total < read) {
int written = pipe_write(&pipes[1], &buf[written_total],
read - written_total);
written_total += written;
}
}
puts("Middle done.");
return NULL;
}
static void *run_end(void *arg)
{
(void) arg;
unsigned read_total = 0;
while (read_total < BYTES_TOTAL) {
char buf[3];
int read = pipe_read(&pipes[1], buf, sizeof (buf));
unsigned read_start = read_total;
read_total += read;
printf("End read: <%.*s> [%u:%u]\n", read, buf,
read_start, read_total);
}
puts("End done.");
return NULL;
}
static unsigned min(unsigned a, unsigned b)
{
return a < b ? a : b;
}
int main(void)
{
puts("Start.");
for (int i = 0; i < 2; ++i) {
ringbuffer_init(&rbs[i], pipe_bufs[i], sizeof (pipe_bufs[i]));
pipe_init(&pipes[i], &rbs[i], NULL);
}
thread_create(stacks[0], sizeof (stacks[0]),
THREAD_PRIORITY_MAIN,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
run_middle, NULL, "middle");
thread_create(stacks[1], sizeof (stacks[1]),
THREAD_PRIORITY_MAIN,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
run_end, NULL, "end");
unsigned total = 0;
while (total < BYTES_TOTAL) {
char buf[5];
unsigned bytes_cur = min(BYTES_TOTAL - total, sizeof (buf));
for (unsigned i = 0; i < bytes_cur; ++i) {
buf[i] = 'A' + total;
++total;
}
unsigned written_total = 0;
while (written_total < bytes_cur) {
int written = pipe_write(&pipes[0], &buf[written_total],
bytes_cur - written_total);
written_total += written;
}
}
puts("Main done.");
return 0;
}