LEFT | RIGHT |
(no file at all) | |
| 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 linux,386 linux,amd64 |
| 6 |
| 7 #include "runtime.h" |
| 8 #include "defs_GOOS_GOARCH.h" |
| 9 |
| 10 int32 runtime·epollcreate(int32 size); |
| 11 int32 runtime·epollcreate1(int32 flags); |
| 12 int32 runtime·epollctl(int32 epfd, int32 op, int32 fd, EpollEvent *ev); |
| 13 int32 runtime·epollwait(int32 epfd, EpollEvent *ev, int32 nev, int32 timeout); |
| 14 void runtime·closeonexec(int32 fd); |
| 15 |
| 16 static int32 epfd = -1; // epoll descriptor |
| 17 |
| 18 void |
| 19 runtime·netpollinit(void) |
| 20 { |
| 21 epfd = runtime·epollcreate1(EPOLL_CLOEXEC); |
| 22 if(epfd >= 0) |
| 23 return; |
| 24 epfd = runtime·epollcreate(1024); |
| 25 if(epfd >= 0) { |
| 26 runtime·closeonexec(epfd); |
| 27 return; |
| 28 } |
| 29 runtime·printf("netpollinit: failed to create descriptor (%d)\n", -epfd)
; |
| 30 runtime·throw("netpollinit: failed to create descriptor"); |
| 31 } |
| 32 |
| 33 int32 |
| 34 runtime·netpollopen(int32 fd, PollDesc *pd) |
| 35 { |
| 36 EpollEvent ev; |
| 37 |
| 38 ev.events = EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET; |
| 39 ev.data = (uint64)pd; |
| 40 return runtime·epollctl(epfd, EPOLL_CTL_ADD, fd, &ev); |
| 41 } |
| 42 |
| 43 // polls for ready network connections |
| 44 // returns list of goroutines that become runnable |
| 45 G* |
| 46 runtime·netpoll(bool block) |
| 47 { |
| 48 EpollEvent events[128], *ev; |
| 49 int32 n, i, waitms, mode; |
| 50 G *gp; |
| 51 |
| 52 if(epfd == -1) |
| 53 return nil; |
| 54 waitms = -1; |
| 55 if(!block) |
| 56 waitms = 0; |
| 57 retry: |
| 58 n = runtime·epollwait(epfd, events, nelem(events), waitms); |
| 59 if(n < 0) { |
| 60 if(n != -EINTR) |
| 61 runtime·printf("epollwait failed with %d\n", -n); |
| 62 goto retry; |
| 63 } |
| 64 gp = nil; |
| 65 for(i = 0; i < n; i++) { |
| 66 ev = &events[i]; |
| 67 if(ev->events == 0) |
| 68 continue; |
| 69 mode = 0; |
| 70 if(ev->events & (EPOLLIN|EPOLLRDHUP|EPOLLHUP|EPOLLERR)) |
| 71 mode += 'r'; |
| 72 if(ev->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) |
| 73 mode += 'w'; |
| 74 if(mode) |
| 75 runtime·netpollready(&gp, (void*)ev->data, mode); |
| 76 } |
| 77 if(block && gp == nil) |
| 78 goto retry; |
| 79 return gp; |
| 80 } |
LEFT | RIGHT |