]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/common/include/private/iot_taskpool_internal.h
Rename \FreeRTOS-Plus\Source\FreeRTOS-Plus-IoT-SDK to \FreeRTOS-Plus\Source\FreeRTOS...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-IoT-Libraries / c_sdk / standard / common / include / private / iot_taskpool_internal.h
diff --git a/FreeRTOS-Plus/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/common/include/private/iot_taskpool_internal.h b/FreeRTOS-Plus/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/common/include/private/iot_taskpool_internal.h
new file mode 100644 (file)
index 0000000..3779ff9
--- /dev/null
@@ -0,0 +1,293 @@
+/*\r
+ * Amazon FreeRTOS Common V1.0.0\r
+ * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://aws.amazon.com/freertos\r
+ * http://www.FreeRTOS.org\r
+ */\r
+\r
+/**\r
+ * @file iot_taskpool_internal.h\r
+ * @brief Internal header of task pool library. This header should not be included in\r
+ * typical application code.\r
+ */\r
+\r
+#ifndef IOT_TASKPOOL_INTERNAL_H_\r
+#define IOT_TASKPOOL_INTERNAL_H_\r
+\r
+/* The config header is always included first. */\r
+#include "iot_config.h"\r
+\r
+/* Task pool include. */\r
+#include "private/iot_error.h"\r
+#include "iot_taskpool.h"\r
+\r
+/* FreeRTOS includes. */\r
+#include "FreeRTOS.h"\r
+#include "semphr.h"\r
+#include "timers.h"\r
+\r
+/* Establish a few convenience macros to handle errors in a standard way. */\r
+\r
+/**\r
+ * @brief Every public API return an enumeration value with an undelying value of 0 in case of success.\r
+ */\r
+#define TASKPOOL_SUCCEEDED( x )               ( ( x ) == IOT_TASKPOOL_SUCCESS )\r
+\r
+/**\r
+ * @brief Every public API returns an enumeration value with an undelying value different than 0 in case of success.\r
+ */\r
+#define TASKPOOL_FAILED( x )                  ( ( x ) != IOT_TASKPOOL_SUCCESS )\r
+\r
+/**\r
+ * @brief Jump to the cleanup area.\r
+ */\r
+#define TASKPOOL_GOTO_CLEANUP()               IOT_GOTO_CLEANUP()\r
+\r
+/**\r
+ * @brief Declare the storage for the error status variable.\r
+ */\r
+#define  TASKPOOL_FUNCTION_ENTRY( result )    IOT_FUNCTION_ENTRY( IotTaskPoolError_t, result )\r
+\r
+/**\r
+ * @brief Check error and leave in case of failure.\r
+ */\r
+#define TASKPOOL_ON_ERROR_GOTO_CLEANUP( expr )                           \\r
+    { if( TASKPOOL_FAILED( status = ( expr ) ) ) { IOT_GOTO_CLEANUP(); } \\r
+    }\r
+\r
+/**\r
+ * @brief Exit if an argument is NULL.\r
+ */\r
+#define TASKPOOL_ON_NULL_ARG_GOTO_CLEANUP( ptr )      IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ptr != NULL ) )\r
+\r
+/**\r
+ * @brief Exit if an argument is NULL.\r
+ */\r
+#define TASKPOOL_ON_ARG_ERROR_GOTO_CLEANUP( expr )    IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ( expr ) == false ) )\r
+\r
+/**\r
+ * @brief Set error and leave.\r
+ */\r
+#define TASKPOOL_SET_AND_GOTO_CLEANUP( expr )         IOT_SET_AND_GOTO_CLEANUP( expr )\r
+\r
+/**\r
+ * @brief Initialize error and declare start of cleanup area.\r
+ */\r
+#define TASKPOOL_FUNCTION_CLEANUP()                   IOT_FUNCTION_CLEANUP_BEGIN()\r
+\r
+/**\r
+ * @brief Initialize error and declare end of cleanup area.\r
+ */\r
+#define TASKPOOL_FUNCTION_CLEANUP_END()               IOT_FUNCTION_CLEANUP_END()\r
+\r
+/**\r
+ * @brief Create an empty cleanup area.\r
+ */\r
+#define TASKPOOL_NO_FUNCTION_CLEANUP()                IOT_FUNCTION_EXIT_NO_CLEANUP()\r
+\r
+/**\r
+ * @brief Does not create a cleanup area.\r
+ */\r
+#define TASKPOOL_NO_FUNCTION_CLEANUP_NOLABEL()        return status\r
+\r
+/**\r
+ * @def IotTaskPool_Assert( expression )\r
+ * @brief Assertion macro for the Task pool library.\r
+ *\r
+ * Set @ref IOT_TASKPOOL_ENABLE_ASSERTS to `1` to enable assertions in the Task pool\r
+ * library.\r
+ *\r
+ * @param[in] expression Expression to be evaluated.\r
+ */\r
+#if IOT_TASKPOOL_ENABLE_ASSERTS == 1\r
+    #ifndef IotTaskPool_Assert\r
+        #include <assert.h>\r
+        #define IotTaskPool_Assert( expression )    assert( expression )\r
+    #endif\r
+#else\r
+    #define IotTaskPool_Assert( expression )\r
+#endif\r
+\r
+/* Configure logs for TASKPOOL functions. */\r
+#ifdef IOT_LOG_LEVEL_TASKPOOL\r
+    #define LIBRARY_LOG_LEVEL        IOT_LOG_LEVEL_TASKPOOL\r
+#else\r
+    #ifdef IOT_LOG_LEVEL_GLOBAL\r
+        #define LIBRARY_LOG_LEVEL    IOT_LOG_LEVEL_GLOBAL\r
+    #else\r
+        #define LIBRARY_LOG_LEVEL    IOT_LOG_NONE\r
+    #endif\r
+#endif\r
+\r
+#define LIBRARY_LOG_NAME    ( "TASKPOOL" )\r
+#include "iot_logging_setup.h"\r
+\r
+/*\r
+ * Provide default values for undefined memory allocation functions based on\r
+ * the usage of dynamic memory allocation.\r
+ */\r
+#if IOT_STATIC_MEMORY_ONLY == 1\r
+    #include "private/iot_static_memory.h"\r
+\r
+/**\r
+ * @brief Allocate an #_taskPool_t. This function should have the\r
+ * same signature as [malloc].\r
+ */\r
+    void * IotTaskPool_MallocTaskPool( size_t size );\r
+\r
+/**\r
+ * @brief Free an #_taskPool_t. This function should have the\r
+ * same signature as [malloc].\r
+ */\r
+    void IotTaskPool_FreeTaskPool( void * ptr );\r
+\r
+/**\r
+ * @brief Allocate an #IotTaskPoolJob_t. This function should have the\r
+ * same signature as [malloc].\r
+ */\r
+    void * IotTaskPool_MallocJob( size_t size );\r
+\r
+/**\r
+ * @brief Free an #IotTaskPoolJob_t. This function should have the same\r
+ * same signature as [malloc].\r
+ * (http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html).\r
+ */\r
+    void IotTaskPool_FreeJob( void * ptr );\r
+\r
+/**\r
+ * @brief Allocate an #_taskPoolTimerEvent_t. This function should have the\r
+ * same signature as [malloc].\r
+ */\r
+    void * IotTaskPool_MallocTimerEvent( size_t size );\r
+\r
+/**\r
+ * @brief Free an #_taskPoolTimerEvent_t. This function should have the\r
+ * same signature as[ free ].\r
+ */\r
+    void IotTaskPool_FreeTimerEvent( void * ptr );\r
+\r
+#else /* if IOT_STATIC_MEMORY_ONLY == 1 */\r
+    #include <stdlib.h>\r
+\r
+    #ifndef IotTaskPool_MallocTaskPool\r
+        #define IotTaskPool_MallocTaskPool    malloc\r
+    #endif\r
+\r
+    #ifndef IotTaskPool_FreeTaskPool\r
+        #define IotTaskPool_FreeTaskPool    free\r
+    #endif\r
+\r
+    #ifndef IotTaskPool_MallocJob\r
+        #define IotTaskPool_MallocJob    malloc\r
+    #endif\r
+\r
+    #ifndef IotTaskPool_FreeJob\r
+        #define IotTaskPool_FreeJob    free\r
+    #endif\r
+\r
+    #ifndef IotTaskPool_MallocTimerEvent\r
+        #define IotTaskPool_MallocTimerEvent    malloc\r
+    #endif\r
+\r
+    #ifndef IotTaskPool_FreeTimerEvent\r
+        #define IotTaskPool_FreeTimerEvent    free\r
+    #endif\r
+\r
+#endif /* if IOT_STATIC_MEMORY_ONLY == 1 */\r
+\r
+/* ---------------------------------------------------------------------------------------------- */\r
+\r
+/**\r
+ * @cond DOXYGEN_IGNORE\r
+ * Doxygen should ignore this section.\r
+ *\r
+ * A macros to manage task pool memory allocation.\r
+ */\r
+#define IOT_TASK_POOL_INTERNAL_STATIC    ( ( uint32_t ) 0x00000001 )      /* Flag to mark a job as user-allocated. */\r
+/** @endcond */\r
+\r
+/**\r
+ * @brief Task pool jobs cache.\r
+ *\r
+ * @warning This is a system-level data type that should not be modified or used directly in any application.\r
+ * @warning This is a system-level data type that can and will change across different versions of the platform, with no regards for backward compatibility.\r
+ *\r
+ */\r
+typedef struct _taskPoolCache\r
+{\r
+    IotListDouble_t freeList; /**< @brief A list ot hold cached jobs. */\r
+\r
+    uint32_t freeCount;       /**< @brief A counter to track the number of jobs in the cache. */\r
+} _taskPoolCache_t;\r
+\r
+/**\r
+ * @brief The task pool data structure keeps track of the internal state and the signals for the dispatcher threads.\r
+ * The task pool is a thread safe data structure.\r
+ *\r
+ * @warning This is a system-level data type that should not be modified or used directly in any application.\r
+ * @warning This is a system-level data type that can and will change across different versions of the platform, with no regards for backward compatibility.\r
+ *\r
+ */\r
+typedef struct _taskPool\r
+{\r
+    IotDeQueue_t dispatchQueue;              /**< @brief The queue for the jobs waiting to be executed. */\r
+    IotListDouble_t timerEventsList;         /**< @brief The timeouts queue for all deferred jobs waiting to be executed. */\r
+    _taskPoolCache_t jobsCache;              /**< @brief A cache to re-use jobs in order to limit memory allocations. */\r
+    uint32_t activeThreads;                  /**< @brief The number of threads in the task pool at any given time. */\r
+    int32_t priority;                        /**< @brief The priority for all task pool threads. */\r
+    SemaphoreHandle_t dispatchSignal;        /**< @brief The synchronization object on which threads are waiting for incoming jobs. */\r
+    StaticSemaphore_t dispatchSignalBuffer;  /**< @brief The semaphore buffer. */\r
+    StaticSemaphore_t startStopSignalBuffer; /**< @brief The semaphore buffer. */\r
+    TimerHandle_t timer;                     /**< @brief The timer for deferred jobs. */\r
+    StaticTimer_t timerBuffer;               /**< @brief The timer buffer. */\r
+    bool running;                            /**< @brief A flag to track whether the task pool is operational or should shut down. */\r
+} _taskPool_t;\r
+\r
+/**\r
+ * @brief The job data structure keeps track of the user callback and context, as well as the status of the job.\r
+ *\r
+ * @warning This is a system-level data type that should not be modified or used directly in any application.\r
+ * @warning This is a system-level data type that can and will change across different versions of the platform, with no regards for backward compatibility.\r
+ *\r
+ */\r
+typedef struct _taskPoolJob\r
+{\r
+    IotLink_t link;                    /**< @brief The link to insert the job in the dispatch queue. */\r
+    IotTaskPoolRoutine_t userCallback; /**< @brief The user provided callback. */\r
+    void * pUserContext;               /**< @brief The user provided context. */\r
+    uint32_t flags;                    /**< @brief Internal flags. */\r
+    IotTaskPoolJobStatus_t status;     /**< @brief The status for the job. */\r
+} _taskPoolJob_t;\r
+\r
+/**\r
+ * @brief Represents an operation that is subject to a timer.\r
+ *\r
+ * These events are queued per MQTT connection. They are sorted by their\r
+ * expiration time.\r
+ */\r
+typedef struct _taskPoolTimerEvent\r
+{\r
+    IotLink_t link;            /**< @brief List link member. */\r
+    TickType_t expirationTime; /**< @brief When this event should be processed. */\r
+    IotTaskPoolJob_t job;      /**< @brief The task pool job associated with this event. */\r
+} _taskPoolTimerEvent_t;\r
+\r
+#endif /* ifndef IOT_TASKPOOL_INTERNAL_H_ */
\ No newline at end of file