void vACoRoutineFunction( CoRoutineHandle_t xHandle,
UBaseType_t uxIndex )
{
static char c = 'a';
// Co-routines must start with a call to crSTART().
crSTART( xHandle );
for( ;; )
{
// If we set c to equal 'b' here ...
c = 'b';
// ... then make a blocking call ...
crDELAY( xHandle, 10 );
// ... c will only be guaranteed to still
// equal 'b' here if it is declared static
// (as it is here).
}
// Co-routines must end with a call to crEND().
crEND();
}
共享堆栈的另一个结果是,能从协程函数本身调用可能导致协程阻塞的 API 函数,
而不能从协程调用的函数中调用。 例如:
void vACoRoutineFunction( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
{
// Co-routines must start with a call to crSTART().
crSTART( xHandle );
for( ;; )
{
// It is fine to make a blocking call here,
crDELAY( xHandle, 10 );
// but a blocking call cannot be made from within
// vACalledFunction().
vACalledFunction();
}
// Co-routines must end with a call to crEND().
crEND();
}
void vACalledFunction( void )
{
// Cannot make a blocking call here!
}
switch 语句的使用
FreeRTOS 下载中的默认协程实现不允许在 switch 语句中进行阻塞调用
。 例如:
void vACoRoutineFunction( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
{
// Co-routines must start with a call to crSTART().
crSTART( xHandle );
for( ;; )
{
// It is fine to make a blocking call here,
crDELAY( xHandle, 10 );
switch( aVariable )
{
case 1 : // Cannot make a blocking call here!
break;
default: // Or here!
}
}
// Co-routines must end with a call to crEND().
crEND();
}
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.