fd_poll_nacl.go 1.8 KB

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