Module Fut.Unix

module Unix: sig .. end
Unix system calls as futures.

This module wraps Fut.Unix system calls that support asynchronous operation and a few other as futures.

For other blocking Unix system calls you can invoke them with Fut.Unix.apply which uses future queues to perform the call, catches unix errors and automatically handles the EINTR error by retrying the call.

Important. File descriptors created outside this module must be set to non-blocking mode with Unix.set_nonblock before they are used with functions of this module.



Unix results and errors


type error = Unix.error * string * string 
The type for Unix errors as reported by Unix.Unix_error exceptions.
type 'a result = [ `Error of error | `Ok of 'a ] 
The type for Unix function results.
val ubind : [> 'b result ] Fut.t ->
('a -> [> 'b result ] Fut.t) -> [> 'b result ] Fut.t
val apply : ?queue:Fut.queue -> ('a -> 'b) -> 'a -> [> 'b result ] Fut.t
apply queue f v applies f v on queue and catches Unix.Unix_error. EINTR is handled by retrying the call.
val call : ('a -> 'b) -> 'a -> [> 'b result ] Fut.t
call f v applies f v synchronously and catches Unix.Unix_error. EINTR is handled by retrying the call.

Signals


val signal : int -> int Fut.t
signal s determines with s when the signal s is received.

Warning. This replaces any handler set with Sys.set_signal.


File descriptors


val nonblock_stdio : unit -> [> unit result ] Fut.t
nonblock_stdio () sets Unix.stdin, Unix.stdout, Unix.stderr to non-blocking mode.
val close : Unix.file_descr -> [> unit result ] Fut.t
close fd is like Unix.close fd, except it handles EINTR and sets any pending read or write on fd to never determine.
val dup2 : Unix.file_descr -> Unix.file_descr -> [> unit result ] Fut.t
dup2 fd1 fd2 is like Unix.dup2 fd1 fd2, except it handles EINTR and sets any pending read or write on fd2 to never determine.
val pipe : unit -> [> (Unix.file_descr * Unix.file_descr) result ] Fut.t
pipe () is like Unix.pipe (), except is sets both file descriptors to non-blocking mode with Unix.set_nonblock.

Sockets


val socket : Unix.socket_domain ->
Unix.socket_type -> int -> [> Unix.file_descr result ] Fut.t
socket d t p is like Unix.socket d t p except it sets the resulting file descriptor to non-blocking mode with Unix.set_nonblock.
val socketpair : Unix.socket_domain ->
Unix.socket_type ->
int -> [> (Unix.file_descr * Unix.file_descr) result ] Fut.t
socketpair d t p is like Unix.socketpair d t p except it sets the resulting file descriptors to non-blocking mode with Unix.set_nonblock.
val accept : Unix.file_descr -> Unix.file_descr * Unix.sockaddr
accept fd is like Unix.accept fd except it handles EINTR and EWOULDBLOCK and sets the resulting file descriptor to non-blocking mode with Unix.set_nonblock.
val connect : Unix.file_descr -> Unix.sockaddr -> [> unit result ] Fut.t
connect is like Fut.Unix.connect except it handles EINTR and EINPROGRESS.
val bind : Unix.file_descr -> Unix.sockaddr -> [ `Error of Unix.error | `Ok ] Fut.t

IO

Important. If you use these functions on a file descriptor fd use Fut.Unix.close and Fut.Unix.dup2 instead of the corresponding functions of the Unix module. This will prevent any EBADF errors if there are undetermined futures concerning fd.

val read : Unix.file_descr -> string -> int -> int -> [> int result ] Fut.t
read fd s j l is like Unix.read fd s j l except it handles EINTR, EAGAIN and EWOULDBLOCK. It is set to never determine if fd is closed with Fut.Unix.close or Fut.Unix.dup2.
val write : Unix.file_descr -> string -> int -> int -> [> int result ] Fut.t
write fd s j l is like Unix.single_write fd s j l except it handles EINTR, EAGAIN and EWOULDBLOCK. It is set to never determine if fd is closed with Fut.Unix.close or Fut.Unix.dup2.