2 * Amazon FreeRTOS Common V1.0.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://aws.amazon.com/freertos
\r
23 * http://www.FreeRTOS.org
\r
27 * @file iot_taskpool_internal.h
\r
28 * @brief Internal header of task pool library. This header should not be included in
\r
29 * typical application code.
\r
32 #ifndef IOT_TASKPOOL_INTERNAL_H_
\r
33 #define IOT_TASKPOOL_INTERNAL_H_
\r
35 /* The config header is always included first. */
\r
36 #include "iot_config.h"
\r
38 /* Task pool include. */
\r
39 #include "private/iot_error.h"
\r
40 #include "iot_taskpool.h"
\r
42 /* FreeRTOS includes. */
\r
43 #include "FreeRTOS.h"
\r
47 /* Establish a few convenience macros to handle errors in a standard way. */
\r
50 * @brief Every public API return an enumeration value with an undelying value of 0 in case of success.
\r
52 #define TASKPOOL_SUCCEEDED( x ) ( ( x ) == IOT_TASKPOOL_SUCCESS )
\r
55 * @brief Every public API returns an enumeration value with an undelying value different than 0 in case of success.
\r
57 #define TASKPOOL_FAILED( x ) ( ( x ) != IOT_TASKPOOL_SUCCESS )
\r
60 * @brief Jump to the cleanup area.
\r
62 #define TASKPOOL_GOTO_CLEANUP() IOT_GOTO_CLEANUP()
\r
65 * @brief Declare the storage for the error status variable.
\r
67 #define TASKPOOL_FUNCTION_ENTRY( result ) IOT_FUNCTION_ENTRY( IotTaskPoolError_t, result )
\r
70 * @brief Check error and leave in case of failure.
\r
72 #define TASKPOOL_ON_ERROR_GOTO_CLEANUP( expr ) \
\r
73 { if( TASKPOOL_FAILED( status = ( expr ) ) ) { IOT_GOTO_CLEANUP(); } \
\r
77 * @brief Exit if an argument is NULL.
\r
79 #define TASKPOOL_ON_NULL_ARG_GOTO_CLEANUP( ptr ) IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ptr != NULL ) )
\r
82 * @brief Exit if an argument is NULL.
\r
84 #define TASKPOOL_ON_ARG_ERROR_GOTO_CLEANUP( expr ) IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ( expr ) == false ) )
\r
87 * @brief Set error and leave.
\r
89 #define TASKPOOL_SET_AND_GOTO_CLEANUP( expr ) IOT_SET_AND_GOTO_CLEANUP( expr )
\r
92 * @brief Initialize error and declare start of cleanup area.
\r
94 #define TASKPOOL_FUNCTION_CLEANUP() IOT_FUNCTION_CLEANUP_BEGIN()
\r
97 * @brief Initialize error and declare end of cleanup area.
\r
99 #define TASKPOOL_FUNCTION_CLEANUP_END() IOT_FUNCTION_CLEANUP_END()
\r
102 * @brief Create an empty cleanup area.
\r
104 #define TASKPOOL_NO_FUNCTION_CLEANUP() IOT_FUNCTION_EXIT_NO_CLEANUP()
\r
107 * @brief Does not create a cleanup area.
\r
109 #define TASKPOOL_NO_FUNCTION_CLEANUP_NOLABEL() return status
\r
112 * @def IotTaskPool_Assert( expression )
\r
113 * @brief Assertion macro for the Task pool library.
\r
115 * Set @ref IOT_TASKPOOL_ENABLE_ASSERTS to `1` to enable assertions in the Task pool
\r
118 * @param[in] expression Expression to be evaluated.
\r
120 #if IOT_TASKPOOL_ENABLE_ASSERTS == 1
\r
121 #ifndef IotTaskPool_Assert
\r
122 #include <assert.h>
\r
123 #define IotTaskPool_Assert( expression ) assert( expression )
\r
126 #define IotTaskPool_Assert( expression )
\r
129 /* Configure logs for TASKPOOL functions. */
\r
130 #ifdef IOT_LOG_LEVEL_TASKPOOL
\r
131 #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_TASKPOOL
\r
133 #ifdef IOT_LOG_LEVEL_GLOBAL
\r
134 #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
\r
136 #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
\r
140 #define LIBRARY_LOG_NAME ( "TASKPOOL" )
\r
141 #include "iot_logging_setup.h"
\r
144 * Provide default values for undefined memory allocation functions based on
\r
145 * the usage of dynamic memory allocation.
\r
147 #if IOT_STATIC_MEMORY_ONLY == 1
\r
148 #include "private/iot_static_memory.h"
\r
151 * @brief Allocate an #_taskPool_t. This function should have the
\r
152 * same signature as [malloc].
\r
154 void * IotTaskPool_MallocTaskPool( size_t size );
\r
157 * @brief Free an #_taskPool_t. This function should have the
\r
158 * same signature as [malloc].
\r
160 void IotTaskPool_FreeTaskPool( void * ptr );
\r
163 * @brief Allocate an #IotTaskPoolJob_t. This function should have the
\r
164 * same signature as [malloc].
\r
166 void * IotTaskPool_MallocJob( size_t size );
\r
169 * @brief Free an #IotTaskPoolJob_t. This function should have the same
\r
170 * same signature as [malloc].
\r
171 * (http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html).
\r
173 void IotTaskPool_FreeJob( void * ptr );
\r
176 * @brief Allocate an #_taskPoolTimerEvent_t. This function should have the
\r
177 * same signature as [malloc].
\r
179 void * IotTaskPool_MallocTimerEvent( size_t size );
\r
182 * @brief Free an #_taskPoolTimerEvent_t. This function should have the
\r
183 * same signature as[ free ].
\r
185 void IotTaskPool_FreeTimerEvent( void * ptr );
\r
187 #else /* if IOT_STATIC_MEMORY_ONLY == 1 */
\r
188 #include <stdlib.h>
\r
190 #ifndef IotTaskPool_MallocTaskPool
\r
191 #define IotTaskPool_MallocTaskPool malloc
\r
194 #ifndef IotTaskPool_FreeTaskPool
\r
195 #define IotTaskPool_FreeTaskPool free
\r
198 #ifndef IotTaskPool_MallocJob
\r
199 #define IotTaskPool_MallocJob malloc
\r
202 #ifndef IotTaskPool_FreeJob
\r
203 #define IotTaskPool_FreeJob free
\r
206 #ifndef IotTaskPool_MallocTimerEvent
\r
207 #define IotTaskPool_MallocTimerEvent malloc
\r
210 #ifndef IotTaskPool_FreeTimerEvent
\r
211 #define IotTaskPool_FreeTimerEvent free
\r
214 #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
\r
216 /* ---------------------------------------------------------------------------------------------- */
\r
219 * @cond DOXYGEN_IGNORE
\r
220 * Doxygen should ignore this section.
\r
222 * A macros to manage task pool memory allocation.
\r
224 #define IOT_TASK_POOL_INTERNAL_STATIC ( ( uint32_t ) 0x00000001 ) /* Flag to mark a job as user-allocated. */
\r
228 * @brief Task pool jobs cache.
\r
230 * @warning This is a system-level data type that should not be modified or used directly in any application.
\r
231 * @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
234 typedef struct _taskPoolCache
\r
236 IotListDouble_t freeList; /**< @brief A list ot hold cached jobs. */
\r
238 uint32_t freeCount; /**< @brief A counter to track the number of jobs in the cache. */
\r
239 } _taskPoolCache_t;
\r
242 * @brief The task pool data structure keeps track of the internal state and the signals for the dispatcher threads.
\r
243 * The task pool is a thread safe data structure.
\r
245 * @warning This is a system-level data type that should not be modified or used directly in any application.
\r
246 * @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
249 typedef struct _taskPool
\r
251 IotDeQueue_t dispatchQueue; /**< @brief The queue for the jobs waiting to be executed. */
\r
252 IotListDouble_t timerEventsList; /**< @brief The timeouts queue for all deferred jobs waiting to be executed. */
\r
253 _taskPoolCache_t jobsCache; /**< @brief A cache to re-use jobs in order to limit memory allocations. */
\r
254 uint32_t activeThreads; /**< @brief The number of threads in the task pool at any given time. */
\r
255 int32_t priority; /**< @brief The priority for all task pool threads. */
\r
256 SemaphoreHandle_t dispatchSignal; /**< @brief The synchronization object on which threads are waiting for incoming jobs. */
\r
257 StaticSemaphore_t dispatchSignalBuffer; /**< @brief The semaphore buffer. */
\r
258 SemaphoreHandle_t startStopSignal; /**< @brief The synchronization object for threads to signal start and stop condition. */
\r
259 StaticSemaphore_t startStopSignalBuffer; /**< @brief The semaphore buffer. */
\r
260 TimerHandle_t timer; /**< @brief The timer for deferred jobs. */
\r
261 StaticTimer_t timerBuffer; /**< @brief The timer buffer. */
\r
262 bool running; /**< @brief A flag to track whether the task pool is operational or should shut down. */
\r
266 * @brief The job data structure keeps track of the user callback and context, as well as the status of the job.
\r
268 * @warning This is a system-level data type that should not be modified or used directly in any application.
\r
269 * @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
272 typedef struct _taskPoolJob
\r
274 IotLink_t link; /**< @brief The link to insert the job in the dispatch queue. */
\r
275 IotTaskPoolRoutine_t userCallback; /**< @brief The user provided callback. */
\r
276 void * pUserContext; /**< @brief The user provided context. */
\r
277 uint32_t flags; /**< @brief Internal flags. */
\r
278 IotTaskPoolJobStatus_t status; /**< @brief The status for the job. */
\r
282 * @brief Represents an operation that is subject to a timer.
\r
284 * These events are queued per MQTT connection. They are sorted by their
\r
287 typedef struct _taskPoolTimerEvent
\r
289 IotLink_t link; /**< @brief List link member. */
\r
290 TickType_t expirationTime; /**< @brief When this event should be processed. */
\r
291 IotTaskPoolJob_t job; /**< @brief The task pool job associated with this event. */
\r
292 } _taskPoolTimerEvent_t;
\r
294 #endif /* ifndef IOT_TASKPOOL_INTERNAL_H_ */