blob: fe7aee56c31433fcbf75d933ceb4ac6219e63c6c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import std/posix
type PollData* = object
fds: seq[TPollFd]
iterator events*(ctx: PollData): TPollFd =
let L = ctx.fds.len
for i in 0 ..< L:
let event = ctx.fds[i]
if event.fd == -1 or ctx.fds[i].revents == 0:
continue
assert (event.revents and POLLNVAL) == 0
yield event
proc register*(ctx: var PollData; fd: int; events: cshort) =
if fd >= ctx.fds.len:
let olen = ctx.fds.len
ctx.fds.setLen(fd + 1)
for i in olen ..< fd:
ctx.fds[i].fd = -1
ctx.fds[fd].fd = cint(fd)
ctx.fds[fd].events = events
proc register*(ctx: var PollData; fd: cint; events: cshort) =
ctx.register(int(fd), events)
proc unregister*(ctx: var PollData; fd: int) =
ctx.fds[fd].fd = -1
proc trim(ctx: var PollData) =
var i = ctx.fds.high
while i >= 0:
if ctx.fds[i].fd != -1:
break
dec i
ctx.fds.setLen(i + 1)
proc clear*(ctx: var PollData) =
ctx.fds.setLen(0)
proc poll*(ctx: var PollData; timeout: cint) =
ctx.trim()
let fds = addr ctx.fds[0]
let nfds = cint(ctx.fds.len)
{.emit: """
poll(`fds`, `nfds`, `timeout`);
""".}
|