epoll.nim 3.5 KB

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