MityDSP Documentation Index

Using the lwIP Stack With Multiple Threads

The lwIP stack maintains internal timers and semaphores on a per thread basis. These timers and semaphores allow blocking sends and receives to work and allow for timeouts in select function calls.

In order for the lwIP stack to allocate these thread variables, it is required that each thread in your application which will access any socket function calls must register itself after it begins execution. A function sys_thread_register() has been provided to perform the thread registration.

 int myThread()
 {
     // register to use the lwIP stack
    sys_thread_register(TSK_self());

    // do something "forever"
     while (true)
     {
         if (something) break;

         // perform application-specific functions
         ...
     }

     // un-register thread from lwIP stack
    sys_thread_unregister(TSK_self());

     return(0);
 }
Note:
If a thread is destroyed, sys_thread_unregister should be called to free resources previously committed by sys_thread_register.
Failure to register a thread may result in an application which will appear to work at first glance. But any unregistered threads will use undefined timers and semaphores resulting in timers being shortened or extended and/or semaphores being inadvertantly shared causing erroneous function returns, socket closures and possibly even application crashes.
All threads which access the socket interface must be at a lower priority the the tcpip_priority specified in the tsDSPNetStackInit structure during. See the nacb_func field in the MityDSP net library tsDSPNetStackInit structure.
There is very little overhead registering threads with the lwIP stack. If you are not sure which threads in your application will be ultimately making socket API function calls, it does not hurt to register all threads with the lwIP stack. The only thing to watch out for here is that you specify enough storage space using the max_threads member of the tsDSPNetStackInit structure during stack initialization.
See also:
Initializing the lwIP stack

  
Generated on Fri Sep 23 16:33:59 2011 for MityDSP lwIP Port by  Doxygen Version 1.6.1
Copyright © 2009, Critical Link LLC, All rights reserved.