fix posix.mq_receive compilation with cpp backend (#20710)

* fix posix.mq_receive compilation with cpp backend

* only enable runnableExample on posix

* linux, not posix

* simplify example

* add test

* mqueue.h does not exist on MacOS

* place test case in own block

* drop runnableExamples
This commit is contained in:
n5m 2022-11-05 05:31:19 +00:00 • committed by GitHub
commit 51bef9b4a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 28 deletions

View file

@ -249,26 +249,52 @@ proc setlocale*(a1: cint, a2: cstring): cstring {.
proc strfmon*(a1: cstring, a2: int, a3: cstring): int {.varargs, proc strfmon*(a1: cstring, a2: int, a3: cstring): int {.varargs,
importc, header: "<monetary.h>".} importc, header: "<monetary.h>".}
when not defined(nintendoswitch): when not (defined(nintendoswitch) or defined(macos) or defined(macosx)):
proc mq_close*(a1: Mqd): cint {.importc, header: "<mqueue.h>".} proc mq_notify*(mqdes: Mqd, event: ptr SigEvent): cint {.
proc mq_getattr*(a1: Mqd, a2: ptr MqAttr): cint {.
importc, header: "<mqueue.h>".}
proc mq_notify*(a1: Mqd, a2: ptr SigEvent): cint {.
importc, header: "<mqueue.h>".}
proc mq_open*(a1: cstring, a2: cint): Mqd {.
varargs, importc, header: "<mqueue.h>".}
proc mq_receive*(a1: Mqd, a2: cstring, a3: int, a4: var int): int {.
importc, header: "<mqueue.h>".}
proc mq_send*(a1: Mqd, a2: cstring, a3: int, a4: int): cint {.
importc, header: "<mqueue.h>".}
proc mq_setattr*(a1: Mqd, a2, a3: ptr MqAttr): cint {.
importc, header: "<mqueue.h>".} importc, header: "<mqueue.h>".}
proc mq_timedreceive*(a1: Mqd, a2: cstring, a3: int, a4: int, proc mq_open*(name: cstring, flags: cint): Mqd {.
a5: ptr Timespec): int {.importc, header: "<mqueue.h>".} varargs, importc, header: "<mqueue.h>".}
proc mq_timedsend*(a1: Mqd, a2: cstring, a3: int, a4: int,
a5: ptr Timespec): cint {.importc, header: "<mqueue.h>".} proc mq_close*(mqdes: Mqd): cint {.importc, header: "<mqueue.h>".}
proc mq_unlink*(a1: cstring): cint {.importc, header: "<mqueue.h>".}
proc mq_receive*(
mqdes: Mqd,
buffer: cstring,
length: csize_t,
priority: var cuint
): int {.importc, header: "<mqueue.h>".}
proc mq_timedreceive*(
mqdes: Mqd,
buffer: cstring,
length: csize_t,
priority: cuint,
timeout: ptr Timespec
): int {.importc, header: "<mqueue.h>".}
proc mq_send*(
mqdes: Mqd,
buffer: cstring,
length: csize_t,
priority: cuint
): cint {.importc, header: "<mqueue.h>".}
proc mq_timedsend*(
mqdes: Mqd,
buffer: cstring,
length: csize_t,
priority: cuint,
timeout: ptr Timespec
): cint {.importc, header: "<mqueue.h>".}
proc mq_getattr*(mqdes: Mqd, attribute: ptr MqAttr): cint {.
importc, header: "<mqueue.h>".}
proc mq_setattr*(mqdes: Mqd, newAttribute, oldAttribute: ptr MqAttr): cint {.
importc, header: "<mqueue.h>".}
proc mq_unlink*(mqdes: cstring): cint {.importc, header: "<mqueue.h>".}
proc getpwnam*(a1: cstring): ptr Passwd {.importc, header: "<pwd.h>".} proc getpwnam*(a1: cstring): ptr Passwd {.importc, header: "<pwd.h>".}

View file

@ -108,15 +108,6 @@ type
p_sign_posn*: char p_sign_posn*: char
thousands_sep*: cstring thousands_sep*: cstring
Mqd* {.importc: "mqd_t", header: "<mqueue.h>", final, pure.} = object
MqAttr* {.importc: "struct mq_attr",
header: "<mqueue.h>",
final, pure.} = object ## message queue attribute
mq_flags*: int ## Message queue flags.
mq_maxmsg*: int ## Maximum number of messages.
mq_msgsize*: int ## Maximum message size.
mq_curmsgs*: int ## Number of messages currently queued.
Passwd* {.importc: "struct passwd", header: "<pwd.h>", Passwd* {.importc: "struct passwd", header: "<pwd.h>",
final, pure.} = object ## struct passwd final, pure.} = object ## struct passwd
pw_name*: cstring ## User's login name. pw_name*: cstring ## User's login name.

View file

@ -7,7 +7,7 @@ outputsub: ""
when not defined(windows): when not defined(windows):
import posix import posix
import std/syncio import std/[assertions, syncio]
var var
u: Utsname u: Utsname
@ -18,3 +18,46 @@ when not defined(windows):
writeLine(stdout, u.nodename) writeLine(stdout, u.nodename)
writeLine(stdout, u.release) writeLine(stdout, u.release)
writeLine(stdout, u.machine) writeLine(stdout, u.machine)
when not (defined(nintendoswitch) or defined(macos) or defined(macosx)):
block:
type Message = object
value: int
const MQ_PATH: cstring = "/top_level_file"
const MQ_PRIORITY: cuint = 170
const MQ_MESSAGE_SIZE: csize_t = csize_t(sizeof(Message))
let mqd_a: posix.MqAttr = MqAttr(mq_maxmsg: 10, mq_msgsize: clong(MQ_MESSAGE_SIZE))
let writable: posix.Mqd = posix.mq_open(
MQ_PATH,
posix.O_CREAT or posix.O_WRONLY or posix.O_NONBLOCK,
posix.S_IRWXU,
addr(mqd_a)
)
let readable: posix.Mqd = posix.mq_open(
MQ_PATH,
posix.O_RDONLY or posix.O_NONBLOCK,
posix.S_IRWXU,
addr(mqd_a)
)
let sent: Message = Message(value: 88)
block:
let success: int = writable.mq_send(
cast[cstring](sent.addr),
MQ_MESSAGE_SIZE,
MQ_PRIORITY
)
doAssert success == 0, $success
block:
var buffer: Message
var priority: cuint
let bytesRead: int = readable.mq_receive(
cast[cstring](buffer.addr),
MQ_MESSAGE_SIZE,
priority
)
doAssert buffer == sent
doAssert bytesRead == int(MQ_MESSAGE_SIZE)