[Haskell-cafe] Re: all threads are blocked by recvFrom
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 , 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 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  (esp
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.
Adam Langley agl at imperialviolet.org http://www.imperialviolet.org
More information about the Haskell-Cafe