Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LWIP + RTOS - how to avoid netconn block the thread forever?

When the LwIP netconn_accept() or netconn_recv() function is called, if we are using a RTOS, it will block the thread and wait for a connection until timeout or forever, depends on the setting of LWIP_SO_RCVTIME0. The timeout duration is equal to the SYS_ARCH_TIMEOUT.

The SYS_ARCH_TIMEOUT is defined as 0xffffffff in the core include part of the LwIP stack, so I think it is not expected to be changed.

Actually, I want it to check if a connection is made, if not then it continue the thread. However, if I call netconn_accept(), it will just block the thread and wait there forever (or a very long time)...I don't want to jsut change the define value of SYS_ARCH_TIMEOUT because I need different timeout in different situation...

What is the good way to do that? Thanks.

like image 606
eepty Avatar asked Jun 01 '12 10:06

eepty


2 Answers

Polling for TCP connection (or acceptance) is usually a bad practice. Consider spawning a new thread dedicated exclusively to the blocking netconn_accept() call.

I understand the limitations of using RTOSes, but spawning just one helper thread with minimal stack space shouldn't be a major problem.

I believe that implementing a solution to the classical Producer-Consumer problem is not that hard.

If you're talking about the FreeRTOS, it has all the tools needed - semaphores and threads.

like image 98
Viktor Latypov Avatar answered Sep 28 '22 07:09

Viktor Latypov


Don't use the blocking API at all. The lwIP stack provides a native, non-blocking, event-driven API, which is more efficient than blocking and does not require a blocking RTOS. A YouTube video shows (at http://youtu.be/MBk5wJ_8jEc) shows how this API has been used in a real-time system based on the QP state machine framework.

like image 41
Miro Samek Avatar answered Sep 28 '22 09:09

Miro Samek