epoll.nim 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. {.deadCodeElim:on.}
  10. from posix import SocketHandle
  11. const
  12. EPOLLIN* = 0x00000001
  13. EPOLLPRI* = 0x00000002
  14. EPOLLOUT* = 0x00000004
  15. EPOLLERR* = 0x00000008
  16. EPOLLHUP* = 0x00000010
  17. EPOLLRDNORM* = 0x00000040
  18. EPOLLRDBAND* = 0x00000080
  19. EPOLLWRNORM* = 0x00000100
  20. EPOLLWRBAND* = 0x00000200
  21. EPOLLMSG* = 0x00000400
  22. EPOLLRDHUP* = 0x00002000
  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. epoll_data* {.importc: "union epoll_data",
  33. header: "<sys/epoll.h>", pure, final.} = object # TODO: This is actually a union.
  34. #thePtr* {.importc: "ptr".}: pointer
  35. fd* {.importc: "fd".}: cint
  36. when defined(linux) and defined(amd64):
  37. u32: uint32 # this field ensures that binary size is right - it cannot be
  38. # used because its offset is wrong
  39. #u64*: uint64
  40. epoll_event* {.importc: "struct epoll_event", header: "<sys/epoll.h>", pure, final.} = object
  41. events*: uint32 # Epoll events
  42. data*: epoll_data # User data variable
  43. proc epoll_create*(size: cint): cint {.importc: "epoll_create",
  44. header: "<sys/epoll.h>".}
  45. ## Creates an epoll instance. Returns an fd for the new instance.
  46. ## The "size" parameter is a hint specifying the number of file
  47. ## descriptors to be associated with the new instance. The fd
  48. ## returned by epoll_create() should be closed with close().
  49. proc epoll_create1*(flags: cint): cint {.importc: "epoll_create1",
  50. header: "<sys/epoll.h>".}
  51. ## Same as epoll_create but with an FLAGS parameter. The unused SIZE
  52. ## parameter has been dropped.
  53. proc epoll_ctl*(epfd: cint; op: cint; fd: cint | SocketHandle; event: ptr epoll_event): cint {.
  54. importc: "epoll_ctl", header: "<sys/epoll.h>".}
  55. ## Manipulate an epoll instance "epfd". Returns 0 in case of success,
  56. ## -1 in case of error ( the "errno" variable will contain the
  57. ## specific error code ) The "op" parameter is one of the EPOLL_CTL_*
  58. ## constants defined above. The "fd" parameter is the target of the
  59. ## operation. The "event" parameter describes which events the caller
  60. ## is interested in and any associated user data.
  61. proc epoll_wait*(epfd: cint; events: ptr epoll_event; maxevents: cint;
  62. timeout: cint): cint {.importc: "epoll_wait",
  63. header: "<sys/epoll.h>".}
  64. ## Wait for events on an epoll instance "epfd". Returns the number of
  65. ## triggered events returned in "events" buffer. Or -1 in case of
  66. ## error with the "errno" variable set to the specific error code. The
  67. ## "events" parameter is a buffer that will contain triggered
  68. ## events. The "maxevents" is the maximum number of events to be
  69. ## returned ( usually size of "events" ). The "timeout" parameter
  70. ## specifies the maximum wait time in milliseconds (-1 == infinite).
  71. ##
  72. ## This function is a cancellation point and therefore not marked with
  73. ## __THROW.
  74. #proc epoll_pwait*(epfd: cint; events: ptr epoll_event; maxevents: cint;
  75. # timeout: cint; ss: ptr sigset_t): cint {.
  76. # importc: "epoll_pwait", header: "<sys/epoll.h>".}
  77. # Same as epoll_wait, but the thread's signal mask is temporarily
  78. # and atomically replaced with the one provided as parameter.
  79. #
  80. # This function is a cancellation point and therefore not marked with
  81. # __THROW.