epoll.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2013 Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. from std/posix import SocketHandle
  10. const
  11. EPOLLIN* = 0x00000001
  12. EPOLLPRI* = 0x00000002
  13. EPOLLOUT* = 0x00000004
  14. EPOLLERR* = 0x00000008
  15. EPOLLHUP* = 0x00000010
  16. EPOLLRDNORM* = 0x00000040
  17. EPOLLRDBAND* = 0x00000080
  18. EPOLLWRNORM* = 0x00000100
  19. EPOLLWRBAND* = 0x00000200
  20. EPOLLMSG* = 0x00000400
  21. EPOLLRDHUP* = 0x00002000
  22. EPOLLEXCLUSIVE* = 1 shl 28
  23. EPOLLWAKEUP* = 1 shl 29
  24. EPOLLONESHOT* = 1 shl 30
  25. EPOLLET* = 1 shl 31
  26. # Valid opcodes ( "op" parameter ) to issue to epoll_ctl().
  27. const
  28. EPOLL_CTL_ADD* = 1 # Add a file descriptor to the interface.
  29. EPOLL_CTL_DEL* = 2 # Remove a file descriptor from the interface.
  30. EPOLL_CTL_MOD* = 3 # Change file descriptor epoll_event structure.
  31. # https://github.com/torvalds/linux/blob/ff6992735ade75aae3e35d16b17da1008d753d28/include/uapi/linux/eventpoll.h#L77
  32. when defined(linux) and defined(amd64):
  33. {.pragma: epollPacked, packed.}
  34. else:
  35. {.pragma: epollPacked.}
  36. type
  37. EpollData* {.importc: "epoll_data_t",
  38. header: "<sys/epoll.h>", pure, final, union.} = object
  39. `ptr`* {.importc: "ptr".}: pointer
  40. fd* {.importc: "fd".}: cint
  41. u32* {.importc: "u32".}: uint32
  42. u64* {.importc: "u64".}: uint64
  43. EpollEvent* {.importc: "struct epoll_event", header: "<sys/epoll.h>", pure, final, epollPacked.} = object
  44. events*: uint32 # Epoll events
  45. data*: EpollData # User data variable
  46. proc epoll_create*(size: cint): cint {.importc: "epoll_create",
  47. header: "<sys/epoll.h>".}
  48. ## Creates an epoll instance. Returns an fd for the new instance.
  49. ##
  50. ## The "size" parameter is a hint specifying the number of file
  51. ## descriptors to be associated with the new instance. The fd
  52. ## returned by epoll_create() should be closed with close().
  53. proc epoll_create1*(flags: cint): cint {.importc: "epoll_create1",
  54. header: "<sys/epoll.h>".}
  55. ## Same as epoll_create but with an FLAGS parameter. The unused SIZE
  56. ## parameter has been dropped.
  57. proc epoll_ctl*(epfd: cint; op: cint; fd: cint | SocketHandle; event: ptr EpollEvent): cint {.
  58. importc: "epoll_ctl", header: "<sys/epoll.h>".}
  59. ## Manipulate an epoll instance "epfd". Returns `0` in case of success,
  60. ## `-1` in case of error (the "errno" variable will contain the specific error code).
  61. ##
  62. ## The "op" parameter is one of the `EPOLL_CTL_*`
  63. ## constants defined above. The "fd" parameter is the target of the
  64. ## operation. The "event" parameter describes which events the caller
  65. ## is interested in and any associated user data.
  66. proc epoll_wait*(epfd: cint; events: ptr EpollEvent; maxevents: cint;
  67. timeout: cint): cint {.importc: "epoll_wait",
  68. header: "<sys/epoll.h>".}
  69. ## Wait for events on an epoll instance "epfd". Returns the number of
  70. ## triggered events returned in "events" buffer. Or -1 in case of
  71. ## error with the "errno" variable set to the specific error code. The
  72. ## "events" parameter is a buffer that will contain triggered
  73. ## events. The "maxevents" is the maximum number of events to be
  74. ## returned ( usually size of "events" ). The "timeout" parameter
  75. ## specifies the maximum wait time in milliseconds (-1 == infinite).
  76. ##
  77. ## This function is a cancellation point and therefore not marked with
  78. ## __THROW.
  79. #proc epoll_pwait*(epfd: cint; events: ptr EpollEvent; maxevents: cint;
  80. # timeout: cint; ss: ptr sigset_t): cint {.
  81. # importc: "epoll_pwait", header: "<sys/epoll.h>".}
  82. # Same as epoll_wait, but the thread's signal mask is temporarily
  83. # and atomically replaced with the one provided as parameter.
  84. #
  85. # This function is a cancellation point and therefore not marked with
  86. # __THROW.