Modified and more reliable kqueue.nim, made according to openbsd/netbsd/freebsd and macosx headers.
This commit is contained in:
parent
4457b0980f
commit
e91e53401e
1 changed files with 110 additions and 25 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Nim's Runtime Library
|
# Nim's Runtime Library
|
||||||
# (c) Copyright 2015 Adam Strzelecki
|
# (c) Copyright 2016 Eugene Kabanov
|
||||||
#
|
#
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
|
|
@ -11,20 +11,35 @@
|
||||||
|
|
||||||
from posix import Timespec
|
from posix import Timespec
|
||||||
|
|
||||||
# Filters:
|
when defined(macosx) or defined(freebsd) or defined(openbsd):
|
||||||
const
|
const
|
||||||
EVFILT_READ* = -1
|
EVFILT_READ* = -1
|
||||||
EVFILT_WRITE* = -2
|
EVFILT_WRITE* = -2
|
||||||
EVFILT_AIO* = -3
|
EVFILT_AIO* = -3 ## attached to aio requests
|
||||||
EVFILT_VNODE* = -4
|
EVFILT_VNODE* = -4 ## attached to vnodes
|
||||||
EVFILT_PROC* = -5
|
EVFILT_PROC* = -5 ## attached to struct proc
|
||||||
EVFILT_SIGNAL* = -6
|
EVFILT_SIGNAL* = -6 ## attached to struct proc
|
||||||
EVFILT_TIMER* = -7
|
EVFILT_TIMER* = -7 ## timers
|
||||||
EVFILT_MACHPORT* = -8
|
elif defined(netbsd):
|
||||||
EVFILT_FS* = -9
|
const
|
||||||
EVFILT_USER* = -10
|
EVFILT_READ* = 0
|
||||||
# -11 is unused
|
EVFILT_WRITE* = 1
|
||||||
EVFILT_VM* = -12
|
EVFILT_AIO* = 2 ## attached to aio requests
|
||||||
|
EVFILT_VNODE* = 3 ## attached to vnodes
|
||||||
|
EVFILT_PROC* = 4 ## attached to struct proc
|
||||||
|
EVFILT_SIGNAL* = 5 ## attached to struct proc
|
||||||
|
EVFILT_TIMER* = 6 ## timers (in ms)
|
||||||
|
when defined(macosx):
|
||||||
|
const
|
||||||
|
EVFILT_MACHPORT* = -8 ## Mach portsets
|
||||||
|
EVFILT_FS* = -9 ## filesystem events
|
||||||
|
EVFILT_USER* = -10 ## user events
|
||||||
|
EVFILT_VM = -12 ## virtual memory events
|
||||||
|
elif defined(freebsd):
|
||||||
|
const
|
||||||
|
EVFILT_FS* = -9 ## filesystem events
|
||||||
|
EVFILT_LIO* = -10 ## attached to lio requests
|
||||||
|
EVFILT_USER* = -11 ## user events
|
||||||
|
|
||||||
# Actions:
|
# Actions:
|
||||||
const
|
const
|
||||||
|
|
@ -41,21 +56,91 @@ const
|
||||||
EV_RECEIPT* = 0x0040 ## Force EV_ERROR on success, data == 0
|
EV_RECEIPT* = 0x0040 ## Force EV_ERROR on success, data == 0
|
||||||
EV_DISPATCH* = 0x0080 ## Disable event after reporting.
|
EV_DISPATCH* = 0x0080 ## Disable event after reporting.
|
||||||
|
|
||||||
|
EV_SYSFLAGS* = 0xF000 ## Reserved by system
|
||||||
|
EV_DROP* = 0x1000 ## Not should be dropped
|
||||||
|
EV_FLAG1* = 0x2000 ## Filter-specific flag
|
||||||
|
|
||||||
# Return values:
|
# Return values:
|
||||||
const
|
const
|
||||||
EV_EOF* = 0x8000 ## EOF detected
|
EV_EOF* = 0x8000 ## EOF detected
|
||||||
EV_ERROR* = 0x4000 ## Error, data contains errno
|
EV_ERROR* = 0x4000 ## Error, data contains errno
|
||||||
|
|
||||||
|
|
||||||
|
when defined(macosx) or defined(freebsd):
|
||||||
|
# EVFILT_USER is not supported by OpenBSD and NetBSD
|
||||||
|
#
|
||||||
|
# data/hint flags/masks for EVFILT_USER, shared with userspace
|
||||||
|
#
|
||||||
|
# On input, the top two bits of fflags specifies how the lower twenty four
|
||||||
|
# bits should be applied to the stored value of fflags.
|
||||||
|
#
|
||||||
|
# On output, the top two bits will always be set to NOTE_FFNOP and the
|
||||||
|
# remaining twenty four bits will contain the stored fflags value.
|
||||||
|
const
|
||||||
|
NOTE_FFNOP* = 0x00000000'u32 ## ignore input fflags
|
||||||
|
NOTE_FFAND* = 0x40000000'u32 ## AND fflags
|
||||||
|
NOTE_FFOR* = 0x80000000'u32 ## OR fflags
|
||||||
|
NOTE_FFCOPY* = 0xc0000000'u32 ## copy fflags
|
||||||
|
NOTE_FFCTRLMASK* = 0xc0000000'u32 ## masks for operations
|
||||||
|
NOTE_FFLAGSMASK* = 0x00ffffff'u32
|
||||||
|
|
||||||
|
NOTE_TRIGGER* = 0x01000000'u32 ## Cause the event to be triggered
|
||||||
|
## for output.
|
||||||
|
|
||||||
|
# data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
|
||||||
|
const
|
||||||
|
NOTE_LOWAT* = 0x0001 ## low water mark
|
||||||
|
|
||||||
|
# data/hint flags for EVFILT_VNODE, shared with userspace
|
||||||
|
const
|
||||||
|
NOTE_DELETE* = 0x0001 ## vnode was removed
|
||||||
|
NOTE_WRITE* = 0x0002 ## data contents changed
|
||||||
|
NOTE_EXTEND* = 0x0004 ## size increased
|
||||||
|
NOTE_ATTRIB* = 0x0008 ## attributes changed
|
||||||
|
NOTE_LINK* = 0x0010 ## link count changed
|
||||||
|
NOTE_RENAME* = 0x0020 ## vnode was renamed
|
||||||
|
NOTE_REVOKE* = 0x0040 ## vnode access was revoked
|
||||||
|
|
||||||
|
# data/hint flags for EVFILT_PROC, shared with userspace
|
||||||
|
const
|
||||||
|
NOTE_EXIT* = 0x80000000'u32 ## process exited
|
||||||
|
NOTE_FORK* = 0x40000000'u32 ## process forked
|
||||||
|
NOTE_EXEC* = 0x20000000'u32 ## process exec'd
|
||||||
|
NOTE_PCTRLMASK* = 0xf0000000'u32 ## mask for hint bits
|
||||||
|
NOTE_PDATAMASK* = 0x000fffff'u32 ## mask for pid
|
||||||
|
|
||||||
|
# additional flags for EVFILT_PROC
|
||||||
|
const
|
||||||
|
NOTE_TRACK* = 0x00000001'u32 ## follow across forks
|
||||||
|
NOTE_TRACKERR* = 0x00000002'u32 ## could not track child
|
||||||
|
NOTE_CHILD* = 0x00000004'u32 ## am a child process
|
||||||
|
|
||||||
|
when defined(macosx) or defined(freebsd):
|
||||||
|
# additional flags for EVFILE_TIMER
|
||||||
|
const
|
||||||
|
NOTE_SECONDS* = 0x00000001'u32 ## data is seconds
|
||||||
|
NOTE_MSECONDS* = 0x00000002'u32 ## data is milliseconds
|
||||||
|
NOTE_USECONDS* = 0x00000004'u32 ## data is microseconds
|
||||||
|
NOTE_NSECONDS* = 0x00000008'u32 ## data is nanoseconds
|
||||||
|
else:
|
||||||
|
# NetBSD and OpenBSD doesnt support NOTE_{TIME} constants, but
|
||||||
|
# support EVFILT_TIMER with granularity of milliseconds.
|
||||||
|
const
|
||||||
|
NOTE_MSECONDS* = 0x00000000'u32
|
||||||
|
|
||||||
type
|
type
|
||||||
KEvent* {.importc: "struct kevent", pure, final
|
## This define not fully satisfy NetBSD "struct kevent"
|
||||||
|
## but it works and tested.
|
||||||
|
KEvent* {.importc: "struct kevent",
|
||||||
header: """#include <sys/types.h>
|
header: """#include <sys/types.h>
|
||||||
#include <sys/event.h>""".} = object
|
#include <sys/event.h>
|
||||||
ident*: cuint ## identifier for this event (uintptr_t)
|
#include <sys/time.h>""", pure, final.} = object
|
||||||
filter*: cshort ## filter for event
|
ident* : uint ## identifier for this event (uintptr_t)
|
||||||
flags*: cushort ## general flags
|
filter* : cshort ## filter for event
|
||||||
fflags*: cuint ## filter-specific flags
|
flags* : cushort ## general flags
|
||||||
data*: cuint ## filter-specific data (intptr_t)
|
fflags* : cuint ## filter-specific flags
|
||||||
#udata*: ptr void ## opaque user data identifier
|
data* : int ## filter-specific data (intptr_t)
|
||||||
|
udata* : pointer ## opaque user data identifier
|
||||||
|
|
||||||
proc kqueue*(): cint {.importc: "kqueue", header: "<sys/event.h>".}
|
proc kqueue*(): cint {.importc: "kqueue", header: "<sys/event.h>".}
|
||||||
## Creates new queue and returns its descriptor.
|
## Creates new queue and returns its descriptor.
|
||||||
|
|
@ -66,7 +151,7 @@ proc kevent*(kqFD: cint,
|
||||||
{.importc: "kevent", header: "<sys/event.h>".}
|
{.importc: "kevent", header: "<sys/event.h>".}
|
||||||
## Manipulates queue for given ``kqFD`` descriptor.
|
## Manipulates queue for given ``kqFD`` descriptor.
|
||||||
|
|
||||||
proc EV_SET*(event: ptr KEvent, ident: cuint, filter: cshort, flags: cushort,
|
proc EV_SET*(event: ptr KEvent, ident: uint, filter: cshort, flags: cushort,
|
||||||
fflags: cuint, data: cuint, udata: ptr void)
|
fflags: cuint, data: int, udata: pointer)
|
||||||
{.importc: "EV_SET", header: "<sys/event.h>".}
|
{.importc: "EV_SET", header: "<sys/event.h>".}
|
||||||
## Fills event with given data.
|
## Fills event with given data.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue