📄 pt-doc.txt
字号:
/**\defgroup pt Protothreads Protothreads are a type of lightweight stackless threads designed forseverly memory constrained systems such as deeply embedded systems orsensor network nodes. Protothreads provides linear code execution forevent-driven systems implemented in C. Protothreads can be used withor without an RTOS.Protothreads are a extremely lightweight, stackless type of threadsthat provides a blocking context on top of an event-driven system,without the overhead of per-thread stacks. The purpose of protothreadsis to implement sequential flow of control without complex statemachines or full multi-threading. Protothreads provides conditionalblocking inside C functions.The advantage of protothreads over a purely event-driven approach isthat protothreads provides a sequential code structure that allows forblocking functions. In purely event-driven systems, blocking must beimplemented by manually breaking the function into two pieces - onefor the piece of code before the blocking call and one for the codeafter the blocking call. This makes it hard to use control structuressuch as if() conditionals and while() loops.The advantage of protothreads over ordinary threads is that aprotothread do not require a separate stack. In memory constrainedsystems, the overhead of allocating multiple stacks can consume largeamounts of the available memory. In contrast, each protothread onlyrequires between two and twelve bytes of state, depending on thearchitecture.\note Because protothreads do not save the stack context across ablocking call, <b>local variables are not preserved when theprotothread blocks</b>. This means that local variables should be usedwith utmost care - <b>if in doubt, do not use local variables inside aprotothread!</b>Main features: - No machine specific code - the protothreads library is pure C - Does not use error-prone functions such as longjmp() - Very small RAM overhead - only two bytes per protothread - Can be used with or without an OS - Provides blocking wait without full multi-threading or stack-switchingExamples applications: - Memory constrained systems - Event-driven protocol stacks - Deeply embedded systems - Sensor network nodesThe protothreads API consists of four basic operations:initialization: PT_INIT(), execution: PT_BEGIN(), conditionalblocking: PT_WAIT_UNTIL() and exit: PT_END(). On top of these, twoconvenience functions are built: reversed condition blocking:PT_WAIT_WHILE() and protothread blocking: PT_WAIT_THREAD().\sa \ref pt "Protothreads API documentation" The protothreads library is released under a BSD-style license thatallows for both non-commercial and commercial usage. The onlyrequirement is that credit is given.\section authors AuthorsThe protothreads library was written by Adam Dunkels <adam@sics.se>with support from Oliver Schmidt <ol.sc@web.de>.\section pt-desc ProtothreadsProtothreads are a extremely lightweight, stackless threads thatprovides a blocking context on top of an event-driven system, withoutthe overhead of per-thread stacks. The purpose of protothreads is toimplement sequential flow of control without using complex statemachines or full multi-threading. Protothreads provides conditionalblocking inside a C function.In memory constrained systems, such as deeply embedded systems,traditional multi-threading may have a too large memory overhead. Intraditional multi-threading, each thread requires its own stack, thattypically is over-provisioned. The stacks may use large parts of theavailable memory.The main advantage of protothreads over ordinary threads is thatprotothreads are very lightweight: a protothread does not require itsown stack. Rather, all protothreads run on the same stack and contextswitching is done by stack rewinding. This is advantageous in memoryconstrained systems, where a stack for a thread might use a large partof the available memory. A protothread only requires only two bytes ofmemory per protothread. Moreover, protothreads are implemented in pureC and do not require any machine-specific assembler code.A protothread runs within a single C function and cannot span overother functions. A protothread may call normal C functions, but cannotblock inside a called function. Blocking inside nested function callsis instead made by spawning a separate protothread for eachpotentially blocking function. The advantage of this approach is thatblocking is explicit: the programmer knows exactly which functionsthat block that which functions the never blocks.Protothreads are similar to asymmetric co-routines. The maindifference is that co-routines uses a separate stack for eachco-routine, whereas protothreads are stackless. The most similarmechanism to protothreads are Python generators. These are alsostackless constructs, but have a different purpose. Protothreadsprovides blocking contexts inside a C function, whereas Pythongenerators provide multiple exit points from a generator function.\section pt-autovars Local variables\note Because protothreads do not save the stack context across a blockingcall, local variables are not preserved when the protothreadblocks. This means that local variables should be used with utmostcare - if in doubt, do not use local variables inside a protothread!\section pt-scheduling SchedulingA protothread is driven by repeated calls to the function in which theprotothread is running. Each time the function is called, theprotothread will run until it blocks or exits. Thus the scheduling ofprotothreads is done by the application that uses protothreads.\section pt-impl ImplementationProtothreads are implemented using \ref lc "local continuations". Alocal continuation represents the current state of execution at aparticular place in the program, but does not provide any call historyor local variables. A local continuation can be set in a specificfunction to capture the state of the function. After a localcontinuation has been set can be resumed in order to restore the stateof the function at the point where the local continuation was set.Local continuations can be implemented in a variety of ways: -# by using machine specific assembler code, -# by using standard C constructs, or -# by using compiler extensions. The first way works by saving and restoring the processor state,except for stack pointers, and requires between 16 and 32 bytes ofmemory per protothread. The exact amount of memory required depends onthe architecture.The standard C implementation requires only two bytes of state perprotothread and utilizes the C switch() statement in a non-obvious waythat is similar to Duff's device. This implementation does, however,impose a slight restriction to the code that uses protothreads in thatthe code cannot use switch() statements itself.Certain compilers has C extensions that can be used to implementprotothreads. GCC supports label pointers that can be used for thispurpose. With this implementation, protothreads require 4 bytes of RAMper protothread.@{ *//** @} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -