* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / net / fd_poll_runtime.go
blobe3b4f7e464801f1aebe446190136f29339140b36
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.
5 // +build darwin linux
7 package net
9 import (
10 "sync"
11 "syscall"
12 "time"
15 func runtime_pollServerInit()
16 func runtime_pollOpen(fd int) (uintptr, int)
17 func runtime_pollClose(ctx uintptr)
18 func runtime_pollWait(ctx uintptr, mode int) int
19 func runtime_pollReset(ctx uintptr, mode int) int
20 func runtime_pollSetDeadline(ctx uintptr, d int64, mode int)
21 func runtime_pollUnblock(ctx uintptr)
23 var canCancelIO = true // used for testing current package
25 type pollDesc struct {
26 runtimeCtx uintptr
29 var serverInit sync.Once
31 func sysInit() {
34 func (pd *pollDesc) Init(fd *netFD) error {
35 serverInit.Do(runtime_pollServerInit)
36 ctx, errno := runtime_pollOpen(fd.sysfd)
37 if errno != 0 {
38 return syscall.Errno(errno)
40 pd.runtimeCtx = ctx
41 return nil
44 func (pd *pollDesc) Close() {
45 runtime_pollClose(pd.runtimeCtx)
48 func (pd *pollDesc) Lock() {
51 func (pd *pollDesc) Unlock() {
54 func (pd *pollDesc) Wakeup() {
57 // Evict evicts fd from the pending list, unblocking any I/O running on fd.
58 // Return value is whether the pollServer should be woken up.
59 func (pd *pollDesc) Evict() bool {
60 runtime_pollUnblock(pd.runtimeCtx)
61 return false
64 func (pd *pollDesc) PrepareRead() error {
65 res := runtime_pollReset(pd.runtimeCtx, 'r')
66 return convertErr(res)
69 func (pd *pollDesc) PrepareWrite() error {
70 res := runtime_pollReset(pd.runtimeCtx, 'w')
71 return convertErr(res)
74 func (pd *pollDesc) WaitRead() error {
75 res := runtime_pollWait(pd.runtimeCtx, 'r')
76 return convertErr(res)
79 func (pd *pollDesc) WaitWrite() error {
80 res := runtime_pollWait(pd.runtimeCtx, 'w')
81 return convertErr(res)
84 func convertErr(res int) error {
85 switch res {
86 case 0:
87 return nil
88 case 1:
89 return errClosing
90 case 2:
91 return errTimeout
93 panic("unreachable")
96 func setReadDeadline(fd *netFD, t time.Time) error {
97 return setDeadlineImpl(fd, t, 'r')
100 func setWriteDeadline(fd *netFD, t time.Time) error {
101 return setDeadlineImpl(fd, t, 'w')
104 func setDeadline(fd *netFD, t time.Time) error {
105 return setDeadlineImpl(fd, t, 'r'+'w')
108 func setDeadlineImpl(fd *netFD, t time.Time, mode int) error {
109 d := t.UnixNano()
110 if t.IsZero() {
111 d = 0
113 if err := fd.incref(false); err != nil {
114 return err
116 runtime_pollSetDeadline(fd.pd.runtimeCtx, d, mode)
117 fd.decref()
118 return nil