newpollserver_rtems.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2010 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. "os"
  7. "syscall"
  8. )
  9. func selfConnectedTCPSocket() (pr, pw *os.File, err error) {
  10. // See ../syscall/exec.go for description of ForkLock.
  11. syscall.ForkLock.RLock()
  12. sockfd, e := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
  13. if e != 0 {
  14. syscall.ForkLock.RUnlock()
  15. return nil, nil, os.Errno(e)
  16. }
  17. syscall.CloseOnExec(sockfd)
  18. syscall.ForkLock.RUnlock()
  19. // Allow reuse of recently-used addresses.
  20. syscall.SetsockoptInt(sockfd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
  21. var laTCP *TCPAddr
  22. var la syscall.Sockaddr
  23. if laTCP, err = ResolveTCPAddr("127.0.0.1:0"); err != nil {
  24. Error:
  25. return nil, nil, err
  26. }
  27. if la, err = laTCP.sockaddr(syscall.AF_INET); err != nil {
  28. goto Error
  29. }
  30. e = syscall.Bind(sockfd, la)
  31. if e != 0 {
  32. Errno:
  33. syscall.Close(sockfd)
  34. return nil, nil, os.Errno(e)
  35. }
  36. laddr, _ := syscall.Getsockname(sockfd)
  37. e = syscall.Connect(sockfd, laddr)
  38. if e != 0 {
  39. goto Errno
  40. }
  41. fd := os.NewFile(sockfd, "wakeupSocket")
  42. return fd, fd, nil
  43. }
  44. func newPollServer() (s *pollServer, err error) {
  45. s = new(pollServer)
  46. s.cr = make(chan *netFD, 1)
  47. s.cw = make(chan *netFD, 1)
  48. // s.pr and s.pw are indistinguishable.
  49. if s.pr, s.pw, err = selfConnectedTCPSocket(); err != nil {
  50. return nil, err
  51. }
  52. var e int
  53. if e = syscall.SetNonblock(s.pr.Fd(), true); e != 0 {
  54. Errno:
  55. err = &os.PathError{"setnonblock", s.pr.Name(), os.Errno(e)}
  56. Error:
  57. s.pr.Close()
  58. return nil, err
  59. }
  60. if s.poll, err = newpollster(); err != nil {
  61. goto Error
  62. }
  63. if _, err = s.poll.AddFD(s.pr.Fd(), 'r', true); err != nil {
  64. s.poll.Close()
  65. goto Error
  66. }
  67. s.pending = make(map[int]*netFD)
  68. go s.Run()
  69. return s, nil
  70. }