github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/netpoll_kqueue.c (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd netbsd openbsd
     6  
     7  #include "runtime.h"
     8  #include "defs_GOOS_GOARCH.h"
     9  #include "os_GOOS.h"
    10  
    11  // Integrated network poller (kqueue-based implementation).
    12  
    13  int32	runtime·kqueue(void);
    14  int32	runtime·kevent(int32, Kevent*, int32, Kevent*, int32, Timespec*);
    15  void	runtime·closeonexec(int32);
    16  
    17  static int32 kq = -1;
    18  
    19  void
    20  runtime·netpollinit(void)
    21  {
    22  	kq = runtime·kqueue();
    23  	if(kq < 0) {
    24  		runtime·printf("netpollinit: kqueue failed with %d\n", -kq);
    25  		runtime·throw("netpollinit: kqueue failed");
    26  	}
    27  	runtime·closeonexec(kq);
    28  }
    29  
    30  int32
    31  runtime·netpollopen(uintptr fd, PollDesc *pd)
    32  {
    33  	Kevent ev[2];
    34  	int32 n;
    35  
    36  	// Arm both EVFILT_READ and EVFILT_WRITE in edge-triggered mode (EV_CLEAR)
    37  	// for the whole fd lifetime.  The notifications are automatically unregistered
    38  	// when fd is closed.
    39  	ev[0].ident = (uint32)fd;
    40  	ev[0].filter = EVFILT_READ;
    41  	ev[0].flags = EV_ADD|EV_CLEAR;
    42  	ev[0].fflags = 0;
    43  	ev[0].data = 0;
    44  	ev[0].udata = (kevent_udata)pd;
    45  	ev[1] = ev[0];
    46  	ev[1].filter = EVFILT_WRITE;
    47  	n = runtime·kevent(kq, ev, 2, nil, 0, nil);
    48  	if(n < 0)
    49  		return -n;
    50  	return 0;
    51  }
    52  
    53  int32
    54  runtime·netpollclose(uintptr fd)
    55  {
    56  	// Don't need to unregister because calling close()
    57  	// on fd will remove any kevents that reference the descriptor.
    58  	USED(fd);
    59  	return 0;
    60  }
    61  
    62  // Polls for ready network connections.
    63  // Returns list of goroutines that become runnable.
    64  G*
    65  runtime·netpoll(bool block)
    66  {
    67  	static int32 lasterr;
    68  	Kevent events[64], *ev;
    69  	Timespec ts, *tp;
    70  	int32 n, i, mode;
    71  	G *gp;
    72  
    73  	if(kq == -1)
    74  		return nil;
    75  	tp = nil;
    76  	if(!block) {
    77  		ts.tv_sec = 0;
    78  		ts.tv_nsec = 0;
    79  		tp = &ts;
    80  	}
    81  	gp = nil;
    82  retry:
    83  	n = runtime·kevent(kq, nil, 0, events, nelem(events), tp);
    84  	if(n < 0) {
    85  		if(n != -EINTR && n != lasterr) {
    86  			lasterr = n;
    87  			runtime·printf("runtime: kevent on fd %d failed with %d\n", kq, -n);
    88  		}
    89  		goto retry;
    90  	}
    91  	for(i = 0; i < n; i++) {
    92  		ev = &events[i];
    93  		mode = 0;
    94  		if(ev->filter == EVFILT_READ)
    95  			mode += 'r';
    96  		if(ev->filter == EVFILT_WRITE)
    97  			mode += 'w';
    98  		if(mode)
    99  			runtime·netpollready(&gp, (PollDesc*)ev->udata, mode);
   100  	}
   101  	if(block && gp == nil)
   102  		goto retry;
   103  	return gp;
   104  }