Cooperative system calls
This modules maps system calls, like those of the standard
library's Unix
module, to cooperative ones, which will not block
the program.
The semantics of all operations is the following: if the action (for example reading from a file descriptor) can be performed immediately, it is done and returns immediately, otherwise it returns a sleeping thread which is woken up when the operation completes.
Most operations on sockets and pipes (on Windows it is only sockets) are cancelable, meaning you can cancel them with Lwt.cancel. For example if you want to read something from a file descriptor with a timeout, you can cancel the action after the timeout and the reading will not be performed if not already done.
For example, consider that you have two sockets sock1
and
sock2
. You want to read something from sock1
or exclusively
from sock2
and fail with an exception if a timeout of 1 second
expires, without reading anything from sock1
and sock2
, even
if they become readable in the future.
Then you can do:
Lwt.pick [Lwt_unix.timeout 1.0; read sock1 buf1 ofs1 len1; read sock2 buf2 ofs2 len2]
In this case, it is guaranteed that exactly one of the three operations will complete, and the others will be cancelled.
#
| Async_none
| (* | System calls are made synchronously, and may block the entire program. | *) |
#
| Async_detach
| (* | System calls are made in another system thread, thus without
blocking other Lwt threads. The drawback is that it may
degrade performances in some cases. This is the default. | *) |
#
| Async_switch
| (* | System calls are made in the main thread, and if one blocks
the execution continue in another system thread. This method
is the most efficient, also you will get better performance
if you force all threads to run on the same cpu. On linux
this can be done by using the command taskset .Note that this method is still experimental. | *) |
For system calls that cannot be made asynchronously, Lwt uses one of the following method:
Returns the default async method.
This can be initialized using the environment variable
"LWT_ASYNC_METHOD"
with possible values "none"
,
"detach"
and "switch"
.
async_method ()
returns the async method used in the current
thread.
with_async_none f
is a shorthand for:
Lwt.with_value async_method_key (Some Async_none) f
with_async_none f
is a shorthand for:
Lwt.with_value async_method_key (Some Async_detach) f
with_async_none f
is a shorthand for:
Lwt.with_value async_method_key (Some Async_switch) f
Exception raised by timeout operations
The 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
| (* | The file descriptor is opened | *) |
#
| Closed
| (* | The file descriptor has been closed by close. It must not be used for any operation. | *) |
#
| Aborted of exn
| (* | The file descriptor has been aborted, the only operation possible is close, all others will fail. | *) |
State of a file descriptor
Returns the underlying unix file descriptor. It always succeeds, even if the file descriptor's state is not [root:Open].
Creates a lwt file descriptor from a unix one.
blocking
is the blocking mode of the file-descriptor, and
describes how Lwt will use it. In non-blocking mode, read/write
on this file descriptor are made using non-blocking IO; in
blocking mode they are made using the current async method. If
blocking
is not specified it is guessed according to the file
kind: socket and pipes are in non-blocking mode and others are
in blocking mode.
If set_flags
is true
(the default) then the file flags are
modified according to the blocking
argument, otherwise they
are left unchanged.
Note that the blocking mode is less efficient than the non-blocking one, so it should be used only for file descriptors that does not support asynchronous operations, such as regular files, or for shared descriptors such as stdout, stderr or stdin.
blocking fd
returns whether fd
is used in blocking or
non-blocking mode.
set_blocking fd b
puts fd
in blocking or non-blocking
mode. If set_flags
is true
(the default) then the file flags
are modified, otherwise the modification is only done at the
application level.
abort fd exn
makes all current and further uses of the file
descriptor fail with the given exception. This put the file
descriptor into the Aborted state.
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.
fork ()
does the same as Unix.fork
. You must use this
function instead of Unix.fork
when you want to use Lwt in the
child process.
Notes:
#
| WEXITED of int
| |||
#
| WSIGNALED of int
| |||
#
| WSTOPPED of int
|
wait4 flags pid
returns (pid, status, rusage)
where (pid,
status)
is the same result as Unix.waitpid flags pid
, and
rusage
contains accounting information about the child.
On windows it will always returns { utime = 0.0; stime = 0.0 }
.
Returns the number of threads waiting for a child to terminate.
Executes the given command, waits until it terminates, and
return its termination status. The string is interpreted by the
shell /bin/sh
on Unix and cmd.exe
on Windows. The result
WEXITED 127
indicates that the shell couldn't be executed.
The standard file descriptor for input. This one is usually a terminal is the program is started from a terminal.
Close a file descriptor. This close the underlying unix file descriptor and set its state to Closed
read fd buf ofs len
has the same semantic as Unix.read
, but
is cooperative
read fd buf ofs len
has the same semantic as Unix.write
, but
is cooperative
waits (without blocking other threads) until there is something to read on the file descriptor
waits (without blocking other threads) until it is possible to write on the file descriptor
Synchronise all data and metadata of the file descriptor with
the disk. On Windows it uses FlushFileBuffers
.
Synchronise all data (but not metadata) of the file descriptor with the disk.
Note that fdatasync
is not available on all platforms.
readdir_n handle count
reads at most count
entry from the
given directory. It is more efficient than calling readdir
count
times. If the length of the returned array is smaller
than count
, this means that the end of the directory has been
reached.
files_of_directory dir
returns the stream of all files of
dir
.
pipe ()
creates pipe using Unix.pipe
and returns two lwt file descriptors created from unix file_descriptor
pipe_in ()
is the same as pipe but 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.
pipe_out ()
is the inverse of pipe_in. You usually want to
use this before forking to send data to the child process
Id of a signal handler, used to cancel it
on_signal signum f
calls f
each time the signal with numnber
signum
is received by the process. It returns a signal handler
identifier that can be used to stop monitoring signum
.
on_signal_full f
is the same as on_signal f
except that f
also receive the signal handler identifier as argument so it can
disable it.
Returns the number of registered signal handler.
reinstall_signal_handler signum
if any signal handler is
registered for this signal with on_signal, it reinstall the
signal handler (with Sys.set_signal
). This is usefull in case
another part of the program install another signal handler.
socket domain type proto
is the same as Unix.socket
but maps
the result into a lwt file descriptor
Wrapper for Unix.socketpair
accept_n fd count
accepts up to count
connections at one time.
count
are available, it returns
all of themcount
are available, it returns the next
count
of themaccept_n
has the advantage of improving performance. If you
want a more detailed description, you can have a look at:
#
| SHUTDOWN_RECEIVE
| |||
#
| SHUTDOWN_SEND
| |||
#
| SHUTDOWN_ALL
|
Wrapper for Unix.recvfrom
Wrapper for Unix.sendto
recv_msg ~socket ~io_vectors
receives 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
provided io_vectors
list.
This call is not available on windows.
send_msg ~socket ~io_vectors ~fds
sends 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 and fds
is not empty, it raises
Lwt_sys.Not_available "fd_passing"
.
This call is not available on windows.
get_credentials fd
returns credential informations 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 raises
Lwt_sys.Not_available "get_credentials"
.
This call is not available on windows.
Wrapper for Unix.getsockopt_optint
Wrapper for Unix.setsockopt_optint
Wrapper for Unix.setsockopt_float
#
h_name
| : string | ; | |||
#
h_aliases
| : string array | ; | |||
#
h_addrtype
| : socket_domain | ; | |||
#
h_addr_list
| : inet_addr array | ; |
#
p_name
| : string | ; | |||
#
p_aliases
| : string array | ; | |||
#
p_proto
| : int | ; |
#
ai_family
| : socket_domain | ; | |||
#
ai_socktype
| : socket_type | ; | |||
#
ai_protocol
| : int | ; | |||
#
ai_addr
| : sockaddr | ; | |||
#
ai_canonname
| : string | ; |
Wrapper for Unix.getaddrinfo
Wrapper for Unix.getnameinfo
#
mutable c_ignbrk
| : bool | ; | |||
#
mutable c_brkint
| : bool | ; | |||
#
mutable c_ignpar
| : bool | ; | |||
#
mutable c_parmrk
| : bool | ; | |||
#
mutable c_inpck
| : bool | ; | |||
#
mutable c_istrip
| : bool | ; | |||
#
mutable c_inlcr
| : bool | ; | |||
#
mutable c_igncr
| : bool | ; | |||
#
mutable c_icrnl
| : bool | ; | |||
#
mutable c_ixon
| : bool | ; | |||
#
mutable c_ixoff
| : bool | ; | |||
#
mutable c_opost
| : bool | ; | |||
#
mutable c_obaud
| : int | ; | |||
#
mutable c_ibaud
| : int | ; | |||
#
mutable c_csize
| : int | ; | |||
#
mutable c_cstopb
| : int | ; | |||
#
mutable c_cread
| : bool | ; | |||
#
mutable c_parenb
| : bool | ; | |||
#
mutable c_parodd
| : bool | ; | |||
#
mutable c_hupcl
| : bool | ; | |||
#
mutable c_clocal
| : bool | ; | |||
#
mutable c_isig
| : bool | ; | |||
#
mutable c_icanon
| : bool | ; | |||
#
mutable c_noflsh
| : bool | ; | |||
#
mutable c_echo
| : bool | ; | |||
#
mutable c_echoe
| : bool | ; | |||
#
mutable c_echok
| : bool | ; | |||
#
mutable c_echonl
| : bool | ; | |||
#
mutable c_vintr
| : char | ; | |||
#
mutable c_vquit
| : char | ; | |||
#
mutable c_verase
| : char | ; | |||
#
mutable c_vkill
| : char | ; | |||
#
mutable c_veof
| : char | ; | |||
#
mutable c_veol
| : char | ; | |||
#
mutable c_vmin
| : int | ; | |||
#
mutable c_vtime
| : int | ; | |||
#
mutable c_vstart
| : char | ; | |||
#
mutable c_vstop
| : char | ; |
If an action raises Retry, it will be requeued until the file descriptor becomes readable/writable again.
If an action raises Retry_read, it will be requeued until the file descriptor becomes readable.
If an action raises Retry_read, it will be requeued until the file descriptor becomes writables.
wrap_syscall set fd action
wrap an action on a file
descriptor. It tries to execture action, and if it can not be
performed immediately without blocking, it is registered for
latter.
In the latter case, if the thread is canceled, action
is
removed from set
.
check_descriptor fd
raise an exception if fd
is not in the
state [root:Open]
register_action set fd action
registers action
on fd
. When
fd
becomes readable
/writable
action
is called.
Note:
check_descriptor fd
before calling
register_action
Type 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.
run_job ?async_method job
starts job
and wait for its
termination.
The async method is choosen follow:
async_method
is specified, it is
used,If the method is Async_none then the job is run synchronously and may block the current system thread, thus blocking all Lwt threads.
If the method is Async_detach then the job is run in another system thread, unless the the maximum number of worker threads has been reached (as given by pool_size).
If the method is Async_switch then the job is run synchronously and if it blocks, execution will continue in another system thread (unless the limit is reached).
abort_jobs exn
make 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.
cancel_jobs ()
is the same as abort_jobs Lwt.Canceled
.
Lwt internally use a pipe to send notification to the main thread. The following functions allow to use this pipe.
new_notifier ?once f
registers a new notifier. It returns the
id of the notifier. Each time a notification with this id is
received, f
is called.
if once
is specified, then the notification is stopped after
the first time it is received. It defaults to false
.
send_notification id
sends a notification.
This function is thread-safe.
Stop 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.
Call the handler associated to the given notification. Note that
if the notification was defined with once = true
it is removed.
set_notification id f
replace the function associated to the
notification by f
. It raises Not_found
if the given
notification is not found.
If the program is using the async method Async_detach or Async_switch, Lwt will launch system threads to execute blocking system calls asynchronously.
Maximum number of system threads that can be started. If this limit is reached, jobs will be executed synchronously.
Change the size of the pool.
The number of system threads running (excluding this one).
The number threads waiting for a job.
get_cpu ()
returns the number of the CPU the current thread is
running on.
get_affinity ?pid ()
returns the list of CPUs the process with
pid pid
is allowed to run on. If pid
is not specified then
the affinity of the current process is returned.
set_affinity ?pid cpus
sets the list of CPUs the given process
is allowed to run on.