epoll.nim 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 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. type
  32. EpollData* {.importc: "union epoll_data",
  33. header: "<sys/epoll.h>", pure, final.} = object # TODO: This is actually a union.
  34. u64* {.importc: "u64".}: uint64
  35. EpollEvent* {.importc: "struct epoll_event", header: "<sys/epoll.h>", pure, final.} = object
  36. events*: uint32 # Epoll events
  37. data*: EpollData # User data variable
  38. proc epoll_create*(size: cint): cint {.importc: "epoll_create",
  39. header: "<sys/epoll.h>".}
  40. ## Creates an epoll instance. Returns an fd for the new instance.
  41. ##
  42. ## The "size" parameter is a hint specifying the number of file
  43. ## descriptors to be associated with the new instance. The fd
  44. ## returned by epoll_create() should be closed with close().
  45. proc epoll_create1*(flags: cint): cint {.importc: "epoll_create1",
  46. header: "<sys/epoll.h>".}
  47. ## Same as epoll_create but with an FLAGS parameter. The unused SIZE
  48. ## parameter has been dropped.
  49. proc epoll_ctl*(epfd: cint; op: cint; fd: cint | SocketHandle; event: ptr EpollEvent): cint {.
  50. importc: "epoll_ctl", header: "<sys/epoll.h>".}
  51. ## Manipulate an epoll instance "epfd". Returns ``0`` in case of success,
  52. ## ``-1`` in case of error (the "errno" variable will contain the specific error code).
  53. ##
  54. ## The "op" parameter is one of the ``EPOLL_CTL_*``
  55. ## constants defined above. The "fd" parameter is the target of the
  56. ## operation. The "event" parameter describes which events the caller
  57. ## is interested in and any associated user data.
  58. proc epoll_wait*(epfd: cint; events: ptr EpollEvent; maxevents: cint;
  59. timeout: cint): cint {.importc: "epoll_wait",
  60. header: "<sys/epoll.h>".}
  61. ## Wait for events on an epoll instance "epfd". Returns the number of
  62. ## triggered events returned in "events" buffer. Or -1 in case of
  63. ## error with the "errno" variable set to the specific error code. The
  64. ## "events" parameter is a buffer that will contain triggered
  65. ## events. The "maxevents" is the maximum number of events to be
  66. ## returned ( usually size of "events" ). The "timeout" parameter
  67. ## specifies the maximum wait time in milliseconds (-1 == infinite).
  68. ##
  69. ## This function is a cancellation point and therefore not marked with
  70. ## __THROW.
  71. #proc epoll_pwait*(epfd: cint; events: ptr EpollEvent; maxevents: cint;
  72. # timeout: cint; ss: ptr sigset_t): cint {.
  73. # importc: "epoll_pwait", header: "<sys/epoll.h>".}
  74. # Same as epoll_wait, but the thread's signal mask is temporarily
  75. # and atomically replaced with the one provided as parameter.
  76. #
  77. # This function is a cancellation point and therefore not marked with
  78. # __THROW.