multithreading.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.13"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>lwIP: Multithreading</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="resize.js"></script>
  14. <script type="text/javascript" src="navtreedata.js"></script>
  15. <script type="text/javascript" src="navtree.js"></script>
  16. <script type="text/javascript">
  17. $(document).ready(initResizable);
  18. </script>
  19. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  20. <script type="text/javascript" src="search/searchdata.js"></script>
  21. <script type="text/javascript" src="search/search.js"></script>
  22. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  23. </head>
  24. <body>
  25. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  26. <div id="titlearea">
  27. <table cellspacing="0" cellpadding="0">
  28. <tbody>
  29. <tr style="height: 56px;">
  30. <td id="projectalign" style="padding-left: 0.5em;">
  31. <div id="projectname">lwIP
  32. &#160;<span id="projectnumber">2.1.2</span>
  33. </div>
  34. <div id="projectbrief">Lightweight IP stack</div>
  35. </td>
  36. </tr>
  37. </tbody>
  38. </table>
  39. </div>
  40. <!-- end header part -->
  41. <!-- Generated by Doxygen 1.8.13 -->
  42. <script type="text/javascript">
  43. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  44. </script>
  45. <script type="text/javascript" src="menudata.js"></script>
  46. <script type="text/javascript" src="menu.js"></script>
  47. <script type="text/javascript">
  48. $(function() {
  49. initMenu('',true,false,'search.php','Search');
  50. $(document).ready(function() { init_search(); });
  51. });
  52. </script>
  53. <div id="main-nav"></div>
  54. </div><!-- top -->
  55. <div id="side-nav" class="ui-resizable side-nav-resizable">
  56. <div id="nav-tree">
  57. <div id="nav-tree-contents">
  58. <div id="nav-sync" class="sync"></div>
  59. </div>
  60. </div>
  61. <div id="splitbar" style="-moz-user-select:none;"
  62. class="ui-resizable-handle">
  63. </div>
  64. </div>
  65. <script type="text/javascript">
  66. $(document).ready(function(){initNavTree('multithreading.html','');});
  67. </script>
  68. <div id="doc-content">
  69. <!-- window showing the filter options -->
  70. <div id="MSearchSelectWindow"
  71. onmouseover="return searchBox.OnSearchSelectShow()"
  72. onmouseout="return searchBox.OnSearchSelectHide()"
  73. onkeydown="return searchBox.OnSearchSelectKey(event)">
  74. </div>
  75. <!-- iframe showing the search results (closed by default) -->
  76. <div id="MSearchResultsWindow">
  77. <iframe src="javascript:void(0)" frameborder="0"
  78. name="MSearchResults" id="MSearchResults">
  79. </iframe>
  80. </div>
  81. <div class="header">
  82. <div class="headertitle">
  83. <div class="title">Multithreading </div> </div>
  84. </div><!--header-->
  85. <div class="contents">
  86. <div class="textblock"><p>lwIP started targeting single-threaded environments. When adding multi- threading support, instead of making the core thread-safe, another approach was chosen: there is one main thread running the lwIP core (also known as the "tcpip_thread"). When running in a multithreaded environment, raw API functions MUST only be called from the core thread since raw API functions are not protected from concurrent access (aside from pbuf- and memory management functions). Application threads using the sequential- or socket API communicate with this main thread through message passing.</p>
  87. <p>As such, the list of functions that may be called from other threads or an ISR is very limited! Only functions from these API header files are thread-safe:</p><ul>
  88. <li><a class="el" href="api_8h.html">api.h</a></li>
  89. <li><a class="el" href="netbuf_8h.html">netbuf.h</a></li>
  90. <li>netdb.h</li>
  91. <li><a class="el" href="netifapi_8h.html">netifapi.h</a></li>
  92. <li>pppapi.h</li>
  93. <li><a class="el" href="sockets_8h.html">sockets.h</a></li>
  94. <li><a class="el" href="sys_8h.html">sys.h</a></li>
  95. </ul>
  96. <p>Additionaly, memory (de-)allocation functions may be called from multiple threads (not ISR!) with NO_SYS=0 since they are protected by <a class="el" href="group__lwip__opts__lock.html#gae85efb3a5fcf8585c94b3c2669978959">SYS_LIGHTWEIGHT_PROT</a> and/or semaphores.</p>
  97. <p>Netconn or Socket API functions are thread safe against the core thread but they are not reentrant at the control block granularity level. That is, a UDP or TCP control block must not be shared among multiple threads without proper locking.</p>
  98. <p>If <a class="el" href="group__lwip__opts__lock.html#gae85efb3a5fcf8585c94b3c2669978959">SYS_LIGHTWEIGHT_PROT</a> is set to 1 and <a class="el" href="group__lwip__opts__mem.html#ga0a3ef6098813c103e5aba07da76e15e2">LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT</a> is set to 1, <a class="el" href="group__pbuf.html#gab0dd696fb4b6bc65e548944584f1738b">pbuf_free()</a> may also be called from another thread or an ISR (since only then, mem_free - for PBUF_RAM - may be called from an ISR: otherwise, the HEAP is only protected by semaphores).</p>
  99. <h2>How to get threading done right </h2>
  100. <p>It is strongly recommended to implement the <a class="el" href="group__lwip__opts__lock.html#ga6a30040db307b3459fc11906bd433f75">LWIP_ASSERT_CORE_LOCKED()</a> macro in an application that uses multithreading. lwIP code has several places where a check for a correct thread context is implemented which greatly helps the user to get threading done right. See the example sys_arch.c files in unix and Win32 port in the contrib repository.</p>
  101. <p>In short: Copy the functions sys_mark_tcpip_thread() and sys_check_core_locking() to your port and modify them to work with your OS. Then let <a class="el" href="group__lwip__opts__lock.html#ga6a30040db307b3459fc11906bd433f75">LWIP_ASSERT_CORE_LOCKED()</a> and <a class="el" href="group__lwip__opts__lock.html#gab97d014f7ecf3b20f9d5abc2d0a79a3e">LWIP_MARK_TCPIP_THREAD()</a> point to these functions.</p>
  102. <p>If you use <a class="el" href="group__lwip__opts__lock.html#ga8e46232794349c209e8ed4e9e7e4f011">LWIP_TCPIP_CORE_LOCKING</a>, you also need to copy and adapt the functions sys_lock_tcpip_core() and sys_unlock_tcpip_core(). Let <a class="el" href="tcpip_8h.html#a4700525e737fc025fea4887b172e0c95">LOCK_TCPIP_CORE()</a> and <a class="el" href="tcpip_8h.html#a915effea029b9c4891e1ec635eb1826d">UNLOCK_TCPIP_CORE()</a> point to these functions. </p>
  103. </div></div><!-- contents -->
  104. </div><!-- doc-content -->
  105. <!-- start footer part -->
  106. <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  107. <ul>
  108. <li class="footer">Generated by
  109. <a href="http://www.doxygen.org/index.html">
  110. <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.13 </li>
  111. </ul>
  112. </div>
  113. </body>
  114. </html>