<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>System.Timeout</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ window.onload = function () {pageLoad();setSynopsis("mini_System-Timeout.html");}; //]]> </script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">base-4.5.1.0: Basic libraries</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>non-portable</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr><tr><th>Safe Haskell</th><td>Trustworthy</td></tr></table><p class="caption">System.Timeout</p></div><div id="description"><p class="caption">Description</p><div class="doc"><p>Attach a timeout event to arbitrary <code><a href="System-IO.html#t:IO">IO</a></code> computations. </p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><a href="#v:timeout">timeout</a> :: <a href="Data-Int.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Maybe.html#t:Maybe">Maybe</a> a)</li></ul></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><a name="v:timeout" class="def">timeout</a> :: <a href="Data-Int.html#t:Int">Int</a> -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Maybe.html#t:Maybe">Maybe</a> a)</p><div class="doc"><p>Wrap an <code><a href="System-IO.html#t:IO">IO</a></code> computation to time out and return <code>Nothing</code> in case no result is available within <code>n</code> microseconds (<code>1/10^6</code> seconds). In case a result is available before the timeout expires, <code>Just a</code> is returned. A negative timeout interval means "wait indefinitely". When specifying long timeouts, be careful not to exceed <code>maxBound :: Int</code>. </p><p>The design of this combinator was guided by the objective that <code>timeout n f</code> should behave exactly the same as <code>f</code> as long as <code>f</code> doesn't time out. This means that <code>f</code> has the same <code><a href="Control-Concurrent.html#v:myThreadId">myThreadId</a></code> it would have without the timeout wrapper. Any exceptions <code>f</code> might throw cancel the timeout and propagate further up. It also possible for <code>f</code> to receive exceptions thrown to it by another thread. </p><p>A tricky implementation detail is the question of how to abort an <code>IO</code> computation. This combinator relies on asynchronous exceptions internally. The technique works very well for computations executing inside of the Haskell runtime system, but it doesn't work at all for non-Haskell code. Foreign function calls, for example, cannot be timed out with this combinator simply because an arbitrary C function cannot receive asynchronous exceptions. When <code>timeout</code> is used to wrap an FFI call that blocks, no timeout event can be delivered until the FFI call returns, which pretty much negates the purpose of the combinator. In practice, however, this limitation is less severe than it may sound. Standard I/O functions like <code><a href="System-IO.html#v:hGetBuf">hGetBuf</a></code>, <code><a href="System-IO.html#v:hPutBuf">hPutBuf</a></code>, Network.Socket.accept, or <code><a href="System-IO.html#v:hWaitForInput">hWaitForInput</a></code> appear to be blocking, but they really don't because the runtime system uses scheduling mechanisms like <code>select(2)</code> to perform asynchronous I/O, so it is possible to interrupt standard socket I/O or file I/O using this combinator. </p></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.11.0</p></div></body></html>