kqueue.nim 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2016 Eugene Kabanov
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. from posix import Timespec
  10. when defined(macosx) or defined(freebsd) or defined(openbsd) or
  11. defined(dragonfly):
  12. const
  13. EVFILT_READ* = -1
  14. EVFILT_WRITE* = -2
  15. EVFILT_AIO* = -3 ## attached to aio requests
  16. EVFILT_VNODE* = -4 ## attached to vnodes
  17. EVFILT_PROC* = -5 ## attached to struct proc
  18. EVFILT_SIGNAL* = -6 ## attached to struct proc
  19. EVFILT_TIMER* = -7 ## timers
  20. elif defined(netbsd):
  21. const
  22. EVFILT_READ* = 0
  23. EVFILT_WRITE* = 1
  24. EVFILT_AIO* = 2 ## attached to aio requests
  25. EVFILT_VNODE* = 3 ## attached to vnodes
  26. EVFILT_PROC* = 4 ## attached to struct proc
  27. EVFILT_SIGNAL* = 5 ## attached to struct proc
  28. EVFILT_TIMER* = 6 ## timers (in ms)
  29. when defined(macosx):
  30. const
  31. EVFILT_MACHPORT* = -8 ## Mach portsets
  32. EVFILT_FS* = -9 ## filesystem events
  33. EVFILT_USER* = -10 ## user events
  34. EVFILT_VM = -12 ## virtual memory events
  35. elif defined(freebsd):
  36. const
  37. EVFILT_FS* = -9 ## filesystem events
  38. EVFILT_LIO* = -10 ## attached to lio requests
  39. EVFILT_USER* = -11 ## user events
  40. elif defined(dragonfly):
  41. const
  42. EVFILT_EXCEPT* = -8 ## exceptional conditions
  43. EVFILT_USER* = -9 ## user events
  44. EVFILT_FS* = -10 ## filesystem events
  45. # Actions:
  46. const
  47. EV_ADD* = 0x0001 ## Add event to queue (implies enable).
  48. ## Re-adding an existing element modifies it.
  49. EV_DELETE* = 0x0002 ## Delete event from queue.
  50. EV_ENABLE* = 0x0004 ## Enable event.
  51. EV_DISABLE* = 0x0008 ## Disable event (not reported).
  52. # Flags:
  53. const
  54. EV_ONESHOT* = 0x0010 ## Only report one occurrence.
  55. EV_CLEAR* = 0x0020 ## Clear event state after reporting.
  56. EV_RECEIPT* = 0x0040 ## Force EV_ERROR on success, data == 0
  57. EV_DISPATCH* = 0x0080 ## Disable event after reporting.
  58. EV_SYSFLAGS* = 0xF000 ## Reserved by system
  59. EV_DROP* = 0x1000 ## Not should be dropped
  60. EV_FLAG1* = 0x2000 ## Filter-specific flag
  61. # Return values:
  62. const
  63. EV_EOF* = 0x8000 ## EOF detected
  64. EV_ERROR* = 0x4000 ## Error, data contains errno
  65. EV_NODATA* = 0x1000 ## EOF and no more data
  66. when defined(macosx) or defined(freebsd) or defined(dragonfly):
  67. # EVFILT_USER is not supported by OpenBSD and NetBSD
  68. #
  69. # data/hint flags/masks for EVFILT_USER, shared with userspace
  70. #
  71. # On input, the top two bits of fflags specifies how the lower twenty four
  72. # bits should be applied to the stored value of fflags.
  73. #
  74. # On output, the top two bits will always be set to NOTE_FFNOP and the
  75. # remaining twenty four bits will contain the stored fflags value.
  76. const
  77. NOTE_FFNOP* = 0x00000000'u32 ## ignore input fflags
  78. NOTE_FFAND* = 0x40000000'u32 ## AND fflags
  79. NOTE_FFOR* = 0x80000000'u32 ## OR fflags
  80. NOTE_FFCOPY* = 0xc0000000'u32 ## copy fflags
  81. NOTE_FFCTRLMASK* = 0xc0000000'u32 ## masks for operations
  82. NOTE_FFLAGSMASK* = 0x00ffffff'u32
  83. NOTE_TRIGGER* = 0x01000000'u32 ## Cause the event to be triggered
  84. ## for output.
  85. # data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
  86. const
  87. NOTE_LOWAT* = 0x0001 ## low water mark
  88. # data/hint flags for EVFILT_VNODE, shared with userspace
  89. const
  90. NOTE_DELETE* = 0x0001 ## vnode was removed
  91. NOTE_WRITE* = 0x0002 ## data contents changed
  92. NOTE_EXTEND* = 0x0004 ## size increased
  93. NOTE_ATTRIB* = 0x0008 ## attributes changed
  94. NOTE_LINK* = 0x0010 ## link count changed
  95. NOTE_RENAME* = 0x0020 ## vnode was renamed
  96. NOTE_REVOKE* = 0x0040 ## vnode access was revoked
  97. # data/hint flags for EVFILT_PROC, shared with userspace
  98. const
  99. NOTE_EXIT* = 0x80000000'u32 ## process exited
  100. NOTE_FORK* = 0x40000000'u32 ## process forked
  101. NOTE_EXEC* = 0x20000000'u32 ## process exec'd
  102. NOTE_PCTRLMASK* = 0xf0000000'u32 ## mask for hint bits
  103. NOTE_PDATAMASK* = 0x000fffff'u32 ## mask for pid
  104. # additional flags for EVFILT_PROC
  105. const
  106. NOTE_TRACK* = 0x00000001'u32 ## follow across forks
  107. NOTE_TRACKERR* = 0x00000002'u32 ## could not track child
  108. NOTE_CHILD* = 0x00000004'u32 ## am a child process
  109. when defined(macosx) or defined(freebsd):
  110. # additional flags for EVFILE_TIMER
  111. const
  112. NOTE_SECONDS* = 0x00000001'u32 ## data is seconds
  113. NOTE_MSECONDS* = 0x00000002'u32 ## data is milliseconds
  114. NOTE_USECONDS* = 0x00000004'u32 ## data is microseconds
  115. NOTE_NSECONDS* = 0x00000008'u32 ## data is nanoseconds
  116. else:
  117. # NetBSD and OpenBSD doesn't support NOTE_{TIME} constants, but
  118. # support EVFILT_TIMER with granularity of milliseconds.
  119. const
  120. NOTE_MSECONDS* = 0x00000000'u32
  121. type
  122. ## This define not fully satisfy NetBSD "struct kevent"
  123. ## but it works and tested.
  124. KEvent* {.importc: "struct kevent",
  125. header: """#include <sys/types.h>
  126. #include <sys/event.h>
  127. #include <sys/time.h>""", pure, final.} = object
  128. ident* : uint ## identifier for this event (uintptr_t)
  129. filter* : cshort ## filter for event
  130. flags* : cushort ## general flags
  131. fflags* : cuint ## filter-specific flags
  132. data* : int ## filter-specific data (intptr_t)
  133. udata* : pointer ## opaque user data identifier
  134. proc kqueue*(): cint {.importc: "kqueue", header: "<sys/event.h>".}
  135. ## Creates new queue and returns its descriptor.
  136. proc kevent*(kqFD: cint,
  137. changelist: ptr KEvent, nchanges: cint,
  138. eventlist: ptr KEvent, nevents: cint, timeout: ptr Timespec): cint
  139. {.importc: "kevent", header: "<sys/event.h>".}
  140. ## Manipulates queue for given ``kqFD`` descriptor.
  141. proc EV_SET*(event: ptr KEvent, ident: uint, filter: cshort, flags: cushort,
  142. fflags: cuint, data: int, udata: pointer)
  143. {.importc: "EV_SET", header: "<sys/event.h>".}
  144. ## Fills event with given data.