aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/fdwait.c
blob: d398341973c709d50e728ac3b3c90225c80713cf (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
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
#define NOPLAN9DEFINES
#include <u.h>
#include <libc.h>
#include <thread.h>
#include "threadimpl.h"
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

void
fdwait()
{
	fd_set rfd, wfd, efd;

	FD_ZERO(&rfd);
	FD_ZERO(&wfd);
	FD_ZERO(&efd);
	if(mode=='w')
		FD_SET(&wfd, fd);
	else
		FD_SET(&rfd, fd);
	FD_SET(&efd, fd);
	select(fd+1, &rfd, &wfd, &efd, nil);
}

void
threadfdwaitsetup(void)
{
}

void
_threadfdwait(int fd, int rw, ulong pc)
{
	int i;

	struct {
		Channel c;
		ulong x;
		Alt *qentry[2];
	} s;

	threadfdwaitsetup();
	chaninit(&s.c, sizeof(ulong), 1);
	s.c.qentry = (volatile Alt**)s.qentry;
	s.c.nentry = 2;
	memset(s.qentry, 0, sizeof s.qentry);
	for(i=0; i<npoll; i++)
		if(pfd[i].fd == -1)
			break;
	if(i==npoll){
		if(npoll >= nelem(polls)){
			fprint(2, "Too many polled fds.\n");
			abort();
		}
		npoll++;
	}

	pfd[i].fd = fd;
	pfd[i].events = rw=='r' ? POLLIN : POLLOUT;
	polls[i].c = &s.c;
	if(0) fprint(2, "%s [%3d] fdwait %d %c list *0x%lux\n",
		argv0, threadid(), fd, rw, pc);
	if(pollpid)
		postnote(PNPROC, pollpid, "interrupt");
	recvul(&s.c);
}

void
threadfdwait(int fd, int rw)
{
	_threadfdwait(fd, rw, getcallerpc(&fd));
}

void
threadsleep(int ms)
{
	struct {
		Channel c;
		ulong x;
		Alt *qentry[2];
	} s;

	threadfdwaitsetup();
	chaninit(&s.c, sizeof(ulong), 1);
	s.c.qentry = (volatile Alt**)s.qentry;
	s.c.nentry = 2;
	memset(s.qentry, 0, sizeof s.qentry);
	
	sleepchan[nsleep] = &s.c;
	sleeptime[nsleep++] = p9nsec()/1000000+ms;
	recvul(&s.c);
}

void
threadfdnoblock(int fd)
{
	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0)|O_NONBLOCK);
}

long
threadread(int fd, void *a, long n)
{
	int nn;

	threadfdnoblock(fd);
again:
	/*
	 * Always call wait (i.e. don't optimistically try the read)
	 * so that the scheduler gets a chance to run other threads.
	 */
	_threadfdwait(fd, 'r', getcallerpc(&fd));
	errno = 0;
	nn = read(fd, a, n);
	if(nn <= 0){
		if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
			goto again;
	}
	return nn;
}

int
threadrecvfd(int fd)
{
	int nn;

	threadfdnoblock(fd);
again:
	/*
	 * Always call wait (i.e. don't optimistically try the recvfd)
	 * so that the scheduler gets a chance to run other threads.
	 */
	_threadfdwait(fd, 'r', getcallerpc(&fd));
	nn = recvfd(fd);
	if(nn < 0){
		if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
			goto again;
	}
	return nn;
}

int
threadsendfd(int fd, int sfd)
{
	int nn;

	threadfdnoblock(fd);
again:
	/*
	 * Always call wait (i.e. don't optimistically try the sendfd)
	 * so that the scheduler gets a chance to run other threads.
	 */
	_threadfdwait(fd, 'w', getcallerpc(&fd));
	nn = sendfd(fd, sfd);
	if(nn < 0){
		if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
			goto again;
	}
	return nn;
}

long
threadreadn(int fd, void *a, long n)
{
	int tot, nn;

	for(tot = 0; tot<n; tot+=nn){
		nn = threadread(fd, (char*)a+tot, n-tot);
		if(nn <= 0){
			if(tot == 0)
				return nn;
			return tot;
		}
	}
	return tot;
}

long
_threadwrite(int fd, const void *a, long n)
{
	int nn;

	threadfdnoblock(fd);
again:
	/*
	 * Always call wait (i.e. don't optimistically try the write)
	 * so that the scheduler gets a chance to run other threads.
	 */
	_threadfdwait(fd, 'w', getcallerpc(&fd));
	nn = write(fd, a, n);
	if(nn < 0){
		if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
			goto again;
	}
	return nn;
}

long
threadwrite(int fd, const void *a, long n)
{
	int tot, nn;

	for(tot = 0; tot<n; tot+=nn){
		nn = _threadwrite(fd, (char*)a+tot, n-tot);
		if(nn <= 0){
			if(tot == 0)
				return nn;
			return tot;
		}
	}
	return tot;
}

int
threadannounce(char *addr, char *dir)
{
	return p9announce(addr, dir);
}

int
threadlisten(char *dir, char *newdir)
{
	int fd, ret;
	extern int _p9netfd(char*);

	fd = _p9netfd(dir);
	if(fd < 0){
		werrstr("bad 'directory' in listen: %s", dir);
		return -1;
	}
	threadfdnoblock(fd);
	while((ret = p9listen(dir, newdir)) < 0 && errno==EAGAIN)
		_threadfdwait(fd, 'r', getcallerpc(&dir));
	return ret;
}

int
threadaccept(int cfd, char *dir)
{
	return p9accept(cfd, dir);
}