2013-07-16 08:54:42 +02:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// +build darwin dragonfly freebsd netbsd openbsd
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
#include "runtime.h"
|
2013-11-14 21:15:04 +01:00
|
|
|
#include "defs.h"
|
|
|
|
#include "malloc.h"
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
// Integrated network poller (kqueue-based implementation).
|
|
|
|
|
|
|
|
int32 runtime_kqueue(void);
|
|
|
|
int32 runtime_kevent(int32, Kevent*, int32, Kevent*, int32, Timespec*);
|
|
|
|
void runtime_closeonexec(int32);
|
|
|
|
|
|
|
|
static int32 kq = -1;
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_netpollinit(void)
|
|
|
|
{
|
|
|
|
kq = runtime_kqueue();
|
|
|
|
if(kq < 0) {
|
|
|
|
runtime_printf("netpollinit: kqueue failed with %d\n", -kq);
|
|
|
|
runtime_throw("netpollinit: kqueue failed");
|
|
|
|
}
|
|
|
|
runtime_closeonexec(kq);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_netpollopen(uintptr fd, PollDesc *pd)
|
2013-07-16 08:54:42 +02:00
|
|
|
{
|
|
|
|
Kevent ev[2];
|
|
|
|
int32 n;
|
|
|
|
|
|
|
|
// Arm both EVFILT_READ and EVFILT_WRITE in edge-triggered mode (EV_CLEAR)
|
|
|
|
// for the whole fd lifetime. The notifications are automatically unregistered
|
|
|
|
// when fd is closed.
|
2013-11-06 20:49:01 +01:00
|
|
|
ev[0].ident = (uint32)fd;
|
2013-07-16 08:54:42 +02:00
|
|
|
ev[0].filter = EVFILT_READ;
|
2013-11-06 20:49:01 +01:00
|
|
|
ev[0].flags = EV_ADD|EV_CLEAR;
|
2013-07-16 08:54:42 +02:00
|
|
|
ev[0].fflags = 0;
|
|
|
|
ev[0].data = 0;
|
2013-11-06 20:49:01 +01:00
|
|
|
ev[0].udata = (kevent_udata)pd;
|
2013-07-16 08:54:42 +02:00
|
|
|
ev[1] = ev[0];
|
|
|
|
ev[1].filter = EVFILT_WRITE;
|
2013-11-06 20:49:01 +01:00
|
|
|
n = runtime_kevent(kq, ev, 2, nil, 0, nil);
|
2013-07-16 08:54:42 +02:00
|
|
|
if(n < 0)
|
|
|
|
return -n;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_netpollclose(uintptr fd)
|
2013-07-16 08:54:42 +02:00
|
|
|
{
|
|
|
|
// Don't need to unregister because calling close()
|
|
|
|
// on fd will remove any kevents that reference the descriptor.
|
|
|
|
USED(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
void
|
2014-07-19 10:53:52 +02:00
|
|
|
runtime_netpollarm(PollDesc* pd, int32 mode)
|
2014-06-07 00:37:27 +02:00
|
|
|
{
|
2014-07-19 10:53:52 +02:00
|
|
|
USED(pd, mode);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_throw("unused");
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Polls for ready network connections.
|
|
|
|
// Returns list of goroutines that become runnable.
|
|
|
|
G*
|
|
|
|
runtime_netpoll(bool block)
|
|
|
|
{
|
|
|
|
static int32 lasterr;
|
|
|
|
Kevent events[64], *ev;
|
|
|
|
Timespec ts, *tp;
|
2013-11-06 20:49:01 +01:00
|
|
|
int32 n, i, mode;
|
2013-07-16 08:54:42 +02:00
|
|
|
G *gp;
|
|
|
|
|
|
|
|
if(kq == -1)
|
|
|
|
return nil;
|
|
|
|
tp = nil;
|
|
|
|
if(!block) {
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
tp = &ts;
|
|
|
|
}
|
|
|
|
gp = nil;
|
|
|
|
retry:
|
|
|
|
n = runtime_kevent(kq, nil, 0, events, nelem(events), tp);
|
|
|
|
if(n < 0) {
|
|
|
|
if(n != -EINTR && n != lasterr) {
|
|
|
|
lasterr = n;
|
|
|
|
runtime_printf("runtime: kevent on fd %d failed with %d\n", kq, -n);
|
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
for(i = 0; i < n; i++) {
|
|
|
|
ev = &events[i];
|
2013-11-06 20:49:01 +01:00
|
|
|
mode = 0;
|
2013-07-16 08:54:42 +02:00
|
|
|
if(ev->filter == EVFILT_READ)
|
2013-11-06 20:49:01 +01:00
|
|
|
mode += 'r';
|
2013-07-16 08:54:42 +02:00
|
|
|
if(ev->filter == EVFILT_WRITE)
|
2013-11-06 20:49:01 +01:00
|
|
|
mode += 'w';
|
|
|
|
if(mode)
|
|
|
|
runtime_netpollready(&gp, (PollDesc*)ev->udata, mode);
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
if(block && gp == nil)
|
|
|
|
goto retry;
|
|
|
|
return gp;
|
|
|
|
}
|
2013-11-14 21:15:04 +01:00
|
|
|
|
|
|
|
void
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_netpoll_scan(struct Workbuf** wbufp, void (*enqueue1)(struct Workbuf**, Obj))
|
2013-11-14 21:15:04 +01:00
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
USED(wbufp);
|
|
|
|
USED(enqueue1);
|
2013-11-14 21:15:04 +01:00
|
|
|
}
|