Module Lwt_unix
Cooperative system calls
val handle_unix_error : ('a -> 'b Lwt.t) -> 'a -> 'b Lwt.tSame as
Unix.handle_unix_errorbut catches lwt-level exceptions
Sleeping
val sleep : float -> unit Lwt.tsleep dis a thread that remains suspended fordseconds and then terminates.
val yield : unit -> unit Lwt.tyield ()is a thread that suspends itself and then resumes as soon as possible and terminates.
val auto_yield : float -> unit -> unit Lwt.tauto_yield timeoutreturns a functionf, andf ()has the following behavior:- If it has been more than
timeoutseconds since the last timef ()behaved likeLwt_unix.yield,f ()callsLwt_unix.yield. - Otherwise, if it has been less than
timeoutseconds,f ()behaves likeLwt.return_unit, i.e. it does not yield.
- If it has been more than
Operation on file-descriptors
type file_descrThe abstract type for file descriptors. A Lwt file descriptor is a pair of a unix file descriptor (of type
Unix.file_descr) and a state.A file descriptor may be:
- opened, in which case it is fully usable
- closed or aborted, in which case it is no longer usable
type state=|OpenedThe file descriptor is opened
|ClosedThe file descriptor has been closed by
close. It must not be used for any operation.|Aborted of exnThe file descriptor has been aborted, the only operation possible is
close, all others will fail.State of a file descriptor
val state : file_descr -> statestate fdreturns the state offd
val unix_file_descr : file_descr -> Unix.file_descrReturns the underlying unix file descriptor. It always succeeds, even if the file descriptor's state is not
Open.
val of_unix_file_descr : ?blocking:bool -> ?set_flags:bool -> Unix.file_descr -> file_descrWraps a
Unixfile descriptorfdin anLwt_unix.file_descrfd'.~blockingcontrols the internal strategy Lwt uses to perform I/O on the underlyingfd. Regardless of~blocking, at the API level,Lwt_unix.read,Lwt_unix.write, etc. onfd'always block the Lwt thread, but never block the whole process. However, for performance reasons, it is important that~blockingmatch the actual blocking mode offd.If
~blockingis not specified,of_unix_file_descrchooses non-blocking mode for Unix sockets, Unix pipes, and Windows sockets, and blocking mode for everything else. Note: not specifying~blockingcausesfstatto be lazily called onfd, the first time your code performs I/O onfd'. Thisfstatcall can be expensive, so if you useof_unix_file_descra lot, be sure to specify~blockingexplicitly.of_unix_file_descrruns a system call to set the specified or chosen blocking mode on the underlyingfd.To prevent
of_unix_file_descrfrom running this system call, you can pass~set_flags:false. Note that, in this case, if~blocking, whether passed explicitly or chosen by Lwt, does not match the true blocking mode of the underlyingfd, I/O onfd'will suffer performance degradation.Note that
~set_flagsis effectively alwaysfalseif running on Windows andfdis not a socket.Generally, non-blocking I/O is faster: for blocking I/O, Lwt typically has to run system calls in worker threads to avoid blocking the process. See your system documentation for whether particular kinds of file descriptors support non-blocking I/O.
val blocking : file_descr -> bool Lwt.tblocking fdindicates whether Lwt is internally using blocking or non-blocking I/O withfd.Note that this may differ from the blocking mode of the underlying
Unixfile descriptor (i.e.unix_file_descr fd).See
of_unix_file_descrfor details.
val set_blocking : ?set_flags:bool -> file_descr -> bool -> unitset_blocking fd bcauses Lwt to internally use blocking or non-blocking I/O withfd, according to the value ofb.If
~set_flagsistrue(the default), Lwt also makes a system call to set the underlying file descriptor's blocking mode to match. Otherwise,set_blockingis only informational for Lwt.It is important that the underlying file descriptor actually have the same blocking mode as that indicated by
b.See
of_unix_file_descrfor details.
val abort : file_descr -> exn -> unitabort fd exnmakes all current and further uses of the file descriptor fail with the given exception. This put the file descriptor into theAbortedstate.If the file descriptor is closed, this does nothing, if it is aborted, this replace the abort exception by
exn.Note that this only works for reading and writing operations on file descriptors supporting non-blocking mode.
Process handling
val fork : unit -> intfork ()does the same asUnix.fork. You must use this function instead ofUnix.forkwhen you want to use Lwt in the child process.Notes:
- in the child process all pending jobs are canceled,
- if you are going to use Lwt in the parent and the child, it is a good idea to call
Lwt_io.flush_allbefore calllingforkto avoid double-flush. - otherwise, if you will not use Lwt in the child, call
Lwt_main.Exit_hooks.remove_allto avoid Lwt callingLwt_main.runduring process exit.
type process_status= Unix.process_status=|WEXITED of int|WSIGNALED of int|WSTOPPED of inttype wait_flag= Unix.wait_flag=|WNOHANG|WUNTRACED
val wait : unit -> (int * process_status) Lwt.tWrapper for
Unix.wait
val waitpid : wait_flag list -> int -> (int * process_status) Lwt.tA promise-returning analog to
Unix.waitpid. This call is non-blocking on Unix-like systems, but is always blocking on Windows.
type resource_usage={ru_utime : float;User time used
ru_stime : float;System time used
}Resource usages
val wait4 : wait_flag list -> int -> (int * process_status * resource_usage) Lwt.twait4 flags pidreturns(pid, status, rusage)where(pid, status)is the same result asUnix.waitpid flags pid, andrusagecontains accounting information about the child.On windows it will always returns
{ utime = 0.0; stime = 0.0 }.
val system : string -> process_status Lwt.tExecutes the given command, waits until it terminates, and return its termination status. The string is interpreted by the shell
/bin/shon Unix andcmd.exeon Windows. The resultWEXITED 127indicates that the shell couldn't be executed.
Basic file input/output
val stdin : file_descrThe standard file descriptor for input. This one is usually a terminal is the program is started from a terminal.
val stdout : file_descrThe standard file descriptor for output
val stderr : file_descrThe standard file descriptor for printing error messages
type file_perm= Unix.file_permtype open_flag= Unix.open_flag=|O_RDONLY|O_WRONLY|O_RDWR|O_NONBLOCK|O_APPEND|O_CREAT|O_TRUNC|O_EXCL|O_NOCTTY|O_DSYNC|O_SYNC|O_RSYNC|O_SHARE_DELETE|O_CLOEXEC|O_KEEPEXEC
val openfile : string -> open_flag list -> file_perm -> file_descr Lwt.tWrapper for
Unix.openfile.
val close : file_descr -> unit Lwt.tClose a file descriptor. This close the underlying unix file descriptor and set its state to
Closed.
val read : file_descr -> bytes -> int -> int -> int Lwt.tread fd buf ofs lenreads up tolenbytes fromfd, and writes them tobuf, starting at offsetofs. The function immediately evaluates to an Lwt thread, which waits for the operation to complete. If it completes successfully, the thread indicates the number of bytes actually read, or zero if the end of file has been reached.Note that the Lwt thread waits for data (or end of file) even if the underlying file descriptor is in non-blocking mode. See
of_unix_file_descrfor a discussion of non-blocking I/O and Lwt.If Lwt is using blocking I/O on
fd,readwrites data into a temporary buffer, then copies it intobuf.The thread can fail with any exception that can be raised by
Unix.read, exceptUnix.Unix_error Unix.EAGAIN,Unix.Unix_error Unix.EWOULDBLOCKorUnix.Unix_error Unix.EINTR.
val pread : file_descr -> bytes -> file_offset:int -> int -> int -> int Lwt.tpread fd buf ~file_offset ofs lenon file descriptors allowing seek, reads up tolenbytes fromfdat offsetfile_offsetfrom the beginning of the file, and writes them tobuf, starting at offsetofs.On Unix systems, the file descriptor position is unaffected. On Windows it is changed to be just after the last read position.
The thread can fail with any exception that can be raised by
readorlseek.
val write : file_descr -> bytes -> int -> int -> int Lwt.twrite fd buf ofs lenwrites up tolenbytes tofdfrombuf, starting at buffer offsetofs. The function immediately evaluates to an Lwt thread, which waits for the operation to complete. If the operation completes successfully, the thread indicates the number of bytes actually written, which may be less thanlen.Note that the Lwt thread waits to write even if the underlying file descriptor is in non-blocking mode. See
of_unix_file_descrfor a discussion of non-blocking I/O and Lwt.If Lwt is using blocking I/O on
fd,bufis copied before writing.The thread can fail with any exception that can be raised by
Unix.single_write, exceptUnix.Unix_error Unix.EAGAIN,Unix.Unix_error Unix.EWOULDBLOCKorUnix.Unix_error Unix.EINTR.
val pwrite : file_descr -> bytes -> file_offset:int -> int -> int -> int Lwt.tpwrite fd buf ~file_offset ofs lenon file descriptors allowing seek, writes up tolenbytes tofdfrombuf, starting at buffer offsetofs. The data is written at offsetfile_offsetfrom the beginning offd.On Unix systems, the file descriptor position is unaffected. On Windows it is changed to be just after the last written position.
The thread can fail with any exception that can be raised by
writeorlseek.
val write_string : file_descr -> string -> int -> int -> int Lwt.tSee
write.
val pwrite_string : file_descr -> string -> file_offset:int -> int -> int -> int Lwt.tSee
pwrite.
module IO_vectors : sig ... endSequences of buffer slices for
writev.
val readv : file_descr -> IO_vectors.t -> int Lwt.treadv fd vsreads bytes fromfdinto the buffer slicesvs. If the operation completes successfully, the resulting thread indicates the number of bytes read.Data is always read directly into
Bigarrayslices. If the Unix file descriptor underlyingfdis in non-blocking mode, data is also read directly intobytesslices. Otherwise, data forbytesslices is first read into temporary buffers, then copied.Note that the returned Lwt thread is blocked until failure or a successful read, even if the underlying file descriptor is in non-blocking mode. See
of_unix_file_descrfor a discussion of non-blocking I/O and Lwt.If
IO_vectors.system_limitisSome nand the count of slices invsexceedsn, thenLwt_unix.readvreads only into the firstnslices ofvs.Not implemented on Windows. It should be possible to implement, upon request, for Windows sockets only.
See
readv(3p).- since
- 2.7.0
val writev : file_descr -> IO_vectors.t -> int Lwt.twritev fd vswrites the bytes in the buffer slicesvsto the file descriptorfd. If the operation completes successfully, the resulting thread indicates the number of bytes written.If the Unix file descriptor underlying
fdis in non-blocking mode,writevdoes not make a copy the bytes before writing. Otherwise, it copiesbytesslices, but notBigarrayslices.Note that the returned Lwt thread is blocked until failure or a successful write, even if the underlying descriptor is in non-blocking mode. See
of_unix_file_descrfor a discussion of non-blocking I/O and Lwt.If
IO_vectors.system_limitisSome nand the count of slices invsexceedsn, thenLwt_unix.writevpasses only the firstnslices invsto the underlyingwritevsystem call.Not implemented on Windows. It should be possible to implement, upon request, for Windows sockets only.
The behavior of
writevwhenvshas zero slices depends on the system, and may change in future versions of Lwt. On Linux,writevwill succeed and write zero bytes. On BSD (including macOS),writevwill fail withUnix.Unix_error (Unix.EINVAL, "writev", ...).See
writev(3p).- since
- 2.7.0
val readable : file_descr -> boolReturns whether the given file descriptor is currently readable.
val writable : file_descr -> boolReturns whether the given file descriptor is currently writable.
val wait_read : file_descr -> unit Lwt.tWaits (without blocking other threads) until there is something to read from the file descriptor.
Note that you don't need to use this function if you are using Lwt I/O functions for reading, since they provide non-blocking waiting automatically.
The intended use case for this function is interfacing with existing libraries that are known to be blocking.
val wait_write : file_descr -> unit Lwt.tWaits (without blocking other threads) until it is possible to write on the file descriptor.
Note that you don't need to use this function if you are using Lwt I/O functions for writing, since they provide non-blocking waiting automatically.
The intended use case for this function is interfacing with existing libraries that are known to be blocking.
Seeking and truncating
val lseek : file_descr -> int -> seek_command -> int Lwt.tWrapper for
Unix.lseek
val truncate : string -> int -> unit Lwt.tWrapper for
Unix.truncate
val ftruncate : file_descr -> int -> unit Lwt.tWrapper for
Unix.ftruncate
Syncing
val fsync : file_descr -> unit Lwt.tSynchronise all data and metadata of the file descriptor with the disk. On Windows it uses
FlushFileBuffers.
val fdatasync : file_descr -> unit Lwt.tSynchronise all data (but not metadata) of the file descriptor with the disk.
Note that
fdatasyncis not available on Windows and OS X.
File status
type file_kind= Unix.file_kind=|S_REG|S_DIR|S_CHR|S_BLK|S_LNK|S_FIFO|S_SOCKtype stats= Unix.stats={st_dev : int;st_ino : int;st_kind : file_kind;st_perm : file_perm;st_nlink : int;st_uid : int;st_gid : int;st_rdev : int;st_size : int;st_atime : float;st_mtime : float;st_ctime : float;}
val fstat : file_descr -> stats Lwt.tWrapper for
Unix.fstat
val file_exists : string -> bool Lwt.tfile_exists nametests if a file namednameexists.Note that
file_existsbehaves similarly toSys.file_exists:- "file" is interpreted as "directory entry" in this context
file_exists namewill returnfalsein circumstances that would makestatraise aUnix.Unix_errorexception.
val utimes : string -> float -> float -> unit Lwt.tutimes path atime mtimeupdates the access and modification times of the file atpath. The access time is set toatimeand the modification time tomtime. To set both to the current time, callutimes path 0. 0..This function corresponds to
Unix.utimes. See alsoutimes(3p).- since
- 2.6.0
val isatty : file_descr -> bool Lwt.tWrapper for
Unix.isatty
File operations on large files
module LargeFile : sig ... endOperations on file names
val unlink : string -> unit Lwt.tWrapper for
Unix.unlink
val rename : string -> string -> unit Lwt.tWrapper for
Unix.rename
val link : string -> string -> unit Lwt.tWrapper for
Unix.link
File permissions and ownership
val fchmod : file_descr -> file_perm -> unit Lwt.tWrapper for
Unix.fchmod
val chown : string -> int -> int -> unit Lwt.tWrapper for
Unix.chown
val fchown : file_descr -> int -> int -> unit Lwt.tWrapper for
Unix.fchown
val access : string -> access_permission list -> unit Lwt.tWrapper for
Unix.access
Operations on file descriptors
val dup : file_descr -> file_descrWrapper for
Unix.dup
val dup2 : file_descr -> file_descr -> unitWrapper for
Unix.dup2
val set_close_on_exec : file_descr -> unitWrapper for
Unix.set_close_on_exec
val clear_close_on_exec : file_descr -> unitWrapper for
Unix.clear_close_on_exec
Directories
val rmdir : string -> unit Lwt.tWrapper for
Unix.rmdir
val chdir : string -> unit Lwt.tWrapper for
Unix.chdir
val getcwd : unit -> string Lwt.tWrapper for
Unix.getcwd- since
- 3.1.0
val chroot : string -> unit Lwt.tWrapper for
Unix.chroot
val opendir : string -> dir_handle Lwt.tOpens a directory for listing. Directories opened with this function must be explicitly closed with
closedir. This is a cooperative analog ofUnix.opendir.
val readdir : dir_handle -> string Lwt.tReads the next directory entry from the given directory. Special entries such as
.and..are included. If all entries have been read, raisesEnd_of_file. This is a cooperative analog ofUnix.readdir.
val readdir_n : dir_handle -> int -> string array Lwt.treaddir_n handle countreads at mostcountentries from the given directory. It is more efficient than callingreaddircounttimes. If the length of the returned array is smaller thancount, this means that the end of the directory has been reached.
val rewinddir : dir_handle -> unit Lwt.tResets the given directory handle, so that directory listing can be restarted. Cooperative analog of
Unix.rewinddir.
val closedir : dir_handle -> unit Lwt.tCloses a directory handle. Cooperative analog of
Unix.closedir.
val files_of_directory : string -> string Lwt_stream.tfiles_of_directory dirreturns the stream of all files ofdir.
Pipes and redirections
val pipe : unit -> file_descr * file_descrpipe ()creates pipe usingUnix.pipeand returns two lwt file descriptors created from unix file_descriptor
val pipe_in : unit -> file_descr * Unix.file_descrpipe_in ()is the same aspipebut maps only the unix file descriptor for reading into a lwt one. The second is not put into non-blocking mode. You usually want to use this before forking to receive data from the child process.
val pipe_out : unit -> Unix.file_descr * file_descrpipe_out ()is the inverse ofpipe_in. You usually want to use this before forking to send data to the child process
Symbolic links
val symlink : string -> string -> unit Lwt.tWrapper for
Unix.symlink
val readlink : string -> string Lwt.tWrapper for
Unix.readlink
Locking
val lockf : file_descr -> lock_command -> int -> unit Lwt.tWrapper for
Unix.lockf
User id, group id
type passwd_entry= Unix.passwd_entry={pw_name : string;pw_passwd : string;pw_uid : int;pw_gid : int;pw_gecos : string;pw_dir : string;pw_shell : string;}type group_entry= Unix.group_entry={gr_name : string;gr_passwd : string;gr_gid : int;gr_mem : string array;}
val getlogin : unit -> string Lwt.tWrapper for
Unix.getlogin
val getpwnam : string -> passwd_entry Lwt.tWrapper for
Unix.getpwnam
val getgrnam : string -> group_entry Lwt.tWrapper for
Unix.getgrnam
val getpwuid : int -> passwd_entry Lwt.tWrapper for
Unix.getpwuid
val getgrgid : int -> group_entry Lwt.tWrapper for
Unix.getgrgid
Signals
val on_signal : int -> (int -> unit) -> signal_handler_idon_signal signum fcallsfeach time the signal with numnbersignumis received by the process. It returns a signal handler identifier that can be used to stop monitoringsignum.
val on_signal_full : int -> (signal_handler_id -> int -> unit) -> signal_handler_idon_signal_full fis the same ason_signal fexcept thatfalso receive the signal handler identifier as argument so it can disable it.
val disable_signal_handler : signal_handler_id -> unitStops receiving this signal
val reinstall_signal_handler : int -> unitreinstall_signal_handler signumif any signal handler is registered for this signal withon_signal, it reinstall the signal handler (withSys.set_signal). This is useful in case another part of the program install another signal handler.
Sockets
type inet_addr= Unix.inet_addrtype socket_domain= Unix.socket_domain=|PF_UNIX|PF_INET|PF_INET6type socket_type= Unix.socket_type=|SOCK_STREAM|SOCK_DGRAM|SOCK_RAW|SOCK_SEQPACKETtype sockaddr= Unix.sockaddr=|ADDR_UNIX of string|ADDR_INET of inet_addr * int
val socket : socket_domain -> socket_type -> int -> file_descrsocket domain type protois the same asUnix.socketbut maps the result into a lwt file descriptor
val socketpair : socket_domain -> socket_type -> int -> file_descr * file_descrWrapper for
Unix.socketpair
val bind : file_descr -> sockaddr -> unit Lwt.tBinds an address to the given socket. This is the cooperative analog of
Unix.bind. See alsobind(3p).- since
- 3.0.0
val listen : file_descr -> int -> unitWrapper for
Unix.listen
val accept : file_descr -> (file_descr * sockaddr) Lwt.tWrapper for
Unix.accept
val accept_n : file_descr -> int -> ((file_descr * sockaddr) list * exn option) Lwt.taccept_n fd countaccepts up tocountconnections at one time.- if no connection is available right now, it returns a sleeping thread
- if more than 1 and less than
countare available, it returns all of them
- if more than
countare available, it returns the nextcountof them
- if an error happens, it returns the connections that have been successfully accepted so far and the error
accept_nhas the advantage of improving performance. If you want a more detailed description, you can have a look at:
val connect : file_descr -> sockaddr -> unit Lwt.tWrapper for
Unix.connect
val shutdown : file_descr -> shutdown_command -> unitWrapper for
Unix.shutdown
val getsockname : file_descr -> sockaddrWrapper for
Unix.getsockname
val getpeername : file_descr -> sockaddrWrapper for
Unix.getpeername
val recv : file_descr -> bytes -> int -> int -> msg_flag list -> int Lwt.tWrapper for
Unix.recv.On Windows,
recvwrites data into a temporary buffer, then copies it into the given one.
val recvfrom : file_descr -> bytes -> int -> int -> msg_flag list -> (int * sockaddr) Lwt.tWrapper for
Unix.recvfrom.On Windows,
recvfromwrites data into a temporary buffer, then copies it into the given one.
val send : file_descr -> bytes -> int -> int -> msg_flag list -> int Lwt.tWrapper for
Unix.send.On Windows,
sendcopies the given buffer before writing.
val sendto : file_descr -> bytes -> int -> int -> msg_flag list -> sockaddr -> int Lwt.tWrapper for
Unix.sendto.On Windows,
sendtocopies the given buffer before writing.
val recv_msg : socket:file_descr -> io_vectors:IO_vectors.t -> (int * Unix.file_descr list) Lwt.trecv_msg ~socket ~io_vectorsreceives data into a list of io-vectors, plus any file-descriptors that may accompany the messages. It returns a tuple whose first field is the number of bytes received and second is a list of received file descriptors. The messages themselves will be recorded in the providedio_vectorslist. Data is written directly into theiov_bufferbuffers.Not implemented on Windows.
- since
- 5.0.0
val send_msg : socket:file_descr -> io_vectors:IO_vectors.t -> fds:Unix.file_descr list -> int Lwt.tsend_msg ~socket ~io_vectors ~fdssends data from a list of io-vectors, accompanied with a list of file-descriptors. It returns the number of bytes sent. If fd-passing is not possible on the current system andfdsis not empty, it raisesLwt_sys.Not_available "fd_passing". Data is written directly from theio_vectorsbuffers.Not implemented on Windows.
- since
- 5.0.0
val get_credentials : file_descr -> credentialsget_credentials fdreturns credentials information from the given socket. On some platforms, obtaining the peer pid is not possible and it will be set to-1. If obtaining credentials is not possible on the current system, it raisesLwt_sys.Not_available "get_credentials".This call is not available on windows.
Socket options
type socket_bool_option= Unix.socket_bool_option=|SO_DEBUG|SO_BROADCAST|SO_REUSEADDR|SO_KEEPALIVE|SO_DONTROUTE|SO_OOBINLINE|SO_ACCEPTCONN|TCP_NODELAY|IPV6_ONLYtype socket_int_option= Unix.socket_int_option=|SO_SNDBUF|SO_RCVBUF|SO_ERROR|SO_TYPE|SO_RCVLOWAT|SO_SNDLOWATtype socket_optint_option= Unix.socket_optint_option=|SO_LINGERtype socket_float_option= Unix.socket_float_option=|SO_RCVTIMEO|SO_SNDTIMEONote: these options are provided for the sake of completeness only. Lwt places all sockets in non-blocking mode, for which these options are meaningless. Use
Lwt.pickwithLwt_unix.sleeporLwt_unix.timeoutfor timeouts.
val getsockopt : file_descr -> socket_bool_option -> boolWrapper for
Unix.getsockopt
val setsockopt : file_descr -> socket_bool_option -> bool -> unitWrapper for
Unix.setsockopt
val getsockopt_int : file_descr -> socket_int_option -> intWrapper for
Unix.getsockopt_int
val setsockopt_int : file_descr -> socket_int_option -> int -> unitWrapper for
Unix.setsockopt_int
val getsockopt_optint : file_descr -> socket_optint_option -> int optionWrapper for
Unix.getsockopt_optint
val setsockopt_optint : file_descr -> socket_optint_option -> int option -> unitWrapper for
Unix.setsockopt_optint
val getsockopt_float : file_descr -> socket_float_option -> floatWrapper for
Unix.getsockopt_float
val setsockopt_float : file_descr -> socket_float_option -> float -> unitWrapper for
Unix.setsockopt_float
val getsockopt_error : file_descr -> Unix.error optionWrapper for
Unix.getsockopt_error
Multicast functions
val mcast_set_loop : file_descr -> bool -> unitWhether sent multicast messages are received by the sending host
val mcast_set_ttl : file_descr -> int -> unitSet TTL/hops value
val mcast_add_membership : file_descr -> ?ifname:Unix.inet_addr -> Unix.inet_addr -> unitmcast_add_membership fd ~ifname addrjoins the multicast groupaddron the network interfaceifname.
val mcast_drop_membership : file_descr -> ?ifname:Unix.inet_addr -> Unix.inet_addr -> unitmcast_drop_membership fd ~ifname addrleaves the multicast groupaddron the network interfaceifname.
Host and protocol databases
type host_entry= Unix.host_entry={h_name : string;h_aliases : string array;h_addrtype : socket_domain;h_addr_list : inet_addr array;}type protocol_entry= Unix.protocol_entry={p_name : string;p_aliases : string array;p_proto : int;}type service_entry= Unix.service_entry={s_name : string;s_aliases : string array;s_port : int;s_proto : string;}
val gethostname : unit -> string Lwt.tWrapper for
Unix.gethostname
val gethostbyname : string -> host_entry Lwt.tWrapper for
Unix.gethostbyname
val gethostbyaddr : inet_addr -> host_entry Lwt.tWrapper for
Unix.gethostbyaddr
val getprotobyname : string -> protocol_entry Lwt.tWrapper for
Unix.getprotobyname
val getprotobynumber : int -> protocol_entry Lwt.tWrapper for
Unix.getprotobynumber
val getservbyname : string -> string -> service_entry Lwt.tWrapper for
Unix.getservbyname
val getservbyport : int -> string -> service_entry Lwt.tWrapper for
Unix.getservbyport
type addr_info= Unix.addr_info={ai_family : socket_domain;ai_socktype : socket_type;ai_protocol : int;ai_addr : sockaddr;ai_canonname : string;}type getaddrinfo_option= Unix.getaddrinfo_option=|AI_FAMILY of socket_domain|AI_SOCKTYPE of socket_type|AI_PROTOCOL of int|AI_NUMERICHOST|AI_CANONNAME|AI_PASSIVE
val getaddrinfo : string -> string -> getaddrinfo_option list -> addr_info list Lwt.tWrapper for
Unix.getaddrinfo
type name_info= Unix.name_info={ni_hostname : string;ni_service : string;}type getnameinfo_option= Unix.getnameinfo_option=|NI_NOFQDN|NI_NUMERICHOST|NI_NAMEREQD|NI_NUMERICSERV|NI_DGRAM
val getnameinfo : sockaddr -> getnameinfo_option list -> name_info Lwt.tWrapper for
Unix.getnameinfo
Terminal interface
type terminal_io= Unix.terminal_io={}
val tcgetattr : file_descr -> terminal_io Lwt.tWrapper for
Unix.tcgetattr
val tcsetattr : file_descr -> setattr_when -> terminal_io -> unit Lwt.tWrapper for
Unix.tcsetattr
val tcsendbreak : file_descr -> int -> unit Lwt.tWrapper for
Unix.tcsendbreak
val tcdrain : file_descr -> unit Lwt.tWrapper for
Unix.tcdrain
val tcflush : file_descr -> flush_queue -> unit Lwt.tWrapper for
Unix.tcflush
val tcflow : file_descr -> flow_action -> unit Lwt.tWrapper for
Unix.tcflow
Configuration (deprecated)
type async_method=For system calls that cannot be made asynchronously, Lwt uses one of the following method:
val default_async_method : unit -> async_methodReturns the default async method.
This can be initialized using the environment variable
"LWT_ASYNC_METHOD"with possible values"none","detach"and"switch".- deprecated
Will always return
Async_detachin Lwt 5.0.0.
val set_default_async_method : async_method -> unitSets the default async method.
- deprecated
Will be a no-op in Lwt 5.0.0.
val async_method : unit -> async_methodasync_method ()returns the async method used in the current thread.- deprecated
Will always return
Async_detachin Lwt 5.0.0.
val async_method_key : async_method Lwt.keyThe key for storing the local async method.
- deprecated
Will be ignored in Lwt 5.0.0.
val with_async_none : (unit -> 'a) -> 'awith_async_none fis a shorthand for:Lwt.with_value async_method_key (Some Async_none) f- deprecated
Will have no effect in Lwt 5.0.0.
Low-level interaction
exceptionRetryIf an action raises
Retry, it will be requeued until the file descriptor becomes readable/writable again.
exceptionRetry_readIf an action raises
Retry_read, it will be requeued until the file descriptor becomes readable.
exceptionRetry_writeIf an action raises
Retry_read, it will be requeued until the file descriptor becomes writables.
val wrap_syscall : io_event -> file_descr -> (unit -> 'a) -> 'a Lwt.twrap_syscall set fd actionwrap an action on a file descriptor. It tries to execute action, and if it can not be performed immediately without blocking, it is registered for later.In the latter case, if the thread is canceled,
actionis removed fromset.
val check_descriptor : file_descr -> unitcheck_descriptor fdraise an exception iffdis not in the stateOpen.
val register_action : io_event -> file_descr -> (unit -> 'a) -> 'a Lwt.tregister_action set fd actionregistersactiononfd. Whenfdbecomesreadable/writableactionis called.Note:
- you must call
check_descriptor fdbefore callingregister_action
- you should prefer using
wrap_syscall
- you must call
type 'a jobType of job descriptions. A job description describe how to call a C function and how to get its result. The C function may be executed in another system thread.
val run_job : ?async_method:async_method -> 'a job -> 'a Lwt.trun_job ?async_method jobstartsjoband wait for its termination.The
~async_methodargument will be ignored in Lwt 5.0.0, and this function will always act as if~async_method:Async_detachis passed.The async method is chosen follow:
- if the optional parameter
async_methodis specified, it is used, - otherwise if the local key
async_method_keyis set in the current thread, it is used, - otherwise the default method (returned by
default_async_method) is used.
If the method is
Async_nonethen the job is run synchronously and may block the current system thread, thus blocking all Lwt threads.If the method is
Async_detachthen the job is run in another system thread, unless the the maximum number of worker threads has been reached (as given bypool_size).If the method is
Async_switchthen the job is run synchronously and if it blocks, execution will continue in another system thread (unless the limit is reached).- if the optional parameter
val abort_jobs : exn -> unitabort_jobs exnmake all pending jobs to fail with exn. Note that this does not abort the real job (i.e. the C function executing it), just the lwt thread for it.
val wait_for_jobs : unit -> unit Lwt.tWait for all pending jobs to terminate.
val execute_job : ?async_method:async_method -> job:'a job -> result:('a job -> 'b) -> free:('a job -> unit) -> 'b Lwt.t- deprecated
Use
run_job.
Notifications
val make_notification : ?once:bool -> (unit -> unit) -> intnew_notifier ?once fregisters a new notifier. It returns the id of the notifier. Each time a notification with this id is received,fis called.if
onceis specified, then the notification is stopped after the first time it is received. It defaults tofalse.
val send_notification : int -> unitsend_notification idsends a notification.This function is thread-safe.
val stop_notification : int -> unitStop the given notification. Note that you should not reuse the id after the notification has been stopped, the result is unspecified if you do so.
System threads pool
CPUs
Versioned interfaces
module Versioned : sig ... endVersioned variants of APIs undergoing breaking changes.