summaryrefslogtreecommitdiff
path: root/minix/lib/libasyn/asyn_wait.c
blob: c167b0c9e133ee0f074ac6437abd7917ebb41b22 (plain)
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
/*	asyn_wait() - wait for asynch operations	Author: Kees J. Bot
 *								7 Jul 1997
 */

#include "asyn.h"
#include <time.h>
#ifdef DEBUG
#include <stdio.h>
#endif

#define TBOUND_MIN	1
#define TBOUND_MAX	16

int asyn_wait(asynchio_t *asyn, int flags, struct timeval *to)
/* Wait for one or more nonblocking operations to return a result. */
{
	int r;
	static struct timeval zero_time;
	struct timeval t;
	static time_t tbound= TBOUND_MIN;

	/* Are there more things to do before we can block? */
	if (asyn->asyn_more > 0) { asyn->asyn_more= 0; return 0; }

	if (flags & ASYN_NONBLOCK) {
		/* Don't block by using a zero second timeout. */
		to= &zero_time;
	} else
	if (to != nil) {
		/* asyn_wait() uses an absolute time. */
		if (to->tv_usec >= 1000000L) {
			to->tv_sec+= to->tv_usec / 1000000L;
			to->tv_usec%= 1000000L;
		}
		(void) gettimeofday(&t, nil);
		if (t.tv_sec > to->tv_sec || (t.tv_sec == to->tv_sec
						&& t.tv_usec >= to->tv_usec)) {
			to= &zero_time;
		} else {
			t.tv_sec= to->tv_sec - t.tv_sec;
			t.tv_usec= to->tv_usec - t.tv_usec;
			if (t.tv_usec < 0) {
				t.tv_sec--;
				t.tv_usec+= 1000000L;
			}
			to= &t;
		}

		/* Don't sleep too long, we don't trust select(). */
		if (to->tv_sec > tbound) goto bound;
	} else {
	bound:
		/* No timeout?  Don't hang in (buggy?) select() forever. */
		to= &t;
		t.tv_sec= tbound;
		t.tv_usec= 0;
	}

#ifdef DEBUG
	{
		int op;

		fprintf(stderr, "select: ");
		for (op= 0; op < SEL_NR; op++) {
			fd_set *fdsetp= &asyn->asyn_fdset[op];
			int fd;

			for (fd= 0; fd < FD_SETSIZE; fd++) {
				if (FD_ISSET(fd, fdsetp)) {
					asyn->asyn_afd[fd].afd_state[op]=
								PENDING;
					fprintf(stderr, "%d%c", fd, "rwx"[op]);
				}
			}
		}
		fflush(stderr);
	}
#endif
	r= select(FD_SETSIZE, &asyn->asyn_fdset[SEL_READ],
				&asyn->asyn_fdset[SEL_WRITE],
				&asyn->asyn_fdset[SEL_EXCEPT], to);
#ifdef DEBUG
	fprintf(stderr, " (%d) ", r);
#endif
	if (r > 0) {
		/* An event occurred on one or more file descriptors. */
		int op;

		for (op= 0; op < SEL_NR; op++) {
			fd_set *fdsetp= &asyn->asyn_fdset[op];
			int fd;

			for (fd= 0; fd < FD_SETSIZE; fd++) {
				if (FD_ISSET(fd, fdsetp)) {
					asyn->asyn_afd[fd].afd_state[op]=
								PENDING;
#ifdef DEBUG
					fprintf(stderr, "%d%c", fd, "rwx"[op]);
#endif
				}
			}
		}
		tbound= TBOUND_MIN;
	} else
	if (r == 0) {
		/* If nothing happened then let the time boundary slip a bit. */
		if (tbound < TBOUND_MAX) tbound <<= 1;
	}
#ifdef DEBUG
	fputc('\n', stderr);
#endif

	FD_ZERO(&asyn->asyn_fdset[SEL_READ]);
	FD_ZERO(&asyn->asyn_fdset[SEL_WRITE]);
	FD_ZERO(&asyn->asyn_fdset[SEL_EXCEPT]);

	return r == 0 ? (errno= EINTR, -1) : r;
}