Threading Question using LWIP with TM4C1294NCPDT

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Threading Question using LWIP with TM4C1294NCPDT

Terence Darwen
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.

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Threading Question using LWIP with TM4C1294NCPDT

Terence Darwen
Bump :)   Anyone?  :)

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Threading Question using LWIP with TM4C1294NCPDT

goldsimon@gmx.de
In reply to this post by Terence Darwen
On 18.10.2018 22:27, Terence Darwen wrote:
> 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.
> [..]
> Can anyone please clarify?  Thank you.

Honestly, I don't know the setup of this TI port. However, I can clarify
on the "not threadsafe" thing:

lwIP does *not* include locking schemes to protect against being called
from multiple threads *or* from a thread/main-loop and/or one or more
interrupt levels at the same time.
There is some protection (a.k.a. "lightweight prot"), but that mainly
covers pool allocation.

So if this port is written in a way that calls to lwIP don't run from
main loop and from interrupt at the same time - and only one interrupt
runs at a time and isn't interrupted by another interrupt while using
lwIP - you should be safe.

I can't tell you if it is that way for your port, though.


Simon

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Threading Question using LWIP with TM4C1294NCPDT

vr roriz
> and only one interrupt runs at a time and isn't interrupted by another interrupt while using
lwIP - you should be safe.

