lwIP start race condition (patch)
Posted: Sun Jun 28, 2015 7:50 pm
Hello,
using any function of lwIP before it completed its initialization usually ends in a crash. lwIP is initialized when its thread starts. So you have a race condition if you are using lwIP just after starting its thread. To prevent that you can pass a callback function to tcpip_init() which is called when the initialization of lwIP is completed.
The ChibiOS layer around lwIP doesn't allow to pass the callback function down to tcpip_init(). A small pacth fixes that.
Greetings
Frank
diff --git a/ChibiOS/os/various/lwip_bindings/lwipthread.c b/ChibiOS/os/various/lwip_bindings/lwipthread.c
index 38ef8cf..2961277 100644
--- a/ChibiOS/os/various/lwip_bindings/lwipthread.c
+++ b/ChibiOS/os/various/lwip_bindings/lwipthread.c
@@ -211,7 +211,7 @@ static err_t ethernetif_init(struct netif *netif) {
* @param[in] p pointer to a @p lwipthread_opts structure or @p NULL
* @return The function does not return.
*/
-msg_t lwip_thread(void *p) {
+msg_t lwip_thread(void *p, tcpip_init_done_fn tcpip_init_done, void *arg) {
EvTimer evt;
EventListener el0, el1;
struct ip_addr ip, gateway, netmask;
@@ -221,7 +221,7 @@ msg_t lwip_thread(void *p) {
chRegSetThreadName("lwipthread");
/* Initializes the thing.*/
- tcpip_init(NULL, NULL);
+ tcpip_init(tcpip_init_done, arg);
/* TCP/IP parameters, runtime or compile time.*/
if (p) {
diff --git a/ChibiOS/os/various/lwip_bindings/lwipthread.h b/ChibiOS/os/various/lwip_bindings/lwipthread.h
index fe4de28..1503dad 100644
--- a/ChibiOS/os/various/lwip_bindings/lwipthread.h
+++ b/ChibiOS/os/various/lwip_bindings/lwipthread.h
@@ -25,6 +25,7 @@
#define _LWIPTHREAD_H_
#include <lwip/opt.h>
+#include <lwip/tcpip.h>
/** @brief MAC thread priority.*/
#ifndef LWIP_THREAD_PRIORITY
@@ -121,7 +122,7 @@ extern WORKING_AREA(wa_lwip_thread, LWIP_THREAD_STACK_SIZE);
#ifdef __cplusplus
extern "C" {
#endif
- msg_t lwip_thread(void *p);
+ msg_t lwip_thread(void *p, tcpip_init_done_fn tcpip_init_done, void *arg);
#ifdef __cplusplus
}
#endif
using any function of lwIP before it completed its initialization usually ends in a crash. lwIP is initialized when its thread starts. So you have a race condition if you are using lwIP just after starting its thread. To prevent that you can pass a callback function to tcpip_init() which is called when the initialization of lwIP is completed.
The ChibiOS layer around lwIP doesn't allow to pass the callback function down to tcpip_init(). A small pacth fixes that.
Greetings
Frank
diff --git a/ChibiOS/os/various/lwip_bindings/lwipthread.c b/ChibiOS/os/various/lwip_bindings/lwipthread.c
index 38ef8cf..2961277 100644
--- a/ChibiOS/os/various/lwip_bindings/lwipthread.c
+++ b/ChibiOS/os/various/lwip_bindings/lwipthread.c
@@ -211,7 +211,7 @@ static err_t ethernetif_init(struct netif *netif) {
* @param[in] p pointer to a @p lwipthread_opts structure or @p NULL
* @return The function does not return.
*/
-msg_t lwip_thread(void *p) {
+msg_t lwip_thread(void *p, tcpip_init_done_fn tcpip_init_done, void *arg) {
EvTimer evt;
EventListener el0, el1;
struct ip_addr ip, gateway, netmask;
@@ -221,7 +221,7 @@ msg_t lwip_thread(void *p) {
chRegSetThreadName("lwipthread");
/* Initializes the thing.*/
- tcpip_init(NULL, NULL);
+ tcpip_init(tcpip_init_done, arg);
/* TCP/IP parameters, runtime or compile time.*/
if (p) {
diff --git a/ChibiOS/os/various/lwip_bindings/lwipthread.h b/ChibiOS/os/various/lwip_bindings/lwipthread.h
index fe4de28..1503dad 100644
--- a/ChibiOS/os/various/lwip_bindings/lwipthread.h
+++ b/ChibiOS/os/various/lwip_bindings/lwipthread.h
@@ -25,6 +25,7 @@
#define _LWIPTHREAD_H_
#include <lwip/opt.h>
+#include <lwip/tcpip.h>
/** @brief MAC thread priority.*/
#ifndef LWIP_THREAD_PRIORITY
@@ -121,7 +122,7 @@ extern WORKING_AREA(wa_lwip_thread, LWIP_THREAD_STACK_SIZE);
#ifdef __cplusplus
extern "C" {
#endif
- msg_t lwip_thread(void *p);
+ msg_t lwip_thread(void *p, tcpip_init_done_fn tcpip_init_done, void *arg);
#ifdef __cplusplus
}
#endif