File: select.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (326 lines) | stat: -rw-r--r-- 7,489 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (c) 1997-1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

#include <oskit/time.h>
#include <oskit/com/listener.h>
#include <oskit/io/asyncio.h>
#include <oskit/dev/dev.h>
#include <oskit/c/environment.h>

#if VERBOSITY > 2
#include <stdio.h>
#endif
#include <string.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/types.h>
#include "posix.h"
#include "fd.h"

static oskit_error_t	callback(oskit_iunknown_t *ioobj, void *arg);

#define VERBOSITY	0
#if VERBOSITY > 20
#define DMARK printf(__FILE__ ":%d: " __FUNCTION__ "\n", __LINE__)
#else
#define DMARK ((void)0)         /* no-op */
#endif

/*
 * Single and Multi threaded select combined.
 */
int 
select(int n, fd_set *in, fd_set *out, fd_set *exc, struct timeval *tout)
{
	int		fd, first, state, count;
	fd_set		inr, outr, excr;
	fd_set		locks;
	osenv_sleeprec_t sleeprec;
	oskit_timespec_t timeout = {0, 0};
#ifdef THREAD_SAFE
	int		gotlocks;
#endif

	count    = 0;
	first    = 1;
	FD_ZERO(&locks);
	FD_ZERO(&inr);
	FD_ZERO(&outr);
	FD_ZERO(&excr);
#ifdef THREAD_SAFE
	gotlocks = 1;

	/*
	 * Try and lock all the descriptor locks. Don't want to deadlock,
	 * so if some are already locked, the select returns 0 so the
	 * caller can try again.
	 */
	for (fd = 0; fd < n; fd++) {
		if (in && FD_ISSET(fd, in)) {
			if (FD_BAD(fd))
				goto badf;
			if (!fd_trylock(fd) ||
			    !FD_ACCESS_TRYLOCK(fd, FD_RDWR)) {
				gotlocks = 0;
				break;
			}
			FD_SET(fd, &locks);
		}
		else if (out && FD_ISSET(fd, out)) {
			if (FD_BAD(fd))
				goto badf;
			if (!fd_trylock(fd) ||
			    !FD_ACCESS_TRYLOCK(fd, FD_RDWR)) {
				break;
			}
			FD_SET(fd, &locks);
		}
		else if (exc && FD_ISSET(fd, exc)) {
			if (FD_BAD(fd))
				goto badf;
			if (!fd_trylock(fd) ||
			    !FD_ACCESS_TRYLOCK(fd, FD_RDWR)) {
				gotlocks = 0;
				break;
			}
			FD_SET(fd, &locks);
		}
	}
	if (!gotlocks) {
		for (fd = 0; fd < n; fd++) {
			if (FD_ISSET(fd, &locks))
				FD_ACCESS_UNLOCK(fd, FD_RDWR);
		}
		return 0;
	}
#else
	/*
	 * Just verify the descriptors. Use the same lock set though since
	 * its convenient.
	 */
	for (fd = 0; fd < n; fd++) {
		if (in && FD_ISSET(fd, in)) {
			if (FD_BAD(fd))
				goto badf;
			FD_SET(fd, &locks);
		}
		else if (out && FD_ISSET(fd, out)) {
			if (FD_BAD(fd))
				goto badf;
			FD_SET(fd, &locks);
		}
		else if (exc && FD_ISSET(fd, exc)) {
			if (FD_BAD(fd))
				goto badf;
			FD_SET(fd, &locks);
		}
	}
#endif
  retry:
	/*
	 * Check descriptors to see if anything is ready.
	 */
	for (fd = 0; fd < n; fd++) {
		fd_t	*fdp  = fd_array + fd;	/* this fd */
		int	ready = 0;

		/*
		 * See if we care about this descriptor.
		 */
		if (! FD_ISSET(fd, &locks))
			continue;

		fdp->selecting_on = 0;	/* Flag for creating listener */
		
		/* 
		 * if the fd doesn't support the interface, 
		 * we just ignore it
		 */
		if (!FD_HAS_INTERFACE(fd, asyncio)) 
			continue;

		/*
		 * Get the state and see if there is something ready
		 * that this select was interested in.
		 */
		state = oskit_asyncio_poll(fdp->asyncio);

		if (in && FD_ISSET(fd, in)) {
			if (state & OSKIT_ASYNCIO_READABLE) {
				FD_SET(fd, &inr);
				ready = 1;
			}
			fdp->selecting_on |= OSKIT_ASYNCIO_READABLE;
		}

		if (out && FD_ISSET(fd, out)) {
			if (state & OSKIT_ASYNCIO_WRITABLE) {
				FD_SET(fd, &outr);
				ready = 1;
			}
			fdp->selecting_on |= OSKIT_ASYNCIO_WRITABLE;
		}

		if (exc && FD_ISSET(fd, exc)) {
			if (state & OSKIT_ASYNCIO_EXCEPTION) {
				FD_SET(fd, &excr);
				ready = 1;
			}
			fdp->selecting_on |= OSKIT_ASYNCIO_EXCEPTION;
		}
		/* count each descriptor only once! */
		if (ready)
			count++;
	}

	/* Either got something, nothing ready after wakeup, or zero timeout? */
	if (count || !first || (tout && !tout->tv_sec && !tout->tv_usec))
		goto done;

	/*
	 * Nothing. Need to create the listeners. Initialize the sleeprec
	 * first to avoid premature and spurious wakeups.
	 */
	oskit_libcenv_sleep_init(libc_environment, &sleeprec);
	
	for (fd = 0; fd < n; fd++) {
		fd_t		  *fdp  = fd_array + fd;	/* this fd */
		
		/*
		 * See if we care about this descriptor.
		 */
		if (! FD_ISSET(fd, &locks))
			continue;

		/*
		 * Create the listener for this fd if we actually care
		 * about it. Use proper flags!
		 *
		 * Note that we reuse the listener if its there.
		 */
		if (fdp->selecting_on) {
			/*
			 * Stash the sleeprec in the fdp so the callback
			 * can do the wakeup.
			 */
			fdp->select_handle = &sleeprec;
			
			if (! fdp->listener)
				fdp->listener =
					oskit_create_listener(callback,
							      (void *) fd);
			
			state = oskit_asyncio_add_listener(fdp->asyncio,
							   fdp->listener,
							   fdp->selecting_on);

			/* Guard against lost wakeup. */
			if (state & fdp->selecting_on)
				count++;
		}
	}
	first = 0;

	/*
	 * if (un)lucky enough for something to come in while setting up
	 * the listeners, go back and see what actually happened.
	 */
	if (count) {
		count = 0;
		goto retry;
	}

	/*
	 * Okay, convert the timeout to a timespec and sleep. If the sleep
	 * returns with a timeout, then nothing came in and we can finish up.
	 */
	if (tout) {
		TIMEVAL_TO_TIMESPEC(tout, &timeout);
	}

	if (oskit_libcenv_sleep(libc_environment, &sleeprec, &timeout)
	    != ETIMEDOUT)
		goto retry;

   done:
	/* Cleanup descriptors to avoid spurious calls to wakeup. */
	if (!first) {
		for (fd = 0; fd < n; fd++) {
			fd_t	*fdp  = fd_array + fd;
			
			if (FD_ISSET(fd, &locks) && fdp->asyncio)
				oskit_asyncio_remove_listener(fdp->asyncio,
							      fdp->listener);
		}
	}
	
	/* copy out results */
	if (in) 
		FD_COPY(&inr, in);
	if (out) 
		FD_COPY(&outr, out);
	if (exc) 
		FD_COPY(&excr, exc);
#ifdef THREAD_SAFE
	/* unlock the descriptors */
	for (fd = 0; fd < n; fd++) {
		if (FD_ISSET(fd, &locks))
			FD_ACCESS_UNLOCK(fd, FD_RDWR);
	}
#endif
	return count;
		
   badf:
	for (fd = 0; fd < n; fd++) {
		if (FD_ISSET(fd, &locks))
			FD_ACCESS_UNLOCK(fd, FD_RDWR);
	}
	errno = EBADF;
	return -1;
}

static oskit_error_t
callback(oskit_iunknown_t *ioobj, void *arg)
{
	oskit_asyncio_t		*as   = (void *)ioobj;
	int			fd    = (int) arg;
	fd_t			*fdp  = fd_array + fd;
	oskit_error_t		mask;

	mask = oskit_asyncio_poll(as);
#if VERBOSITY > 2
	if (mask < 0) {
		printf("fd %d, poll on %p failed: %s\n", 
			fd, as, strerror(mask));
	} else {
		printf("fd %d is ready, mask = 0x%x ", fd, mask);
		if (mask & OSKIT_ASYNCIO_READABLE)
			printf(" READ ");
		if (mask & OSKIT_ASYNCIO_WRITABLE)
			printf(" WRITE ");
		if (mask & OSKIT_ASYNCIO_EXCEPTION)
			printf(" EXCEPTION ");
		printf("\n");
	}
#endif
	if (fdp->selecting_on & mask) {
		oskit_libcenv_wakeup(libc_environment,
				     (osenv_sleeprec_t *) fdp->select_handle);
	}

	return 0;
}