Labor04
croutine.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS Kernel V10.0.0
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software. If you wish to use our Amazon
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * http://www.FreeRTOS.org
24  * http://aws.amazon.com/freertos
25  *
26  * 1 tab == 4 spaces!
27  */
28 
29 #ifndef CO_ROUTINE_H
30 #define CO_ROUTINE_H
31 
32 #ifndef INC_FREERTOS_H
33 #error "include FreeRTOS.h must appear in source files before include croutine.h"
34 #endif
35 
36 #include "list.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /* Used to hide the implementation of the co-routine control block. The
43 control block structure however has to be included in the header due to
44 the macro implementation of the co-routine functionality. */
45 typedef void * CoRoutineHandle_t;
46 
47 /* Defines the prototype to which co-routine functions must conform. */
49 
51 {
53  ListItem_t xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
54  ListItem_t xEventListItem; /*< List item used to place the CRCB in event lists. */
55  UBaseType_t uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
56  UBaseType_t uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
57  uint16_t uxState; /*< Used internally by the co-routine implementation. */
58 } CRCB_t; /* Co-routine control block. Note must be identical in size down to uxPriority with TCB_t. */
59 
132 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex );
133 
134 
174 void vCoRoutineSchedule( void );
175 
205 #define crSTART( pxCRCB ) switch( ( ( CRCB_t * )( pxCRCB ) )->uxState ) { case 0:
206 
236 #define crEND() }
237 
238 /*
239  * These macros are intended for internal use by the co-routine implementation
240  * only. The macros should not be used directly by application writers.
241  */
242 #define crSET_STATE0( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
243 #define crSET_STATE1( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
244 
291 #define crDELAY( xHandle, xTicksToDelay ) \
292  if( ( xTicksToDelay ) > 0 ) \
293  { \
294  vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
295  } \
296  crSET_STATE0( ( xHandle ) );
297 
381 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
382 { \
383  *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
384  if( *( pxResult ) == errQUEUE_BLOCKED ) \
385  { \
386  crSET_STATE0( ( xHandle ) ); \
387  *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
388  } \
389  if( *pxResult == errQUEUE_YIELD ) \
390  { \
391  crSET_STATE1( ( xHandle ) ); \
392  *pxResult = pdPASS; \
393  } \
394 }
395 
473 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
474 { \
475  *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), ( xTicksToWait ) ); \
476  if( *( pxResult ) == errQUEUE_BLOCKED ) \
477  { \
478  crSET_STATE0( ( xHandle ) ); \
479  *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), 0 ); \
480  } \
481  if( *( pxResult ) == errQUEUE_YIELD ) \
482  { \
483  crSET_STATE1( ( xHandle ) ); \
484  *( pxResult ) = pdPASS; \
485  } \
486 }
487 
582 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
583 
584 
695 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
696 
697 /*
698  * This function is intended for internal use by the co-routine macros only.
699  * The macro nature of the co-routine implementation requires that the
700  * prototype appears here. The function should not be used by application
701  * writers.
702  *
703  * Removes the current co-routine from its ready list and places it in the
704  * appropriate delayed list.
705  */
706 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList );
707 
708 /*
709  * This function is intended for internal use by the queue implementation only.
710  * The function should not be used by application writers.
711  *
712  * Removes the highest priority co-routine from the event list and places it in
713  * the pending ready list.
714  */
716 
717 #ifdef __cplusplus
718 }
719 #endif
720 
721 #endif /* CO_ROUTINE_H */
listSET_LIST_ITEM_OWNER
#define listSET_LIST_ITEM_OWNER(pxListItem, pxOwner)
Definition: list.h:180
croutine.h
task.h
xLIST
Definition: list.h:165
CoRoutineHandle_t
void * CoRoutineHandle_t
Definition: croutine.h:45
vListInitialise
void vListInitialise(List_t *const pxList)
Definition: list.c:38
corCoRoutineControlBlock::uxState
uint16_t uxState
Definition: croutine.h:57
configMAX_CO_ROUTINE_PRIORITIES
#define configMAX_CO_ROUTINE_PRIORITIES
Definition: FreeRTOSConfig.h:21
xLIST_ITEM
Definition: list.h:141
errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY
#define errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY
Definition: projdefs.h:54
vCoRoutineAddToDelayedList
void vCoRoutineAddToDelayedList(TickType_t xTicksToDelay, List_t *pxEventList)
uxListRemove
UBaseType_t uxListRemove(ListItem_t *const pxItemToRemove)
Definition: list.c:171
listGET_OWNER_OF_HEAD_ENTRY
#define listGET_OWNER_OF_HEAD_ENTRY(pxList)
Definition: list.h:307
pdPASS
#define pdPASS
Definition: projdefs.h:48
TickType_t
uint32_t TickType_t
Definition: portmacro.h:64
xLIST_ITEM::pvContainer
void *configLIST_VOLATILE pvContainer
Definition: list.h:147
xCoRoutineCreate
BaseType_t xCoRoutineCreate(crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex)
corCoRoutineControlBlock
Definition: croutine.h:51
listGET_LIST_ITEM_VALUE
#define listGET_LIST_ITEM_VALUE(pxListItem)
Definition: list.h:208
listSET_LIST_ITEM_VALUE
#define listSET_LIST_ITEM_VALUE(pxListItem, xValue)
Definition: list.h:198
corCoRoutineControlBlock::uxIndex
UBaseType_t uxIndex
Definition: croutine.h:56
corCoRoutineControlBlock::xEventListItem
ListItem_t xEventListItem
Definition: croutine.h:54
pdFALSE
#define pdFALSE
Definition: projdefs.h:45
vListInsertEnd
void vListInsertEnd(List_t *const pxList, ListItem_t *const pxNewListItem)
Definition: list.c:75
UBaseType_t
unsigned long UBaseType_t
Definition: portmacro.h:58
portDISABLE_INTERRUPTS
#define portDISABLE_INTERRUPTS()
Definition: portmacro.h:105
listLIST_IS_EMPTY
#define listLIST_IS_EMPTY(pxList)
Definition: list.h:250
vListInsert
void vListInsert(List_t *const pxList, ListItem_t *const pxNewListItem)
Definition: list.c:104
FreeRTOS.h
corCoRoutineControlBlock::xGenericListItem
ListItem_t xGenericListItem
Definition: croutine.h:53
portENABLE_INTERRUPTS
#define portENABLE_INTERRUPTS()
Definition: portmacro.h:106
corCoRoutineControlBlock::uxPriority
UBaseType_t uxPriority
Definition: croutine.h:55
corCoRoutineControlBlock::pxCoRoutineFunction
crCOROUTINE_CODE pxCoRoutineFunction
Definition: croutine.h:52
BaseType_t
long BaseType_t
Definition: portmacro.h:57
pdTRUE
#define pdTRUE
Definition: projdefs.h:46
xTaskGetTickCount
TickType_t xTaskGetTickCount(void) PRIVILEGED_FUNCTION
Definition: tasks.c:2214
vCoRoutineSchedule
void vCoRoutineSchedule(void)
listGET_OWNER_OF_NEXT_ENTRY
#define listGET_OWNER_OF_NEXT_ENTRY(pxTCB, pxList)
Definition: list.h:277
crCOROUTINE_CODE
void(* crCOROUTINE_CODE)(CoRoutineHandle_t, UBaseType_t)
Definition: croutine.h:48
CRCB_t
struct corCoRoutineControlBlock CRCB_t
xCoRoutineRemoveFromEventList
BaseType_t xCoRoutineRemoveFromEventList(const List_t *pxEventList)
list.h
vListInitialiseItem
void vListInitialiseItem(ListItem_t *const pxItem)
Definition: list.c:63
pvPortMalloc
void * pvPortMalloc(size_t xWantedSize)
Definition: heap_1.c:71