ioselectors_epoll.nim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. # This module implements Linux epoll().
  10. import posix, times, epoll
  11. # Maximum number of events that can be returned
  12. const MAX_EPOLL_EVENTS = 64
  13. when not defined(android):
  14. type
  15. SignalFdInfo* {.importc: "struct signalfd_siginfo",
  16. header: "<sys/signalfd.h>", pure, final.} = object
  17. ssi_signo*: uint32
  18. ssi_errno*: int32
  19. ssi_code*: int32
  20. ssi_pid*: uint32
  21. ssi_uid*: uint32
  22. ssi_fd*: int32
  23. ssi_tid*: uint32
  24. ssi_band*: uint32
  25. ssi_overrun*: uint32
  26. ssi_trapno*: uint32
  27. ssi_status*: int32
  28. ssi_int*: int32
  29. ssi_ptr*: uint64
  30. ssi_utime*: uint64
  31. ssi_stime*: uint64
  32. ssi_addr*: uint64
  33. pad* {.importc: "__pad".}: array[0..47, uint8]
  34. proc timerfd_create(clock_id: ClockId, flags: cint): cint
  35. {.cdecl, importc: "timerfd_create", header: "<sys/timerfd.h>".}
  36. proc timerfd_settime(ufd: cint, flags: cint,
  37. utmr: var Itimerspec, otmr: var Itimerspec): cint
  38. {.cdecl, importc: "timerfd_settime", header: "<sys/timerfd.h>".}
  39. proc eventfd(count: cuint, flags: cint): cint
  40. {.cdecl, importc: "eventfd", header: "<sys/eventfd.h>".}
  41. when not defined(android):
  42. proc signalfd(fd: cint, mask: var Sigset, flags: cint): cint
  43. {.cdecl, importc: "signalfd", header: "<sys/signalfd.h>".}
  44. when hasThreadSupport:
  45. type
  46. SelectorImpl[T] = object
  47. epollFD: cint
  48. maxFD: int
  49. numFD: int
  50. fds: ptr SharedArray[SelectorKey[T]]
  51. count*: int
  52. Selector*[T] = ptr SelectorImpl[T]
  53. else:
  54. type
  55. SelectorImpl[T] = object
  56. epollFD: cint
  57. maxFD: int
  58. numFD: int
  59. fds: seq[SelectorKey[T]]
  60. count*: int
  61. Selector*[T] = ref SelectorImpl[T]
  62. type
  63. SelectEventImpl = object
  64. efd: cint
  65. SelectEvent* = ptr SelectEventImpl
  66. proc newSelector*[T](): Selector[T] =
  67. # Retrieve the maximum fd count (for current OS) via getrlimit()
  68. var maxFD = maxDescriptors()
  69. doAssert(maxFD > 0)
  70. # Start with a reasonable size, checkFd() will grow this on demand
  71. const numFD = 1024
  72. var epollFD = epoll_create1(O_CLOEXEC)
  73. if epollFD < 0:
  74. raiseOSError(osLastError())
  75. when hasThreadSupport:
  76. result = cast[Selector[T]](allocShared0(sizeof(SelectorImpl[T])))
  77. result.epollFD = epollFD
  78. result.maxFD = maxFD
  79. result.numFD = numFD
  80. result.fds = allocSharedArray[SelectorKey[T]](numFD)
  81. else:
  82. result = Selector[T]()
  83. result.epollFD = epollFD
  84. result.maxFD = maxFD
  85. result.numFD = numFD
  86. result.fds = newSeq[SelectorKey[T]](numFD)
  87. for i in 0 ..< numFD:
  88. result.fds[i].ident = InvalidIdent
  89. proc close*[T](s: Selector[T]) =
  90. let res = posix.close(s.epollFD)
  91. when hasThreadSupport:
  92. deallocSharedArray(s.fds)
  93. deallocShared(cast[pointer](s))
  94. if res != 0:
  95. raiseIOSelectorsError(osLastError())
  96. proc newSelectEvent*(): SelectEvent =
  97. let fdci = eventfd(0, O_CLOEXEC or O_NONBLOCK)
  98. if fdci == -1:
  99. raiseIOSelectorsError(osLastError())
  100. result = cast[SelectEvent](allocShared0(sizeof(SelectEventImpl)))
  101. result.efd = fdci
  102. proc trigger*(ev: SelectEvent) =
  103. var data: uint64 = 1
  104. if posix.write(ev.efd, addr data, sizeof(uint64)) == -1:
  105. raiseIOSelectorsError(osLastError())
  106. proc close*(ev: SelectEvent) =
  107. let res = posix.close(ev.efd)
  108. deallocShared(cast[pointer](ev))
  109. if res != 0:
  110. raiseIOSelectorsError(osLastError())
  111. template checkFd(s, f) =
  112. # TODO: I don't see how this can ever happen. You won't be able to create an
  113. # FD if there is too many. -- DP
  114. if f >= s.maxFD:
  115. raiseIOSelectorsError("Maximum number of descriptors is exhausted!")
  116. if f >= s.numFD:
  117. var numFD = s.numFD
  118. while numFD <= f: numFD *= 2
  119. when hasThreadSupport:
  120. s.fds = reallocSharedArray(s.fds, numFD)
  121. else:
  122. s.fds.setLen(numFD)
  123. for i in s.numFD ..< numFD:
  124. s.fds[i].ident = InvalidIdent
  125. s.numFD = numFD
  126. proc registerHandle*[T](s: Selector[T], fd: int | SocketHandle,
  127. events: set[Event], data: T) =
  128. let fdi = int(fd)
  129. s.checkFd(fdi)
  130. doAssert(s.fds[fdi].ident == InvalidIdent, "Descriptor $# already registered" % $fdi)
  131. s.setKey(fdi, events, 0, data)
  132. if events != {}:
  133. var epv = EpollEvent(events: EPOLLRDHUP)
  134. epv.data.u64 = fdi.uint
  135. if Event.Read in events: epv.events = epv.events or EPOLLIN
  136. if Event.Write in events: epv.events = epv.events or EPOLLOUT
  137. if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fdi.cint, addr epv) != 0:
  138. raiseIOSelectorsError(osLastError())
  139. inc(s.count)
  140. proc updateHandle*[T](s: Selector[T], fd: int | SocketHandle, events: set[Event]) =
  141. let maskEvents = {Event.Timer, Event.Signal, Event.Process, Event.Vnode,
  142. Event.User, Event.Oneshot, Event.Error}
  143. let fdi = int(fd)
  144. s.checkFd(fdi)
  145. var pkey = addr(s.fds[fdi])
  146. doAssert(pkey.ident != InvalidIdent,
  147. "Descriptor $# is not registered in the selector!" % $fdi)
  148. doAssert(pkey.events * maskEvents == {})
  149. if pkey.events != events:
  150. var epv = EpollEvent(events: EPOLLRDHUP)
  151. epv.data.u64 = fdi.uint
  152. if Event.Read in events: epv.events = epv.events or EPOLLIN
  153. if Event.Write in events: epv.events = epv.events or EPOLLOUT
  154. if pkey.events == {}:
  155. if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fdi.cint, addr epv) != 0:
  156. raiseIOSelectorsError(osLastError())
  157. inc(s.count)
  158. else:
  159. if events != {}:
  160. if epoll_ctl(s.epollFD, EPOLL_CTL_MOD, fdi.cint, addr epv) != 0:
  161. raiseIOSelectorsError(osLastError())
  162. else:
  163. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  164. raiseIOSelectorsError(osLastError())
  165. dec(s.count)
  166. pkey.events = events
  167. proc unregister*[T](s: Selector[T], fd: int|SocketHandle) =
  168. let fdi = int(fd)
  169. s.checkFd(fdi)
  170. var pkey = addr(s.fds[fdi])
  171. doAssert(pkey.ident != InvalidIdent,
  172. "Descriptor $# is not registered in the selector!" % $fdi)
  173. if pkey.events != {}:
  174. when not defined(android):
  175. if Event.Read in pkey.events or Event.Write in pkey.events or Event.User in pkey.events:
  176. var epv = EpollEvent()
  177. # TODO: Refactor all these EPOLL_CTL_DEL + dec(s.count) into a proc.
  178. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  179. raiseIOSelectorsError(osLastError())
  180. dec(s.count)
  181. elif Event.Timer in pkey.events:
  182. if Event.Finished notin pkey.events:
  183. var epv = EpollEvent()
  184. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  185. raiseIOSelectorsError(osLastError())
  186. dec(s.count)
  187. if posix.close(cint(fdi)) != 0:
  188. raiseIOSelectorsError(osLastError())
  189. elif Event.Signal in pkey.events:
  190. var epv = EpollEvent()
  191. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  192. raiseIOSelectorsError(osLastError())
  193. var nmask, omask: Sigset
  194. discard sigemptyset(nmask)
  195. discard sigemptyset(omask)
  196. discard sigaddset(nmask, cint(s.fds[fdi].param))
  197. unblockSignals(nmask, omask)
  198. dec(s.count)
  199. if posix.close(cint(fdi)) != 0:
  200. raiseIOSelectorsError(osLastError())
  201. elif Event.Process in pkey.events:
  202. if Event.Finished notin pkey.events:
  203. var epv = EpollEvent()
  204. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  205. raiseIOSelectorsError(osLastError())
  206. var nmask, omask: Sigset
  207. discard sigemptyset(nmask)
  208. discard sigemptyset(omask)
  209. discard sigaddset(nmask, SIGCHLD)
  210. unblockSignals(nmask, omask)
  211. dec(s.count)
  212. if posix.close(cint(fdi)) != 0:
  213. raiseIOSelectorsError(osLastError())
  214. else:
  215. if Event.Read in pkey.events or Event.Write in pkey.events or Event.User in pkey.events:
  216. var epv = EpollEvent()
  217. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  218. raiseIOSelectorsError(osLastError())
  219. dec(s.count)
  220. elif Event.Timer in pkey.events:
  221. if Event.Finished notin pkey.events:
  222. var epv = EpollEvent()
  223. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  224. raiseIOSelectorsError(osLastError())
  225. dec(s.count)
  226. if posix.close(cint(fdi)) != 0:
  227. raiseIOSelectorsError(osLastError())
  228. clearKey(pkey)
  229. proc unregister*[T](s: Selector[T], ev: SelectEvent) =
  230. let fdi = int(ev.efd)
  231. s.checkFd(fdi)
  232. var pkey = addr(s.fds[fdi])
  233. doAssert(pkey.ident != InvalidIdent, "Event is not registered in the queue!")
  234. doAssert(Event.User in pkey.events)
  235. var epv = EpollEvent()
  236. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fdi.cint, addr epv) != 0:
  237. raiseIOSelectorsError(osLastError())
  238. dec(s.count)
  239. clearKey(pkey)
  240. proc registerTimer*[T](s: Selector[T], timeout: int, oneshot: bool,
  241. data: T): int {.discardable.} =
  242. var
  243. newTs: Itimerspec
  244. oldTs: Itimerspec
  245. let fdi = timerfd_create(CLOCK_MONOTONIC, O_CLOEXEC or O_NONBLOCK).int
  246. if fdi == -1:
  247. raiseIOSelectorsError(osLastError())
  248. s.checkFd(fdi)
  249. doAssert(s.fds[fdi].ident == InvalidIdent)
  250. var events = {Event.Timer}
  251. var epv = EpollEvent(events: EPOLLIN or EPOLLRDHUP)
  252. epv.data.u64 = fdi.uint
  253. if oneshot:
  254. newTs.it_interval.tv_sec = posix.Time(0)
  255. newTs.it_interval.tv_nsec = 0
  256. newTs.it_value.tv_sec = posix.Time(timeout div 1_000)
  257. newTs.it_value.tv_nsec = (timeout %% 1_000) * 1_000_000
  258. incl(events, Event.Oneshot)
  259. epv.events = epv.events or EPOLLONESHOT
  260. else:
  261. newTs.it_interval.tv_sec = posix.Time(timeout div 1000)
  262. newTs.it_interval.tv_nsec = (timeout %% 1_000) * 1_000_000
  263. newTs.it_value.tv_sec = newTs.it_interval.tv_sec
  264. newTs.it_value.tv_nsec = newTs.it_interval.tv_nsec
  265. if timerfd_settime(fdi.cint, cint(0), newTs, oldTs) != 0:
  266. raiseIOSelectorsError(osLastError())
  267. if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fdi.cint, addr epv) != 0:
  268. raiseIOSelectorsError(osLastError())
  269. s.setKey(fdi, events, 0, data)
  270. inc(s.count)
  271. result = fdi
  272. when not defined(android):
  273. proc registerSignal*[T](s: Selector[T], signal: int,
  274. data: T): int {.discardable.} =
  275. var
  276. nmask: Sigset
  277. omask: Sigset
  278. discard sigemptyset(nmask)
  279. discard sigemptyset(omask)
  280. discard sigaddset(nmask, cint(signal))
  281. blockSignals(nmask, omask)
  282. let fdi = signalfd(-1, nmask, O_CLOEXEC or O_NONBLOCK).int
  283. if fdi == -1:
  284. raiseIOSelectorsError(osLastError())
  285. s.checkFd(fdi)
  286. doAssert(s.fds[fdi].ident == InvalidIdent)
  287. var epv = EpollEvent(events: EPOLLIN or EPOLLRDHUP)
  288. epv.data.u64 = fdi.uint
  289. if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fdi.cint, addr epv) != 0:
  290. raiseIOSelectorsError(osLastError())
  291. s.setKey(fdi, {Event.Signal}, signal, data)
  292. inc(s.count)
  293. result = fdi
  294. proc registerProcess*[T](s: Selector, pid: int,
  295. data: T): int {.discardable.} =
  296. var
  297. nmask: Sigset
  298. omask: Sigset
  299. discard sigemptyset(nmask)
  300. discard sigemptyset(omask)
  301. discard sigaddset(nmask, posix.SIGCHLD)
  302. blockSignals(nmask, omask)
  303. let fdi = signalfd(-1, nmask, O_CLOEXEC or O_NONBLOCK).int
  304. if fdi == -1:
  305. raiseIOSelectorsError(osLastError())
  306. s.checkFd(fdi)
  307. doAssert(s.fds[fdi].ident == InvalidIdent)
  308. var epv = EpollEvent(events: EPOLLIN or EPOLLRDHUP)
  309. epv.data.u64 = fdi.uint
  310. epv.events = EPOLLIN or EPOLLRDHUP
  311. if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fdi.cint, addr epv) != 0:
  312. raiseIOSelectorsError(osLastError())
  313. s.setKey(fdi, {Event.Process, Event.Oneshot}, pid, data)
  314. inc(s.count)
  315. result = fdi
  316. proc registerEvent*[T](s: Selector[T], ev: SelectEvent, data: T) =
  317. let fdi = int(ev.efd)
  318. doAssert(s.fds[fdi].ident == InvalidIdent, "Event is already registered in the queue!")
  319. s.setKey(fdi, {Event.User}, 0, data)
  320. var epv = EpollEvent(events: EPOLLIN or EPOLLRDHUP)
  321. epv.data.u64 = ev.efd.uint
  322. if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, ev.efd, addr epv) != 0:
  323. raiseIOSelectorsError(osLastError())
  324. inc(s.count)
  325. proc selectInto*[T](s: Selector[T], timeout: int,
  326. results: var openArray[ReadyKey]): int =
  327. var
  328. resTable: array[MAX_EPOLL_EVENTS, EpollEvent]
  329. maxres = MAX_EPOLL_EVENTS
  330. i, k: int
  331. if maxres > len(results):
  332. maxres = len(results)
  333. verifySelectParams(timeout)
  334. let count = epoll_wait(s.epollFD, addr(resTable[0]), maxres.cint,
  335. timeout.cint)
  336. if count < 0:
  337. result = 0
  338. let err = osLastError()
  339. if cint(err) != EINTR:
  340. raiseIOSelectorsError(err)
  341. elif count == 0:
  342. result = 0
  343. else:
  344. i = 0
  345. k = 0
  346. while i < count:
  347. let fdi = int(resTable[i].data.u64)
  348. let pevents = resTable[i].events
  349. var pkey = addr(s.fds[fdi])
  350. doAssert(pkey.ident != InvalidIdent)
  351. var rkey = ReadyKey(fd: fdi, events: {})
  352. if (pevents and EPOLLERR) != 0 or (pevents and EPOLLHUP) != 0:
  353. if (pevents and EPOLLHUP) != 0:
  354. rkey.errorCode = OSErrorCode ECONNRESET
  355. else:
  356. # Try reading SO_ERROR from fd.
  357. var error: cint
  358. var size = SockLen sizeof(error)
  359. if getsockopt(SocketHandle fdi, SOL_SOCKET, SO_ERROR, addr(error),
  360. addr(size)) == 0'i32:
  361. rkey.errorCode = OSErrorCode error
  362. rkey.events.incl(Event.Error)
  363. if (pevents and EPOLLOUT) != 0:
  364. rkey.events.incl(Event.Write)
  365. when not defined(android):
  366. if (pevents and EPOLLIN) != 0:
  367. if Event.Read in pkey.events:
  368. rkey.events.incl(Event.Read)
  369. elif Event.Timer in pkey.events:
  370. var data: uint64 = 0
  371. if posix.read(cint(fdi), addr data,
  372. sizeof(uint64)) != sizeof(uint64):
  373. raiseIOSelectorsError(osLastError())
  374. rkey.events.incl(Event.Timer)
  375. elif Event.Signal in pkey.events:
  376. var data = SignalFdInfo()
  377. if posix.read(cint(fdi), addr data,
  378. sizeof(SignalFdInfo)) != sizeof(SignalFdInfo):
  379. raiseIOSelectorsError(osLastError())
  380. rkey.events.incl(Event.Signal)
  381. elif Event.Process in pkey.events:
  382. var data = SignalFdInfo()
  383. if posix.read(cint(fdi), addr data,
  384. sizeof(SignalFdInfo)) != sizeof(SignalFdInfo):
  385. raiseIOSelectorsError(osLastError())
  386. if cast[int](data.ssi_pid) == pkey.param:
  387. rkey.events.incl(Event.Process)
  388. else:
  389. inc(i)
  390. continue
  391. elif Event.User in pkey.events:
  392. var data: uint64 = 0
  393. if posix.read(cint(fdi), addr data,
  394. sizeof(uint64)) != sizeof(uint64):
  395. let err = osLastError()
  396. if err == OSErrorCode(EAGAIN):
  397. inc(i)
  398. continue
  399. else:
  400. raiseIOSelectorsError(err)
  401. rkey.events.incl(Event.User)
  402. else:
  403. if (pevents and EPOLLIN) != 0:
  404. if Event.Read in pkey.events:
  405. rkey.events.incl(Event.Read)
  406. elif Event.Timer in pkey.events:
  407. var data: uint64 = 0
  408. if posix.read(cint(fdi), addr data,
  409. sizeof(uint64)) != sizeof(uint64):
  410. raiseIOSelectorsError(osLastError())
  411. rkey.events.incl(Event.Timer)
  412. elif Event.User in pkey.events:
  413. var data: uint64 = 0
  414. if posix.read(cint(fdi), addr data,
  415. sizeof(uint64)) != sizeof(uint64):
  416. let err = osLastError()
  417. if err == OSErrorCode(EAGAIN):
  418. inc(i)
  419. continue
  420. else:
  421. raiseIOSelectorsError(err)
  422. rkey.events.incl(Event.User)
  423. if Event.Oneshot in pkey.events:
  424. var epv = EpollEvent()
  425. if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, cint(fdi), addr epv) != 0:
  426. raiseIOSelectorsError(osLastError())
  427. # we will not clear key until it will be unregistered, so
  428. # application can obtain data, but we will decrease counter,
  429. # because epoll is empty.
  430. dec(s.count)
  431. # we are marking key with `Finished` event, to avoid double decrease.
  432. pkey.events.incl(Event.Finished)
  433. results[k] = rkey
  434. inc(k)
  435. inc(i)
  436. result = k
  437. proc select*[T](s: Selector[T], timeout: int): seq[ReadyKey] =
  438. result = newSeq[ReadyKey](MAX_EPOLL_EVENTS)
  439. let count = selectInto(s, timeout, result)
  440. result.setLen(count)
  441. template isEmpty*[T](s: Selector[T]): bool =
  442. (s.count == 0)
  443. proc contains*[T](s: Selector[T], fd: SocketHandle|int): bool {.inline.} =
  444. return s.fds[fd.int].ident != InvalidIdent
  445. proc getData*[T](s: Selector[T], fd: SocketHandle|int): var T =
  446. let fdi = int(fd)
  447. s.checkFd(fdi)
  448. if fdi in s:
  449. result = s.fds[fdi].data
  450. proc setData*[T](s: Selector[T], fd: SocketHandle|int, data: T): bool =
  451. let fdi = int(fd)
  452. s.checkFd(fdi)
  453. if fdi in s:
  454. s.fds[fdi].data = data
  455. result = true
  456. template withData*[T](s: Selector[T], fd: SocketHandle|int, value,
  457. body: untyped) =
  458. mixin checkFd
  459. let fdi = int(fd)
  460. s.checkFd(fdi)
  461. if fdi in s:
  462. var value = addr(s.fds[fdi].data)
  463. body
  464. template withData*[T](s: Selector[T], fd: SocketHandle|int, value, body1,
  465. body2: untyped) =
  466. mixin checkFd
  467. let fdi = int(fd)
  468. s.checkFd(fdi)
  469. if fdi in s:
  470. var value = addr(s.fds[fdi].data)
  471. body1
  472. else:
  473. body2
  474. proc getFd*[T](s: Selector[T]): int =
  475. return s.epollFD.int