-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sfio.c
129 lines (112 loc) · 3.59 KB
/
test_sfio.c
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
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <[email protected]>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <[email protected]>.
**
** test_sfio.c: Pth test program (Sfio support)
*/
/* ``No, I'm not going to explain it.
If you can't figure it out, you
didn't want to know anyway...''
-- Larry Wall */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "pth.h"
#if PTH_EXT_SFIO
/* a worker thread */
static void *worker(void *_dummy)
{
char line[1024];
int c;
int i;
for (;;) {
i = 0;
while (!sfeof(sfstdin)) {
c = sfgetc(sfstdin);
if (c == '\n') {
line[i] = '\0';
break;
}
line[i++] = (char)c;
}
sfprintf(sfstderr, "you entered '%s' on sfstdin\n", line);
}
return NULL;
}
/* a useless ticker thread */
static void *ticker(void *_arg)
{
time_t now;
sfprintf(sfstderr, "ticker: start\n");
for (;;) {
pth_sleep(5);
now = time(NULL);
sfprintf(sfstderr, "ticker was woken up on %s", ctime(&now));
}
/* NOTREACHED */
return NULL;
}
int main(int argc, char *argv[])
{
Sfdisc_t *disc;
pth_attr_t a;
pth_t w;
pth_t t;
pth_init();
/* get an Sfio discipline for Pth */
if ((disc = pth_sfiodisc()) == NULL) {
perror("pth_sfioiodc");
exit(1);
}
/* push it onto Sfio's input/output streams */
if (sfdisc(sfstdin, disc) != disc) {
perror("sfdisc");
exit(1);
}
if (sfdisc(sfstdout, disc) != disc) {
perror("sfdisc");
exit(1);
}
sfprintf(sfstderr, "This is TEST_SFIO, a Pth test using Sfio disciplines.\n");
sfprintf(sfstderr, "\n");
sfprintf(sfstderr, "Stdout/Stdin are connected to Sfio streams with a Pth\n");
sfprintf(sfstderr, "discipline on top of the streams in order to use Pth's\n");
sfprintf(sfstderr, "I/O operations. It demonstrates that the process this\n");
sfprintf(sfstderr, "way does not block. Instead only one thread blocks.\n");
sfprintf(sfstderr, "\n");
a = pth_attr_new();
pth_attr_set(a, PTH_ATTR_NAME, "worker");
pth_attr_set(a, PTH_ATTR_JOINABLE, FALSE);
pth_attr_set(a, PTH_ATTR_STACK_SIZE, 16*1024);
w = pth_spawn(a, worker, NULL);
pth_attr_set(a, PTH_ATTR_NAME, "ticker");
t = pth_spawn(a, ticker, NULL);
pth_attr_destroy(a);
pth_exit(NULL);
return 0;
}
#else
int main(int argc, char *argv[])
{
fprintf(stderr, "You have to build Pth with --with-sfio to run this test!\n");
return 0;
}
#endif