Skip to content

Commit 3063389

Browse files
committed
Add couple of examples of using Control.terminate_after
1 parent 9a87292 commit 3063389

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/picos_structured/picos_structured.mli

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ module Control : sig
172172
(** [terminate_after ~seconds thunk] arranges to terminate the execution of
173173
[thunk] on the current fiber after the specified timeout in [seconds].
174174
175+
Using [terminate_after] one can conveniently attempt any blocking
176+
operation that supports cancelation with a timeout. For example, one can
177+
perform a timed {{!Picos_sync.Ivar.read} [Ivar.read]}
178+
179+
{[
180+
let peek_in ~seconds ivar =
181+
match
182+
Control.terminate_after ~seconds @@ fun () ->
183+
Ivar.read ivar
184+
with
185+
| value -> Some value
186+
| exception Control.Terminate -> None
187+
]}
188+
189+
or one could try to {{!Picos_stdio.Unix.connect} [connect]} to a socket
190+
with a timeout
191+
192+
{[
193+
let try_connect_in ~seconds socket sockaddr =
194+
match
195+
Control.terminate_after ~seconds @@ fun () ->
196+
Unix.connect socket sockaddr
197+
with
198+
| () -> true
199+
| exception Control.Terminate -> false
200+
]}
201+
202+
using the {!Picos_stdio.Unix} module.
203+
175204
The optional [callstack] argument specifies the number of callstack
176205
entries to capture with the {{!Control.Terminate} [Terminate]} exception.
177206
The default is [0].

0 commit comments

Comments
 (0)