main.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
  3. <title>The Protothreads Library 1.4: The Protothreads Library</title>
  4. <link href="doxygen.css" rel="stylesheet" type="text/css">
  5. <link href="tabs.css" rel="stylesheet" type="text/css">
  6. </head><body>
  7. <!-- Generated by Doxygen 1.4.6 -->
  8. <div class="tabs">
  9. <ul>
  10. <li id="current"><a href="main.html"><span>Main&nbsp;Page</span></a></li>
  11. <li><a href="modules.html"><span>Modules</span></a></li>
  12. <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
  13. <li><a href="files.html"><span>Files</span></a></li>
  14. </ul></div>
  15. <h1>The Protothreads Library</h1>
  16. <p>
  17. <dl compact><dt><b>Author:</b></dt><dd>Adam Dunkels &lt;<a href="mailto:adam@sics.se">adam@sics.se</a>&gt;</dd></dl>
  18. Protothreads are a type of lightweight stackless threads designed for severly memory constrained systems such as deeply embedded systems or sensor network nodes. Protothreads provides linear code execution for event-driven systems implemented in C. Protothreads can be used with or without an RTOS.<p>
  19. Protothreads are a extremely lightweight, stackless type of threads that provides a blocking context on top of an event-driven system, without the overhead of per-thread stacks. The purpose of protothreads is to implement sequential flow of control without complex state machines or full multi-threading. Protothreads provides conditional blocking inside C functions.<p>
  20. Main features:<p>
  21. <ul>
  22. <li>No machine specific code - the protothreads library is pure C</li></ul>
  23. <p>
  24. <ul>
  25. <li>Does not use error-prone functions such as longjmp()</li></ul>
  26. <p>
  27. <ul>
  28. <li>Very small RAM overhead - only two bytes per protothread</li></ul>
  29. <p>
  30. <ul>
  31. <li>Can be used with or without an OS</li></ul>
  32. <p>
  33. <ul>
  34. <li>Provides blocking wait without full multi-threading or stack-switching</li></ul>
  35. <p>
  36. Examples applications:<p>
  37. <ul>
  38. <li>Memory constrained systems</li></ul>
  39. <p>
  40. <ul>
  41. <li>Event-driven protocol stacks</li></ul>
  42. <p>
  43. <ul>
  44. <li>Deeply embedded systems</li></ul>
  45. <p>
  46. <ul>
  47. <li>Sensor network nodes</li></ul>
  48. <p>
  49. <dl compact><dt><b>See also:</b></dt><dd><a class="el" href="a00015.html">Example programs</a> <p>
  50. <a class="el" href="a00014.html">Protothreads API documentation</a></dd></dl>
  51. The protothreads library is released under a BSD-style license that allows for both non-commercial and commercial usage. The only requirement is that credit is given.<p>
  52. More information and new version of the code can be found at the Protothreads homepage:<p>
  53. <a href="http://www.sics.se/~adam/pt/">http://www.sics.se/~adam/pt/</a><h2><a class="anchor" name="authors">
  54. Authors</a></h2>
  55. The protothreads library was written by Adam Dunkels &lt;<a href="mailto:adam@sics.se">adam@sics.se</a>&gt; with support from Oliver Schmidt &lt;<a href="mailto:ol.sc@web.de">ol.sc@web.de</a>&gt;.<h2><a class="anchor" name="using">
  56. Using protothreads</a></h2>
  57. Using protothreads in a project is easy: simply copy the files <a class="el" href="a00013.html">pt.h</a>, <a class="el" href="a00011.html">lc.h</a> and lc-switch.h into the include files directory of the project, and #include "pt.h" in all files that should use protothreads.<h2><a class="anchor" name="pt-desc">
  58. Protothreads</a></h2>
  59. Protothreads are a extremely lightweight, stackless threads that provides a blocking context on top of an event-driven system, without the overhead of per-thread stacks. The purpose of protothreads is to implement sequential flow of control without using complex state machines or full multi-threading. Protothreads provides conditional blocking inside a C function.<p>
  60. In memory constrained systems, such as deeply embedded systems, traditional multi-threading may have a too large memory overhead. In traditional multi-threading, each thread requires its own stack, that typically is over-provisioned. The stacks may use large parts of the available memory.<p>
  61. The main advantage of protothreads over ordinary threads is that protothreads are very lightweight: a protothread does not require its own stack. Rather, all protothreads run on the same stack and context switching is done by stack rewinding. This is advantageous in memory constrained systems, where a stack for a thread might use a large part of the available memory. A protothread only requires only two bytes of memory per protothread. Moreover, protothreads are implemented in pure C and do not require any machine-specific assembler code.<p>
  62. A protothread runs within a single C function and cannot span over other functions. A protothread may call normal C functions, but cannot block inside a called function. Blocking inside nested function calls is instead made by spawning a separate protothread for each potentially blocking function. The advantage of this approach is that blocking is explicit: the programmer knows exactly which functions that block that which functions the never blocks.<p>
  63. Protothreads are similar to asymmetric co-routines. The main difference is that co-routines uses a separate stack for each co-routine, whereas protothreads are stackless. The most similar mechanism to protothreads are Python generators. These are also stackless constructs, but have a different purpose. Protothreads provides blocking contexts inside a C function, whereas Python generators provide multiple exit points from a generator function.<h2><a class="anchor" name="pt-autovars">
  64. Local variables</a></h2>
  65. <dl compact><dt><b>Note:</b></dt><dd>Because protothreads do not save the stack context across a blocking call, local variables are not preserved when the protothread blocks. This means that local variables should be used with utmost care - if in doubt, do not use local variables inside a protothread!</dd></dl>
  66. <h2><a class="anchor" name="pt-scheduling">
  67. Scheduling</a></h2>
  68. A protothread is driven by repeated calls to the function in which the protothread is running. Each time the function is called, the protothread will run until it blocks or exits. Thus the scheduling of protothreads is done by the application that uses protothreads.<h2><a class="anchor" name="pt-impl">
  69. Implementation</a></h2>
  70. Protothreads are implemented using local continuations. A local continuation represents the current state of execution at a particular place in the program, but does not provide any call history or local variables. A local continuation can be set in a specific function to capture the state of the function. After a local continuation has been set can be resumed in order to restore the state of the function at the point where the local continuation was set.<p>
  71. Local continuations can be implemented in a variety of ways:<p>
  72. <ol type=1>
  73. <li>by using machine specific assembler code,</li><li>by using standard C constructs, or</li><li>by using compiler extensions.</li></ol>
  74. <p>
  75. The first way works by saving and restoring the processor state, except for stack pointers, and requires between 16 and 32 bytes of memory per protothread. The exact amount of memory required depends on the architecture.<p>
  76. The standard C implementation requires only two bytes of state per protothread and utilizes the C switch() statement in a non-obvious way that is similar to Duff's device. This implementation does, however, impose a slight restriction to the code that uses protothreads: a protothread cannot perform a blocking wait (<a class="el" href="a00014.html#g99e43010ec61327164466aa2d902de45">PT_WAIT_UNTIL()</a> or <a class="el" href="a00014.html#g155cba6121323726d02c00284428fed6">PT_YIELD()</a>) inside a switch() statement.<p>
  77. Certain compilers has C extensions that can be used to implement protothreads. GCC supports label pointers that can be used for this purpose. With this implementation, protothreads require 4 bytes of RAM per protothread. <hr size="1"><address style="align: right;"><small>Generated on Mon Oct 2 10:06:29 2006 for The Protothreads Library 1.4 by&nbsp;
  78. <a href="http://www.doxygen.org/index.html">
  79. <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
  80. </body>
  81. </html>