1 /*
2 ** GNU Pth - The GNU Portable Threads
3 ** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
4 **
5 ** This file is part of GNU Pth, a non-preemptive thread scheduling
6 ** library which can be found at http://www.gnu.org/software/pth/.
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU Lesser General Public
19 ** License along with this library; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 ** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22 **
23 ** test_select.c: Pth test program (select)
24 */
25 /* ``Most computer problems are located
26 between the keyboard and the chair.'' */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35
36 #include "pth.h"
37
38 /* a useless ticker thread */
ticker(void * _arg)39 static void *ticker(void *_arg)
40 {
41 time_t now;
42 fprintf(stderr, "ticker: start\n");
43 for (;;) {
44 pth_sleep(5);
45 now = time(NULL);
46 fprintf(stderr, "ticker was woken up on %s", ctime(&now));
47 }
48 /* NOTREACHED */
49 return NULL;
50 }
51
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54 pth_event_t evt;
55 pth_t t_ticker;
56 pth_attr_t t_attr;
57 fd_set rfds;
58 char c;
59 int n;
60
61 pth_init();
62
63 fprintf(stderr, "This is TEST_SELECT, a Pth test using select.\n");
64 fprintf(stderr, "\n");
65 fprintf(stderr, "Enter data. Hit CTRL-C to stop this test.\n");
66 fprintf(stderr, "\n");
67
68 t_attr = pth_attr_new();
69 pth_attr_set(t_attr, PTH_ATTR_NAME, "ticker");
70 t_ticker = pth_spawn(t_attr, ticker, NULL);
71 pth_attr_destroy(t_attr);
72 pth_yield(NULL);
73
74 evt = NULL;
75 for (;;) {
76 if (evt == NULL)
77 evt = pth_event(PTH_EVENT_TIME, pth_timeout(10,0));
78 else
79 evt = pth_event(PTH_EVENT_TIME|PTH_MODE_REUSE, evt, pth_timeout(10,0));
80 FD_ZERO(&rfds);
81 FD_SET(STDIN_FILENO, &rfds);
82 n = pth_select_ev(STDIN_FILENO+1, &rfds, NULL, NULL, NULL, evt);
83 if (n == -1 && errno == EINTR) {
84 fprintf(stderr, "main: timeout - repeating\n");
85 continue;
86 }
87 if (!FD_ISSET(STDIN_FILENO, &rfds)) {
88 fprintf(stderr, "main: Hmmmm... strange situation: bit not set\n");
89 exit(1);
90 }
91 fprintf(stderr, "main: select returned %d\n", n);
92 while (pth_read(STDIN_FILENO, &c, 1) > 0)
93 fprintf(stderr, "main: read stdin '%c'\n", c);
94 }
95
96 pth_cancel(t_ticker);
97 pth_join(t_ticker, NULL);
98 pth_event_free(evt, PTH_FREE_THIS);
99 pth_kill();
100 return 0;
101 }
102
103