[En-Nut-Discussion] killing threads waiting for a connection in NutTcpAccept

Timothy M. De Baillie debaillie at ciholas.com
Mon Apr 6 19:36:54 CEST 2009


José Vallet wrote:
> I have some threads waiting for connections with NutTcpAccept, and I'd 
> like to kill them.
>
> The (simplified) code goes like this
> __________________
> ...
> sock = NutTcpCreateSocket();
> NutTcpAccept(sock, SERVER_PORT);
> mystream =_fdopen((int) sock, "r+b");
> whihle(!disconnect)
> {
> ...
> }
> fclose(sl_stream);
> NutTcpCloseSocket(_sl_sock);
> NutThreadExit();
> __________________
>
>
> The problem is that while the connection does not arrive the thread is 
> blocked waiting for connections, and thus the thread will never reach 
> NutThreadExit.
>
> I have "solved" this by sending from another thread an event to 
> "sock->so_pc_tq", which is the queue where NutTcpStatePassiveOpenEvent 
> (called from NutTcpAccept) blocks waiting for events (connections). The 
> result is that, as the stack thinks that a valid connection has arrived, 
> all the code between NutTcpAccept and NutThreadExit() is executed 
> normally as if the connection had arrived properly.
>
> Though I can live with this for my particular application, I guess this 
> solution is ugly and dirty (at least to me) and I wonder if there is any 
> other better solution to unblock the thread waiting for connections 
> (note: closing the socket does not unblock NutTcpAccept).
>
> Thanks and regards
> José
> _______________________________________________
> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
>
>   
Jose,

There is no easy answer to your problem.  The best thing you could do is 
change the timeout in the NutTcpStatePassiveOpenEvent function in 
net/tcpsm.c to a non-zero value.  0 causes an infinite event wait.  So 
you could change the code to something like this:

if(NutEventWait(&sock->so_pc_tq, SOME_DEFINED_TIME_IN_MS))
    return -1;
else
    return 0;

Then your NutTcpAccept would return 0 on success and -1 on failure.

This would be a very easy change to the OS and probably should be a 
configurable option.

Tim




More information about the En-Nut-Discussion mailing list