Skip to content

Instantly share code, notes, and snippets.

@narslan
Created April 9, 2024 00:51
Show Gist options
  • Save narslan/fa1e17e6eec40baadb19e5ecb413de15 to your computer and use it in GitHub Desktop.
Save narslan/fa1e17e6eec40baadb19e5ecb413de15 to your computer and use it in GitHub Desktop.
posix send with sml
open MLton.Pointer
open MLton.Platform
val O_RDONLY = 0wx0 and O_WRONLY = 0wx1 and O_RDWR = 0wx2
val O_CREAT = 0wx40 and O_CLOEXEC = 0x80000 and O_EXCL = 0wx80 and O_NONBLOCK = 0wx800
val S_IRUSR = 0wx100 and S_IWUSR = 0wx80
val mq_open = _import "mq_open" : string * int * int * t -> int;
val mq_close = _import "mq_close" : int -> int;
val mq_unlink = _import "mq_unlink" : string -> int;
val mq_send = _import "mq_send" : int * string * int * int -> int;
exception Ev of string
fun mqOpen name oflag mode = let
val fd = mq_open (name, oflag, mode, null)
in
if fd >= 0
then fd
else raise Ev "mqOpen"
end
fun mqClose mqdesc = let
val fd = mq_close(mqdesc)
in
if fd = 0
then 1
else raise Ev "mqClose"
end
fun mqUnlink name = let
val fd = mq_unlink(name)
in
if fd = 0
then 1
else raise Ev "mqUnlink"
end
fun mqSend mqdes msg = let
val msg_len = String.size msg
val fd = mq_send (mqdes, msg, msg_len, 0)
in
if fd >= 0
then fd
else raise Ev "mqSend"
end
fun sayInt i = print (Int.toString i)
fun sayWord i = print (Word8.fmt StringCvt.BIN i)
fun orb (x,y) = Word.toInt(Word.orb (x,y))
fun main () = let
val oflag = orb(O_RDWR, O_CREAT)
val mode = orb(S_IRUSR, S_IWUSR)
val mq = mqOpen "/hello" oflag mode
in
sayInt mq;
mqSend mq "here";
mqClose mq
(* mqUnlink("/hello") *)
end
val _ = main ()
(* mlton -default-ann 'allowFFI true' posixmq.mlb *)
local
$(SML_LIB)/basis/mlton.mlb
$(SML_LIB)/basis/basis.mlb
in
posixmq.sml
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment