1 /* Test pth_select call (this also tests pth_poll, implicitly).
2 * Copyright (C) 2001 Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_select.c,v 1.4 2003/02/05 22:13:33 rich Exp $
34 #ifdef HAVE_SYS_TIME_H
42 #include "pthr_reactor.h"
43 #include "pthr_pseudothread.h"
48 static pool reader_pool;
49 static pseudothread reader_pth;
50 static pool writer_pool[NR_WRITERS];
51 static pseudothread writer_pth[NR_WRITERS];
53 static int fds[NR_WRITERS][2];
59 int i, fd = fds[id][1];
60 char c[1] = { '0' + id };
62 for (i = 0; i < NR_CHARS; ++i)
76 int i, running = NR_WRITERS, r, max_fd = -1;
77 fd_set readfds, returnfds;
82 for (i = 0; i < NR_WRITERS; ++i)
86 if (fd > max_fd) max_fd = fd;
87 FD_SET (fd, &readfds);
95 r = pth_select (max_fd+1, &returnfds, 0, 0, &tv);
97 if (r == -1) abort ();
100 for (i = 0; i <= max_fd; ++i)
102 if (FD_ISSET (i, &returnfds))
114 putchar (c[0]); putchar ('\r'); fflush (stdout);
127 for (i = 0; i < NR_WRITERS; ++i)
129 if (pipe (fds[i]) == -1) abort ();
131 if (fcntl (fds[i][0], F_SETFL, O_NONBLOCK) == -1) abort ();
132 if (fcntl (fds[i][1], F_SETFL, O_NONBLOCK) == -1) abort ();
134 writer_pool[i] = new_subpool (global_pool);
135 writer_pth[i] = new_pseudothread (writer_pool[i], writer, &i,
137 pth_start (writer_pth[i]);
140 reader_pool = new_subpool (global_pool);
141 reader_pth = new_pseudothread (reader_pool, reader, 0, "reader");
142 pth_start (reader_pth);
144 while (pseudothread_count_threads () > 0)