This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TM4C1294NCPDT: Threading Question using LWIP

Part Number: TM4C1294NCPDT

Tool/software: TI C/C++ Compiler

Hi, I recently posted the following on the LWIP users forum but it seems quite inactive so I'm hoping someone here might be able to help:

I'm using LWIP for TCP/IP communication on a TI Tiva microcontroller (a TM4C1294NCPDT).  TI includes LWIP 1.4.1 in its TivaWare package.  I've read that LWIP is not threadsafe, however, I'm not sure exactly what that means in my context where I'm using it with no operating system on the single core TI Tiva microcontroller.

It seems the way LWIP works on the Tiva is during the SysTick interrupt, you call the lwIPTimer function giving it an amount of time for processing.  lwIPTimer then triggers the Tiva's ethernet interrupt and it does it's processing for the given amount of time.
Inside the lwIPTimer function, I see this comment above the line of code that triggers the Tiva's ethernet interrupt:
    //
    // Generate an Ethernet interrupt.  This will perform the actual work
    // of checking the lwIP timers and taking the appropriate actions.  This is
    // needed since lwIP is not re-entrant, and this allows all lwIP calls to
    // be placed inside the Ethernet interrupt handler ensuring that all calls
    // into lwIP are coming from the same context, preventing any reentrancy
    // issues.  Putting all the lwIP calls in the Ethernet interrupt handler
    // avoids the use of mutexes to avoid re-entering lwIP.
    //
My current code is making calls to tcp_write() and tcp_output() *outside* of the ethernet interrupt handler.  My calls to tcp_write() and tcp_output() occur in the SysTick interrupt just before I call lwIPTimer().  Does this violate the advice in the comment above?  I'm unsure since these are "tcp_" calls and not "lwIP" calls.
Can anyone please clarify?  Thank you.
  • Hi,

      This LwIP wiki link talks about using LwIP in a thread-safe context. 

    I'm not an expert in OS design,  normally thread safe means the manipulation of shared data structures in a manner that guarantees safe execution by multiple threads at the same time.

    Here is additional link about thread safety. 

  • Charles Tsai said:

    normally thread safe means the manipulation of shared data structures in a manner that guarantees safe execution by multiple threads at the same time.

    Right, but what I'm struggling with a bit is if execution of an interrupt is considered a separate thread in the context of the LWIP documentation.  I've seen the LWIP Wiki document you mention and it's not entirely clear on this (at least to me).

  • Hi,
    If the lwIP core is only accessed by a single thread at a time then I think you should be fine in your case. Normally this is achieved by having all lwIP processing done by a single thread and the device drivers pass packets to this thread for processing.

    For LwIP specific as such it is better to ask in LwIP forum.
  • Charles Tsai said:
    Hi,
    If the lwIP core is only accessed by a single thread at a time then I think you should be fine in your case. Normally this is achieved by having all lwIP processing done by a single thread and the device drivers pass packets to this thread for processing.

    Hi Charles, thanks for the additional thoughts.  I'm curious, when you suggest "device drivers pass packets to the single LWIP thread" are you saying that they should pass data to the LWIP thread and that data should be stored in a queue (say, within an LWIP wrapper) and then the data in the queue should be sent when the LWIP interrupt is doing its processing?

  • Okay, thanks for the info.  This is not a trivial change to my software but is doable.  I will do this and report back if it has resolved the issue.

  • HI,
    There are several TivaWare Ethernet examples. I will also suggest you to take a look at them. These examples are non-OS based and they use the LwIP raw API.