Embedded Network patches - eventfd & socket getters (#19632)
This commit is contained in:
parent
7d32425a57
commit
eae29e8eaf
2 changed files with 36 additions and 9 deletions
|
|
@ -14,6 +14,12 @@ import posix, times
|
||||||
# Maximum number of events that can be returned
|
# Maximum number of events that can be returned
|
||||||
const MAX_POLL_EVENTS = 64
|
const MAX_POLL_EVENTS = 64
|
||||||
|
|
||||||
|
const hasEventFds = defined(zephyr) or defined(nimPollHasEventFds)
|
||||||
|
|
||||||
|
when hasEventFds:
|
||||||
|
proc eventfd(count: cuint, flags: cint): cint
|
||||||
|
{.cdecl, importc: "eventfd", header: "<sys/eventfd.h>".}
|
||||||
|
|
||||||
when hasThreadSupport:
|
when hasThreadSupport:
|
||||||
type
|
type
|
||||||
SelectorImpl[T] = object
|
SelectorImpl[T] = object
|
||||||
|
|
@ -184,14 +190,22 @@ proc unregister*[T](s: Selector[T], ev: SelectEvent) =
|
||||||
s.pollRemove(fdi.cint)
|
s.pollRemove(fdi.cint)
|
||||||
|
|
||||||
proc newSelectEvent*(): SelectEvent =
|
proc newSelectEvent*(): SelectEvent =
|
||||||
var fds: array[2, cint]
|
when not hasEventFds:
|
||||||
if posix.pipe(fds) != 0:
|
var fds: array[2, cint]
|
||||||
raiseIOSelectorsError(osLastError())
|
if posix.pipe(fds) != 0:
|
||||||
setNonBlocking(fds[0])
|
raiseIOSelectorsError(osLastError())
|
||||||
setNonBlocking(fds[1])
|
setNonBlocking(fds[0])
|
||||||
result = cast[SelectEvent](allocShared0(sizeof(SelectEventImpl)))
|
setNonBlocking(fds[1])
|
||||||
result.rfd = fds[0]
|
result = cast[SelectEvent](allocShared0(sizeof(SelectEventImpl)))
|
||||||
result.wfd = fds[1]
|
result.rfd = fds[0]
|
||||||
|
result.wfd = fds[1]
|
||||||
|
else:
|
||||||
|
let fdci = eventfd(0, posix.O_NONBLOCK)
|
||||||
|
if fdci == -1:
|
||||||
|
raiseIOSelectorsError(osLastError())
|
||||||
|
result = cast[SelectEvent](allocShared0(sizeof(SelectEventImpl)))
|
||||||
|
result.rfd = fdci
|
||||||
|
result.wfd = fdci
|
||||||
|
|
||||||
proc trigger*(ev: SelectEvent) =
|
proc trigger*(ev: SelectEvent) =
|
||||||
var data: uint64 = 1
|
var data: uint64 = 1
|
||||||
|
|
@ -200,7 +214,10 @@ proc trigger*(ev: SelectEvent) =
|
||||||
|
|
||||||
proc close*(ev: SelectEvent) =
|
proc close*(ev: SelectEvent) =
|
||||||
let res1 = posix.close(ev.rfd)
|
let res1 = posix.close(ev.rfd)
|
||||||
let res2 = posix.close(ev.wfd)
|
let res2 =
|
||||||
|
when hasEventFds: 0
|
||||||
|
else: posix.close(ev.wfd)
|
||||||
|
|
||||||
deallocShared(cast[pointer](ev))
|
deallocShared(cast[pointer](ev))
|
||||||
if res1 != 0 or res2 != 0:
|
if res1 != 0 or res2 != 0:
|
||||||
raiseIOSelectorsError(osLastError())
|
raiseIOSelectorsError(osLastError())
|
||||||
|
|
|
||||||
|
|
@ -1798,6 +1798,16 @@ proc isSsl*(socket: Socket): bool =
|
||||||
proc getFd*(socket: Socket): SocketHandle = return socket.fd
|
proc getFd*(socket: Socket): SocketHandle = return socket.fd
|
||||||
## Returns the socket's file descriptor
|
## Returns the socket's file descriptor
|
||||||
|
|
||||||
|
when defined(zephyr) or defined(nimNetSocketExtras): # Remove in future
|
||||||
|
proc getDomain*(socket: Socket): Domain = return socket.domain
|
||||||
|
## Returns the socket's domain
|
||||||
|
|
||||||
|
proc getType*(socket: Socket): SockType = return socket.sockType
|
||||||
|
## Returns the socket's type
|
||||||
|
|
||||||
|
proc getProtocol*(socket: Socket): Protocol = return socket.protocol
|
||||||
|
## Returns the socket's protocol
|
||||||
|
|
||||||
when defined(nimHasStyleChecks):
|
when defined(nimHasStyleChecks):
|
||||||
{.push styleChecks: off.}
|
{.push styleChecks: off.}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue