[Haskell-cafe] Re: all threads are blocked by recvFrom
Adam Langley
agl at imperialviolet.org
Mon Mar 17 13:06:54 EDT 2008
On Mon, Mar 17, 2008 at 2:29 AM, Vitaliy Akimov
<vitaliy.akimov at gmail.com> wrote:
> Hi Adam, sorry for late answer. Here is my example [1], but yours
> doesn't work on my PC too. And it's strange it works on yours.
Are you running Windows? Because you're hpaste example works,
nonblocking for me too.
> And after changing in the network package FFI declaration for
> c_recvfrom from unsafe to safe both examples start working.
The important point here is that the recvFrom calls in
Network.Socket[1] don't block. They are non-blocking calls so,
although other threads may be suspended while in the FFI call, the FFI
call returns -EAGAIN if it would block and blocking is passed to the
RTS to handle. While sleeping in the RTS (with threadWaitRead etc),
other Haskell threads can read. See the code in [1] (esp
throwErrnoIfMinus1Retry_repeatOnBlock)
So, the only reason that other threads would be blocked is if the
socket wasn't marked as non-blocking. Here's a snippet of strace
output from compiling your hpaste example:
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 5
fcntl64(5, F_GETFL) = 0x2 (flags O_RDWR)
fcntl64(5, F_SETFL, O_RDWR|O_NONBLOCK) = 0
You can see that the socket is set to non-blocking immediately.
[1] http://haskell.org/ghc/docs/latest/html/libraries/network/src/Network-Socket.html#recvFrom
--
Adam Langley agl at imperialviolet.org http://www.imperialviolet.org
More information about the Haskell-Cafe
mailing list