Simon, could you clarify why nesting interrupts are a problem and
non-nesting are not? Do you mean that, if an interrupt executes lwIP
code then it should not be interrupted by another?
My interrupts never execute lwIP code, they just trigger processes
that run lwIP, and all of these processes have the same priority.
Before I was not experiecing problems, while I had one interrupt
(timer) triggerig a process to call network_timeouts_checker and one
interrupt (ETH controller) to trigger a process to handle income ETH
Rx data. Now, I am trying to also trigger a process to handle ETH Tx
transmission done and I am always having an exception (data access
asynchronous error) at tcp_output, at:

       /* In the case of fast retransmit, the packet should not go to the tail
         * of the unacked queue, but rather somewhere before it. We
need to check for
         * this case. -STJ Jul 27, 2004 */
        if (TCP_SEQ_LT(lwip_ntohl(seg->tcphdr->seqno),
lwip_ntohl(useg->tcphdr->seqno))) {

Would you have some insight/comment?

MfG,
Vitor
Em seg, 22 de out de 2018 às 20:16, [hidden email]
<[hidden email]> escreveu:

>
> On 18.10.2018 22:27, Terence Darwen wrote:
> > 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.
> > [..]
> > Can anyone please clarify?  Thank you.
>
> Honestly, I don't know the setup of this TI port. However, I can clarify
> on the "not threadsafe" thing:
>
> lwIP does *not* include locking schemes to protect against being called
> from multiple threads *or* from a thread/main-loop and/or one or more
> interrupt levels at the same time.
> There is some protection (a.k.a. "lightweight prot"), but that mainly
> covers pool allocation.
>
> So if this port is written in a way that calls to lwIP don't run from
> main loop and from interrupt at the same time - and only one interrupt
> runs at a time and isn't interrupted by another interrupt while using
> lwIP - you should be safe.
>
> I can't tell you if it is that way for your port, though.
>
>
> Simon
>
> _______________________________________________
> lwip-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Threading Question using LWIP with TM4C1294NCPDT

Jan Menzel
On 26.10.2018 10:27, vr roriz wrote:
>> and only one interrupt runs at a time and isn't interrupted by another interrupt while using
> lwIP - you should be safe.
>
> Simon, could you clarify why nesting interrupts are a problem and
> non-nesting are not? Do you mean that, if an interrupt executes lwIP
> code then it should not be interrupted by another?
> My interrupts never execute lwIP code, they just trigger processes
> that run lwIP, and all of these processes have the same priority.

Vitor, make sure your processes are never cycled by the scheduler
otherwise you'll violate the threading requirements. I'd suggest to just
have a single thread for lwip and use a mailbox or similar to trigger
certain actions. The OS will take care that you can write into the
mailbox from any interrupt/thread/timer.

        Jan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Threading Question using LWIP with TM4C1294NCPDT

vr roriz
>Vitor, make sure your processes are never cycled by the scheduler
otherwise you'll violate the threading requirements. I'd suggest to just
have a single thread for lwip and use a mailbox or similar to trigger
certain actions. The OS will take care that you can write into the
mailbox from any interrupt/thread/timer.

Hi Jan, I do have a "mailbox", I am using a message-passing OS. All my
interrupts just send messages to processes that execute lwIP code, all
these processes have the same priority. What do you exactly mean by "
never cycle"? The scheduling algo is priority level based, without
time-slices.
Em sex, 26 de out de 2018 às 15:39, Jan Menzel
<[hidden email]> escreveu:

>
> On 26.10.2018 10:27, vr roriz wrote:
> >> and only one interrupt runs at a time and isn't interrupted by another interrupt while using
> > lwIP - you should be safe.
> >
> > Simon, could you clarify why nesting interrupts are a problem and
> > non-nesting are not? Do you mean that, if an interrupt executes lwIP
> > code then it should not be interrupted by another?
> > My interrupts never execute lwIP code, they just trigger processes
> > that run lwIP, and all of these processes have the same priority.
>
> Vitor, make sure your processes are never cycled by the scheduler
> otherwise you'll violate the threading requirements. I'd suggest to just
> have a single thread for lwip and use a mailbox or similar to trigger
> certain actions. The OS will take care that you can write into the
> mailbox from any interrupt/thread/timer.
>
>         Jan
>
> _______________________________________________
> lwip-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Threading Question using LWIP with TM4C1294NCPDT

Jan Menzel
On 26.10.2018 15:57, vr roriz wrote:

>> Vitor, make sure your processes are never cycled by the scheduler
> otherwise you'll violate the threading requirements. I'd suggest to just
> have a single thread for lwip and use a mailbox or similar to trigger
> certain actions. The OS will take care that you can write into the
> mailbox from any interrupt/thread/timer.
>
> Hi Jan, I do have a "mailbox", I am using a message-passing OS. All my
> interrupts just send messages to processes that execute lwIP code, all
> these processes have the same priority. What do you exactly mean by "
> never cycle"? The scheduling algo is priority level based, without
> time-slices.

Just make sure your scheduler never interrupts one process in favour for
an other with same priority. I would consider this non standard.
Alternatively you could have one lock shared between all your processes
to force the scheduler to never interrupt any lwip function to execute
any other.

        Jan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Threading Question using LWIP with TM4C1294NCPDT

vr roriz
Sorry, I forgot to reply.

My problem was probably due to concurrent access to lwIP, since my low_level_output function might block if the ETH Tx buffer is full, waiting for a Tx trigger. I've created an access queue based on messages to simulate a lock in the high-level driver.

Vitor

Em sex, 26 de out de 2018 às 18:29, Jan Menzel <[hidden email]> escreveu:
On 26.10.2018 15:57, vr roriz wrote:
>> Vitor, make sure your processes are never cycled by the scheduler
> otherwise you'll violate the threading requirements. I'd suggest to just
> have a single thread for lwip and use a mailbox or similar to trigger
> certain actions. The OS will take care that you can write into the
> mailbox from any interrupt/thread/timer.
>
> Hi Jan, I do have a "mailbox", I am using a message-passing OS. All my
> interrupts just send messages to processes that execute lwIP code, all
> these processes have the same priority. What do you exactly mean by "
> never cycle"? The scheduling algo is priority level based, without
> time-slices.

Just make sure your scheduler never interrupts one process in favour for
an other with same priority. I would consider this non standard.
Alternatively you could have one lock shared between all your processes
to force the scheduler to never interrupt any lwip function to execute
any other.

        Jan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users