pt-mainpage.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. \mainpage The Protothreads Library
  3. \author Adam Dunkels <adam@sics.se>
  4. Protothreads are a type of lightweight stackless threads designed for
  5. severly memory constrained systems such as deeply embedded systems or
  6. sensor network nodes. Protothreads provides linear code execution for
  7. event-driven systems implemented in C. Protothreads can be used with
  8. or without an RTOS.
  9. Protothreads are a extremely lightweight, stackless type of threads
  10. that provides a blocking context on top of an event-driven system,
  11. without the overhead of per-thread stacks. The purpose of protothreads
  12. is to implement sequential flow of control without complex state
  13. machines or full multi-threading. Protothreads provides conditional
  14. blocking inside C functions.
  15. Main features:
  16. - No machine specific code - the protothreads library is pure C
  17. - Does not use error-prone functions such as longjmp()
  18. - Very small RAM overhead - only two bytes per protothread
  19. - Can be used with or without an OS
  20. - Provides blocking wait without full multi-threading or
  21. stack-switching
  22. Examples applications:
  23. - Memory constrained systems
  24. - Event-driven protocol stacks
  25. - Deeply embedded systems
  26. - Sensor network nodes
  27. \sa \ref examples "Example programs"
  28. \sa \ref pt "Protothreads API documentation"
  29. The protothreads library is released under a BSD-style license that
  30. allows for both non-commercial and commercial usage. The only
  31. requirement is that credit is given.
  32. More information and new version of the code can be found at the
  33. Protothreads homepage:
  34. http://www.sics.se/~adam/pt/
  35. \section authors Authors
  36. The protothreads library was written by Adam Dunkels <adam@sics.se>
  37. with support from Oliver Schmidt <ol.sc@web.de>.
  38. \section using Using protothreads
  39. Using protothreads in a project is easy: simply copy the files pt.h,
  40. lc.h and lc-switch.h into the include files directory of the project,
  41. and \#include "pt.h" in all files that should use protothreads.
  42. \section pt-desc Protothreads
  43. Protothreads are a extremely lightweight, stackless threads that
  44. provides a blocking context on top of an event-driven system, without
  45. the overhead of per-thread stacks. The purpose of protothreads is to
  46. implement sequential flow of control without using complex state
  47. machines or full multi-threading. Protothreads provides conditional
  48. blocking inside a C function.
  49. In memory constrained systems, such as deeply embedded systems,
  50. traditional multi-threading may have a too large memory overhead. In
  51. traditional multi-threading, each thread requires its own stack, that
  52. typically is over-provisioned. The stacks may use large parts of the
  53. available memory.
  54. The main advantage of protothreads over ordinary threads is that
  55. protothreads are very lightweight: a protothread does not require its
  56. own stack. Rather, all protothreads run on the same stack and context
  57. switching is done by stack rewinding. This is advantageous in memory
  58. constrained systems, where a stack for a thread might use a large part
  59. of the available memory. A protothread only requires only two bytes of
  60. memory per protothread. Moreover, protothreads are implemented in pure
  61. C and do not require any machine-specific assembler code.
  62. A protothread runs within a single C function and cannot span over
  63. other functions. A protothread may call normal C functions, but cannot
  64. block inside a called function. Blocking inside nested function calls
  65. is instead made by spawning a separate protothread for each
  66. potentially blocking function. The advantage of this approach is that
  67. blocking is explicit: the programmer knows exactly which functions
  68. that block that which functions the never blocks.
  69. Protothreads are similar to asymmetric co-routines. The main
  70. difference is that co-routines uses a separate stack for each
  71. co-routine, whereas protothreads are stackless. The most similar
  72. mechanism to protothreads are Python generators. These are also
  73. stackless constructs, but have a different purpose. Protothreads
  74. provides blocking contexts inside a C function, whereas Python
  75. generators provide multiple exit points from a generator function.
  76. \section pt-autovars Local variables
  77. \note
  78. Because protothreads do not save the stack context across a blocking
  79. call, local variables are not preserved when the protothread
  80. blocks. This means that local variables should be used with utmost
  81. care - if in doubt, do not use local variables inside a protothread!
  82. \section pt-scheduling Scheduling
  83. A protothread is driven by repeated calls to the function in which the
  84. protothread is running. Each time the function is called, the
  85. protothread will run until it blocks or exits. Thus the scheduling of
  86. protothreads is done by the application that uses protothreads.
  87. \section pt-impl Implementation
  88. Protothreads are implemented using local continuations. A local
  89. continuation represents the current state of execution at a particular
  90. place in the program, but does not provide any call history or local
  91. variables. A local continuation can be set in a specific function to
  92. capture the state of the function. After a local continuation has been
  93. set can be resumed in order to restore the state of the function at
  94. the point where the local continuation was set.
  95. Local continuations can be implemented in a variety of ways:
  96. -# by using machine specific assembler code,
  97. -# by using standard C constructs, or
  98. -# by using compiler extensions.
  99. The first way works by saving and restoring the processor state,
  100. except for stack pointers, and requires between 16 and 32 bytes of
  101. memory per protothread. The exact amount of memory required depends on
  102. the architecture.
  103. The standard C implementation requires only two bytes of state per
  104. protothread and utilizes the C switch() statement in a non-obvious way
  105. that is similar to Duff's device. This implementation does, however,
  106. impose a slight restriction to the code that uses protothreads: a
  107. protothread cannot perform a blocking wait (PT_WAIT_UNTIL() or
  108. PT_YIELD()) inside a switch() statement.
  109. Certain compilers has C extensions that can be used to implement
  110. protothreads. GCC supports label pointers that can be used for this
  111. purpose. With this implementation, protothreads require 4 bytes of RAM
  112. per protothread.
  113. */