github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/net/fd_poll_nacl.go (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  package net
     6  
     7  import (
     8  	"syscall"
     9  	"time"
    10  )
    11  
    12  type pollDesc struct {
    13  	fd      *netFD
    14  	closing bool
    15  }
    16  
    17  func (pd *pollDesc) Init(fd *netFD) error { pd.fd = fd; return nil }
    18  
    19  func (pd *pollDesc) Close() {}
    20  
    21  func (pd *pollDesc) Evict() {
    22  	pd.closing = true
    23  	if pd.fd != nil {
    24  		syscall.StopIO(pd.fd.sysfd)
    25  	}
    26  }
    27  
    28  func (pd *pollDesc) Prepare(mode int) error {
    29  	if pd.closing {
    30  		return errClosing
    31  	}
    32  	return nil
    33  }
    34  
    35  func (pd *pollDesc) PrepareRead() error { return pd.Prepare('r') }
    36  
    37  func (pd *pollDesc) PrepareWrite() error { return pd.Prepare('w') }
    38  
    39  func (pd *pollDesc) Wait(mode int) error {
    40  	if pd.closing {
    41  		return errClosing
    42  	}
    43  	return errTimeout
    44  }
    45  
    46  func (pd *pollDesc) WaitRead() error { return pd.Wait('r') }
    47  
    48  func (pd *pollDesc) WaitWrite() error { return pd.Wait('w') }
    49  
    50  func (pd *pollDesc) WaitCanceled(mode int) {}
    51  
    52  func (pd *pollDesc) WaitCanceledRead() {}
    53  
    54  func (pd *pollDesc) WaitCanceledWrite() {}
    55  
    56  func (fd *netFD) setDeadline(t time.Time) error {
    57  	return setDeadlineImpl(fd, t, 'r'+'w')
    58  }
    59  
    60  func (fd *netFD) setReadDeadline(t time.Time) error {
    61  	return setDeadlineImpl(fd, t, 'r')
    62  }
    63  
    64  func (fd *netFD) setWriteDeadline(t time.Time) error {
    65  	return setDeadlineImpl(fd, t, 'w')
    66  }
    67  
    68  func setDeadlineImpl(fd *netFD, t time.Time, mode int) error {
    69  	d := t.UnixNano()
    70  	if t.IsZero() {
    71  		d = 0
    72  	}
    73  	if err := fd.incref(); err != nil {
    74  		return err
    75  	}
    76  	switch mode {
    77  	case 'r':
    78  		syscall.SetReadDeadline(fd.sysfd, d)
    79  	case 'w':
    80  		syscall.SetWriteDeadline(fd.sysfd, d)
    81  	case 'r' + 'w':
    82  		syscall.SetReadDeadline(fd.sysfd, d)
    83  		syscall.SetWriteDeadline(fd.sysfd, d)
    84  	}
    85  	fd.decref()
    86  	return nil
    87  }