Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


use local variables in coroutine

Posted by ares.qi on July 4, 2006
hello richard

Do you kown protothread by Adam Dunkels? It's similar to coroutine and is more mature, i think. May be we can expand coroutine based on that.

I have a way to use local variables in each coroutines.

1. we add a field of void * pVar in corCRCB and the initial value is NULL
2. when we enter a coroutine and if pVar is NULL , then we enter the coroutine first time , so we can malloc some space for our local variables. But how many space is machine and compiler dependent.
3. all local variable must use pointers which can point to a space return by malloc.

for example

void vACoRoutineFunction( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )

//define local variables here
//do it before crSTART
int *a,*b; //we need 2 int variables
void **c; //we need 1 void* variable

void *pMalloc

if (xHandle->pVar == NULL)
pMalloc = pvPortMalloc( (sizeof)int + (sizeof)int + (sizeof)(void *) );
if (pMalloc)
a = pMalloc;
b = pMalloc +1 //in 32bit machine
c = pMalloc +1 //in 32bit machine
xHandle->pVar = pMalloc;

pMalloc = xHandle->pVar;
a = pMalloc;
b = pMalloc +1 //in 32bit machine
c = pMalloc +1 //in 32bit machine

// Co-routines must start with a call to crSTART().
crSTART( xHandle );

for( ;; )
*a = 10;
// It is fine to make a blocking call here,
crDELAY( xHandle, 10 );

//we can use local variables now
if (*a == 10)


// Co-routines must end with a call to crEND().

RE: use local variables in coroutine

Posted by Richard on July 4, 2006
I have seen older versions of protothreads, but not the latest.

Your idea is quite neat, but I'm not sure if it has many advantages over declaring the variables static in an array, as per the examples. Both ways will convert to code that uses a pointer offset, but the array version will be more portable (probably) with respect to data sizes and byte alignment of pointers.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS v10.2.0 is available for immediate download. MIT licensed, and including RISC-V and ARMv8-M (Cortex-M33) demos.

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Cadence Tensilica Cortes

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers





STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner