libgo: update to go1.9
[official-gcc.git] / libgo / go / runtime / netpoll_aix.go
blob8c8e1882219f2cb87aac7ca561b932791aeb5c5a
1 // Copyright 2017 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.
5 package runtime
7 import "unsafe"
9 // This is based on the former libgo/runtime/netpoll_select.c implementation
10 // except that it uses AIX pollset_poll instead of select and is written in Go.
12 type pollset_t int32
14 type pollfd struct {
15 fd int32
16 events int16
17 revents int16
20 const _POLLIN = 0x0001
21 const _POLLOUT = 0x0002
22 const _POLLHUP = 0x2000
23 const _POLLERR = 0x4000
25 type poll_ctl struct {
26 cmd int16
27 events int16
28 fd int32
31 const _PS_ADD = 0x0
32 const _PS_DELETE = 0x2
34 //extern pollset_create
35 func pollset_create(maxfd int32) pollset_t
37 //extern pollset_ctl
38 func pollset_ctl(ps pollset_t, pollctl_array *poll_ctl, array_length int32) int32
40 //extern pollset_poll
41 func pollset_poll(ps pollset_t, polldata_array *pollfd, array_length int32, timeout int32) int32
43 //extern pipe
44 func libc_pipe(fd *int32) int32
46 //extern __go_fcntl_uintptr
47 func fcntlUintptr(fd, cmd, arg uintptr) (uintptr, uintptr)
49 func fcntl(fd, cmd int32, arg uintptr) uintptr {
50 r, _ := fcntlUintptr(uintptr(fd), uintptr(cmd), arg)
51 return r
54 var (
55 ps pollset_t = -1
56 mpfds map[int32]*pollDesc
57 pmtx mutex
58 rdwake int32
59 wrwake int32
60 needsUpdate bool
63 func netpollinit() {
64 var p [2]int32
66 if ps = pollset_create(-1); ps < 0 {
67 throw("netpollinit: failed to create pollset")
69 // It is not possible to add or remove descriptors from
70 // the pollset while pollset_poll is active.
71 // We use a pipe to wakeup pollset_poll when the pollset
72 // needs to be updated.
73 if err := libc_pipe(&p[0]); err < 0 {
74 throw("netpollinit: failed to create pipe")
76 rdwake = p[0]
77 wrwake = p[1]
79 fl := fcntl(rdwake, _F_GETFL, 0)
80 fcntl(rdwake, _F_SETFL, fl|_O_NONBLOCK)
81 fcntl(rdwake, _F_SETFD, _FD_CLOEXEC)
83 fl = fcntl(wrwake, _F_GETFL, 0)
84 fcntl(wrwake, _F_SETFL, fl|_O_NONBLOCK)
85 fcntl(wrwake, _F_SETFD, _FD_CLOEXEC)
87 // Add the read side of the pipe to the pollset.
88 var pctl poll_ctl
89 pctl.cmd = _PS_ADD
90 pctl.fd = rdwake
91 pctl.events = _POLLIN
92 if pollset_ctl(ps, &pctl, 1) != 0 {
93 throw("netpollinit: failed to register pipe")
96 mpfds = make(map[int32]*pollDesc)
99 func netpollopen(fd uintptr, pd *pollDesc) int32 {
100 // pollset_ctl will block if pollset_poll is active
101 // so wakeup pollset_poll first.
102 lock(&pmtx)
103 needsUpdate = true
104 unlock(&pmtx)
105 b := [1]byte{0}
106 write(uintptr(wrwake), unsafe.Pointer(&b[0]), 1)
108 var pctl poll_ctl
109 pctl.cmd = _PS_ADD
110 pctl.fd = int32(fd)
111 pctl.events = _POLLIN | _POLLOUT
112 if pollset_ctl(ps, &pctl, 1) != 0 {
113 return int32(errno())
115 lock(&pmtx)
116 mpfds[int32(fd)] = pd
117 needsUpdate = false
118 unlock(&pmtx)
120 return 0
123 func netpollclose(fd uintptr) int32 {
124 // pollset_ctl will block if pollset_poll is active
125 // so wakeup pollset_poll first.
126 lock(&pmtx)
127 needsUpdate = true
128 unlock(&pmtx)
129 b := [1]byte{0}
130 write(uintptr(wrwake), unsafe.Pointer(&b[0]), 1)
132 var pctl poll_ctl
133 pctl.cmd = _PS_DELETE
134 pctl.fd = int32(fd)
135 if pollset_ctl(ps, &pctl, 1) != 0 {
136 return int32(errno())
138 lock(&pmtx)
139 delete(mpfds, int32(fd))
140 needsUpdate = false
141 unlock(&pmtx)
143 return 0
146 func netpollarm(pd *pollDesc, mode int) {
147 throw("unused")
150 func netpoll(block bool) *g {
151 if ps == -1 {
152 return nil
154 timeout := int32(-1)
155 if !block {
156 timeout = 0
158 var pfds [128]pollfd
159 retry:
160 lock(&pmtx)
161 if needsUpdate {
162 unlock(&pmtx)
163 osyield()
164 goto retry
166 unlock(&pmtx)
167 nfound := pollset_poll(ps, &pfds[0], int32(len(pfds)), timeout)
168 if nfound < 0 {
169 e := errno()
170 if e != _EINTR {
171 throw("pollset_poll failed")
173 goto retry
175 var gp guintptr
176 for i := int32(0); i < nfound; i++ {
177 pfd := &pfds[i]
179 var mode int32
180 if pfd.revents&(_POLLIN|_POLLHUP|_POLLERR) != 0 {
181 if pfd.fd == rdwake {
182 var b [1]byte
183 read(pfd.fd, unsafe.Pointer(&b[0]), 1)
184 continue
186 mode += 'r'
188 if pfd.revents&(_POLLOUT|_POLLHUP|_POLLERR) != 0 {
189 mode += 'w'
191 if mode != 0 {
192 lock(&pmtx)
193 pd := mpfds[pfd.fd]
194 unlock(&pmtx)
195 if pd != nil {
196 netpollready(&gp, pd, mode)
200 if block && gp == 0 {
201 goto retry
203 return gp.ptr()