]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Source/FreeRTOS-Plus-IoT-SDK/c_sdk/standard/common/taskpool/iot_taskpool.c
Update task pool so tasks and timer are allocated statically.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-IoT-SDK / c_sdk / standard / common / taskpool / iot_taskpool.c
index 2144ba5d803357baf868eec076ce09b18a356690..6f90c19afec6b74c9702bef18df72830d6878dd1 100644 (file)
@@ -42,7 +42,7 @@
 #include <string.h>\r
 \r
 #if !defined( configSUPPORT_STATIC_ALLOCATION ) || ( configSUPPORT_STATIC_ALLOCATION != 1 )\r
-       #error configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h to build this file.\r
+    #error configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h to build this file.\r
 #endif\r
 \r
 /* Task pool internal include. */\r
@@ -653,8 +653,16 @@ static IotTaskPoolError_t _createTaskPool( const IotTaskPoolInfo_t * const pInfo
 {\r
     TASKPOOL_FUNCTION_ENTRY( IOT_TASKPOOL_SUCCESS );\r
 \r
+    /* Static TCB structures and arrays to be used by statically allocated\r
+    worker tasks. */\r
+    static StaticTask_t workerTaskTCBs[ IOT_TASKPOOL_NUMBER_OF_WORKERS ];\r
+    static StackType_t workerTaskStacks[ IOT_TASKPOOL_NUMBER_OF_WORKERS ][ IOT_TASKPOOL_WORKER_STACK_SIZE_BYTES / sizeof( portSTACK_TYPE ) ];\r
+\r
+       /* Static structure to hold te software timer. */\r
+       static StaticTimer_t staticTimer;\r
+\r
     uint32_t threadsCreated = 0; /* Although initialised before use removing the initialiser here results in compiler warnings. */\r
-    char cTaskName[ 10 ];\r
+    char taskName[ 10 ];\r
 \r
     /* Check input values for consistency. */\r
     TASKPOOL_ON_NULL_ARG_GOTO_CLEANUP( pTaskPool );\r
@@ -666,14 +674,12 @@ static IotTaskPoolError_t _createTaskPool( const IotTaskPoolInfo_t * const pInfo
     _initTaskPoolControlStructures( pTaskPool );\r
 \r
     /* Create the timer for a new connection. */\r
-    pTaskPool->timer = xTimerCreate( NULL, portMAX_DELAY, pdFALSE, ( void * ) pTaskPool, _timerCallback );\r
-\r
-    if( pTaskPool->timer == NULL )\r
-    {\r
-        IotLogError( "Failed to create timer for task pool." );\r
-\r
-        TASKPOOL_SET_AND_GOTO_CLEANUP( IOT_TASKPOOL_NO_MEMORY );\r
-    }\r
+    pTaskPool->timer = xTimerCreateStatic( NULL, /* Text name for the timer, only used for debugging. */\r
+                                                  portMAX_DELAY, /* Timer period in ticks. */\r
+                                                  pdFALSE, /* pdFALSE means its a one-shot timer. */\r
+                                                 ( void * ) pTaskPool, /* Parameter passed into callback. */\r
+                                                 _timerCallback, /* Callback that executes when the timer expires. */\r
+                                                 &staticTimer ); /* Static storage for the timer's data structure. */\r
 \r
     /* The task pool will initialize the minimum number of threads requested by the user upon start.\r
     Note this tailored version of the task pool does not autoscale, but fixes the number of tasks\r
@@ -681,46 +687,25 @@ static IotTaskPoolError_t _createTaskPool( const IotTaskPoolInfo_t * const pInfo
     /* Create the minimum number of threads specified by the user, and if one fails shutdown and return error. */\r
     for( threadsCreated = 0; threadsCreated < pInfo->minThreads; )\r
     {\r
-        TaskHandle_t task = NULL;\r
-\r
         /* Generate a unique name for the task. */\r
-        snprintf( cTaskName, sizeof( cTaskName ), "pool%d", ( int ) threadsCreated );\r
-\r
-        BaseType_t res = xTaskCreate( _taskPoolWorker,\r
-                                      cTaskName,\r
-                                      pInfo->stackSize / sizeof( portSTACK_TYPE ), /* xTaskCreate() expects the stack size to be specified in words. */\r
-                                      pTaskPool,\r
-                                      pInfo->priority,\r
-                                      &task );\r
+        snprintf( taskName, sizeof( taskName ), "pool%d", ( int ) threadsCreated );\r
 \r
-        /* Create one thread. */\r
-        if( res == pdFALSE ) //_RB_ would not be needed if tasks are created statically.\r
-        {\r
-            IotLogError( "Could not create worker thread! Exiting..." );\r
+        xTaskCreateStatic( _taskPoolWorker, /* Function that implements the task. */\r
+                           taskName,       /* Text name for the task, used for debugging only. */\r
+                                      IOT_TASKPOOL_WORKER_STACK_SIZE_BYTES / sizeof( portSTACK_TYPE ), /* xTaskCreate() expects the stack size to be specified in words. */\r
+                           pTaskPool,       /* Parameter passed into the task. */\r
+                           pInfo->priority, /* Priority at which the task starts running. */\r
+                           &( workerTaskStacks[ threadsCreated ][ 0 ] ), /* Pointer to static storage for the task's stack. */\r
+                                                  &( workerTaskTCBs[ threadsCreated ] ) ); /* Pointer to static storage for te task's TCB. */\r
 \r
-            /* If creating one thread fails, set error condition and exit the loop. */\r
-            TASKPOOL_SET_AND_GOTO_CLEANUP( IOT_TASKPOOL_NO_MEMORY );\r
-        }\r
-\r
-        /* Upon successful thread creation, increase the number of active threads. */\r
+               /* Upon successful thread creation, increase the number of active threads. */\r
         pTaskPool->activeThreads++;\r
-        IotTaskPool_Assert( task != NULL );\r
-\r
         ++threadsCreated;\r
     }\r
+       pTaskPool->running = true;\r
 \r
     TASKPOOL_FUNCTION_CLEANUP();\r
 \r
-    /* In case of failure, wait on the created threads to exit. */\r
-    if( TASKPOOL_FAILED( status ) )\r
-    {\r
-        /* Set the exit condition for the newly created threads. */\r
-#pragma message( "Threads need to be created statically here to ensure creation cannot fail as there is no shutdown mechanism." )\r
-        _destroyTaskPool( pTaskPool );\r
-    }\r
-\r
-    pTaskPool->running = true;\r
-\r
     TASKPOOL_FUNCTION_CLEANUP_END();\r
 }\r
 \r