mTower
croutine.h
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 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.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27 
28 #ifndef CO_ROUTINE_H
29 #define CO_ROUTINE_H
30 
31 #ifndef INC_FREERTOS_H
32  #error "include FreeRTOS.h must appear in source files before include croutine.h"
33 #endif
34 
35 #include "list.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* Used to hide the implementation of the co-routine control block. The
42 control block structure however has to be included in the header due to
43 the macro implementation of the co-routine functionality. */
44 typedef void * CoRoutineHandle_t;
45 
46 /* Defines the prototype to which co-routine functions must conform. */
47 typedef void (*crCOROUTINE_CODE)( CoRoutineHandle_t, UBaseType_t );
48 
50 {
51  crCOROUTINE_CODE pxCoRoutineFunction;
52  ListItem_t xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
53  ListItem_t xEventListItem; /*< List item used to place the CRCB in event lists. */
54  UBaseType_t uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
55  UBaseType_t uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
56  uint16_t uxState; /*< Used internally by the co-routine implementation. */
57 } CRCB_t; /* Co-routine control block. Note must be identical in size down to uxPriority with TCB_t. */
58 
131 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex );
132 
133 
173 void vCoRoutineSchedule( void );
174 
204 #define crSTART( pxCRCB ) switch( ( ( CRCB_t * )( pxCRCB ) )->uxState ) { case 0:
205 
235 #define crEND() }
236 
237 /*
238  * These macros are intended for internal use by the co-routine implementation
239  * only. The macros should not be used directly by application writers.
240  */
241 #define crSET_STATE0( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
242 #define crSET_STATE1( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
243 
290 #define crDELAY( xHandle, xTicksToDelay ) \
291  if( ( xTicksToDelay ) > 0 ) \
292  { \
293  vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
294  } \
295  crSET_STATE0( ( xHandle ) );
296 
380 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
381 { \
382  *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
383  if( *( pxResult ) == errQUEUE_BLOCKED ) \
384  { \
385  crSET_STATE0( ( xHandle ) ); \
386  *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
387  } \
388  if( *pxResult == errQUEUE_YIELD ) \
389  { \
390  crSET_STATE1( ( xHandle ) ); \
391  *pxResult = pdPASS; \
392  } \
393 }
394 
472 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
473 { \
474  *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), ( xTicksToWait ) ); \
475  if( *( pxResult ) == errQUEUE_BLOCKED ) \
476  { \
477  crSET_STATE0( ( xHandle ) ); \
478  *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), 0 ); \
479  } \
480  if( *( pxResult ) == errQUEUE_YIELD ) \
481  { \
482  crSET_STATE1( ( xHandle ) ); \
483  *( pxResult ) = pdPASS; \
484  } \
485 }
486 
581 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
582 
583 
694 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
695 
696 /*
697  * This function is intended for internal use by the co-routine macros only.
698  * The macro nature of the co-routine implementation requires that the
699  * prototype appears here. The function should not be used by application
700  * writers.
701  *
702  * Removes the current co-routine from its ready list and places it in the
703  * appropriate delayed list.
704  */
705 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList );
706 
707 /*
708  * This function is intended for internal use by the queue implementation only.
709  * The function should not be used by application writers.
710  *
711  * Removes the highest priority co-routine from the event list and places it in
712  * the pending ready list.
713  */
714 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList );
715 
716 #ifdef __cplusplus
717 }
718 #endif
719 
720 #endif /* CO_ROUTINE_H */
Definition: croutine.h:49
Definition: list.h:140
Definition: list.h:164