# Protothreads **Repository Path**: xw19981010/protothreads ## Basic Information - **Project Name**: Protothreads - **Description**: 内存受限嵌入式系统的事件驱动编程 - **Primary Language**: C - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2024-04-21 - **Last Updated**: 2024-04-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Protothreads ## 1. Introduction:Event-driven vs Multithread? 用于内存受限嵌入式系统的多线程方法编程 - Many operating systems for sensor networks (TinyOS,Contiki,SOS)use event-driven programming model instead of traditional multithreaded approach - Event-driven systems support high level of concurrency with little RAM - –Single stack - –Sensor nodes have limited RAM (few kilobytes) - Multithreading is difficult due to limited RAM - –Per-thread stack (Default stack size for each stack in MANTIS is 128 bytes) - –Expensive context switching,thread-scheduling,synchronization,reentrancy etc. - –Can limit concurrency ### 2. Protothreads:Motivation * An event-driven model does not support blocking wait abstraction * Programming is difficult –a logically blocking sequence must be written in a state machine style * –Split phase operation in TinyOS * Thus many practical event-based programs are difficult to understand * Protothreads was originally developed for managing the complexity of state machines in the event-driven uIP embedded TCP/IP stack ### 3. Protothreads:Goals * Simplify programming by reducing or eliminating state machine management * Negligible RAM overhead * Not intended to replace event-driven systems * –rather be able to use protothreads on top of event driven system if state management becomes difficult * Protothreads provide conditional blocking abstractions to simplify programming for memory constrained embedded devices * –The blocking wait semantics allow linear sequencing of statements in event-driven systems * Protothreads are stackless–all protothreads in a system run on the same stack,which is rewound every time a protothread blocks. * A protothread is invoked by repeated calls to the function in which it runs ### 4. Protothread scheduling * The protothreads mechanism does not specify any specific method to invoke or schedule a protothread * If a protothread is run on top of an underlying 10 event-driven system,the protothread is scheduled whenever the event handler containing the protothread is invoked by the event scheduler ## 5. Prototype Implementation * Proof-of-concept implementation in pure ANSI C * –No changes to compiler * –No architecture specific machine code * Very simple implementation * Very low memory overhead * –Two bytes of RAM per protothread * No per-thread stacks ### 6. Example Implementation ```c# #define _scode static uint32_t _l=0;\ static uint64_t _ss=0;\ uint64_t _sc=0;\ switch(_l){case 0: _l=0; #define _ecode };_l=0; _ss=0; _sc=0; #define _sleep(time) Sytm_getTickCount(&_ss);_l=_ln; case _ln:\ Sytm_getTickCount(&_sc);\ if((_sc-_ss)◄ `_wtc(condition); ` 等待条件满足 `_scode` 开始 `_ecode` 结束 `_sleep(time)` 休眠 # Licesne **SPDX-License-Identifier: Apache-2.0** Copyright in some of the content available in this BSP belongs to third parties. Third parties license is specified in a file header or license file. M031 BSP files are provided under the Apache-2.0 license.