Task Pool API Reference
Task pool library
iot_taskpool_internal.h
Go to the documentation of this file.
1 /*
2  * Amazon FreeRTOS Common V1.0.0
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://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25 
32 #ifndef IOT_TASKPOOL_INTERNAL_H_
33 #define IOT_TASKPOOL_INTERNAL_H_
34 
35 /* The config header is always included first. */
36 #include "iot_config.h"
37 
38 /* Task pool include. */
39 #include "private/iot_error.h"
40 #include "iot_taskpool.h"
41 
42 /* FreeRTOS includes. */
43 #include "FreeRTOS.h"
44 #include "semphr.h"
45 #include "timers.h"
46 
47 /* Establish a few convenience macros to handle errors in a standard way. */
48 
52 #define TASKPOOL_SUCCEEDED( x ) ( ( x ) == IOT_TASKPOOL_SUCCESS )
53 
57 #define TASKPOOL_FAILED( x ) ( ( x ) != IOT_TASKPOOL_SUCCESS )
58 
62 #define TASKPOOL_GOTO_CLEANUP() IOT_GOTO_CLEANUP()
63 
67 #define TASKPOOL_FUNCTION_ENTRY( result ) IOT_FUNCTION_ENTRY( IotTaskPoolError_t, result )
68 
72 #define TASKPOOL_ON_ERROR_GOTO_CLEANUP( expr ) \
73  { if( TASKPOOL_FAILED( status = ( expr ) ) ) { IOT_GOTO_CLEANUP(); } \
74  }
75 
79 #define TASKPOOL_ON_NULL_ARG_GOTO_CLEANUP( ptr ) IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ptr != NULL ) )
80 
84 #define TASKPOOL_ON_ARG_ERROR_GOTO_CLEANUP( expr ) IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ( expr ) == false ) )
85 
89 #define TASKPOOL_SET_AND_GOTO_CLEANUP( expr ) IOT_SET_AND_GOTO_CLEANUP( expr )
90 
94 #define TASKPOOL_FUNCTION_CLEANUP() IOT_FUNCTION_CLEANUP_BEGIN()
95 
99 #define TASKPOOL_FUNCTION_CLEANUP_END() IOT_FUNCTION_CLEANUP_END()
100 
104 #define TASKPOOL_NO_FUNCTION_CLEANUP() IOT_FUNCTION_EXIT_NO_CLEANUP()
105 
109 #define TASKPOOL_NO_FUNCTION_CLEANUP_NOLABEL() return status
110 
120 #if IOT_TASKPOOL_ENABLE_ASSERTS == 1
121  #ifndef IotTaskPool_Assert
122  #ifdef Iot_DefaultAssert
123  #define IotTaskPool_Assert( expression ) Iot_DefaultAssert( expression )
124  #else
125  #error "Asserts are enabled for Task Pool, but IotTaskPool_Assert is not defined"
126  #endif
127  #endif
128 #else
129  #define IotTaskPool_Assert( expression )
130 #endif
131 
132 /* Configure logs for TASKPOOL functions. */
133 #ifdef IOT_LOG_LEVEL_TASKPOOL
134  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_TASKPOOL
135 #else
136  #ifdef IOT_LOG_LEVEL_GLOBAL
137  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
138  #else
139  #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
140  #endif
141 #endif
142 
143 #define LIBRARY_LOG_NAME ( "TASKPOOL" )
144 #include "iot_logging_setup.h"
145 
146 /*
147  * Provide default values for undefined memory allocation functions based on
148  * the usage of dynamic memory allocation.
149  */
150 #if IOT_STATIC_MEMORY_ONLY == 1
151  #include "private/iot_static_memory.h"
152 
157  void * IotTaskPool_MallocJob( size_t size );
158 
164  void IotTaskPool_FreeJob( void * ptr );
165 
170  void * IotTaskPool_MallocTimerEvent( size_t size );
171 
176  void IotTaskPool_FreeTimerEvent( void * ptr );
177 
178 #else /* if IOT_STATIC_MEMORY_ONLY == 1 */
179  #ifndef IotTaskPool_MallocJob
180  #ifdef Iot_DefaultMalloc
181  #define IotTaskPool_MallocJob Iot_DefaultMalloc
182  #else
183  #error "No malloc function defined for IotTaskPool_MallocJob"
184  #endif
185  #endif
186 
187  #ifndef IotTaskPool_FreeJob
188  #ifdef Iot_DefaultFree
189  #define IotTaskPool_FreeJob Iot_DefaultFree
190  #else
191  #error "No free function defined for IotTaskPool_FreeJob"
192  #endif
193  #endif
194 
195  #ifndef IotTaskPool_MallocTimerEvent
196  #ifdef Iot_DefaultMalloc
197  #define IotTaskPool_MallocTimerEvent Iot_DefaultMalloc
198  #else
199  #error "No malloc function defined for IotTaskPool_MallocTimerEvent"
200  #endif
201  #endif
202 
203  #ifndef IotTaskPool_FreeTimerEvent
204  #ifdef Iot_DefaultFree
205  #define IotTaskPool_FreeTimerEvent Iot_DefaultFree
206  #else
207  #error "No free function defined for IotTaskPool_FreeTimerEvent"
208  #endif
209  #endif
210 #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
211 
212 /* ---------------------------------------------------------------------------------------------- */
213 
220 #define IOT_TASK_POOL_INTERNAL_STATIC ( ( uint32_t ) 0x00000001 ) /* Flag to mark a job as user-allocated. */
221 
230 typedef struct _taskPoolCache
231 {
234  uint32_t freeCount;
236 
245 typedef struct _taskPool
246 {
250  uint32_t activeThreads;
251  int32_t priority;
252  SemaphoreHandle_t dispatchSignal;
253  StaticSemaphore_t dispatchSignalBuffer;
254  StaticSemaphore_t startStopSignalBuffer;
255  TimerHandle_t timer;
256  StaticTimer_t timerBuffer;
257  bool running;
258 } _taskPool_t;
259 
267 typedef struct _taskPoolJob
268 {
271  void * pUserContext;
272  uint32_t flags;
275 
282 typedef struct _taskPoolTimerEvent
283 {
285  TickType_t expirationTime;
288 
289 #endif /* ifndef IOT_TASKPOOL_INTERNAL_H_ */
IotTaskPoolRoutine_t
void(* IotTaskPoolRoutine_t)(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext)
Callback type for a user callback.
Definition: iot_taskpool_types.h:259
_taskPoolJob_t
The job data structure keeps track of the user callback and context, as well as the status of the job...
Definition: iot_taskpool_internal.h:267
IotTaskPool_FreeJob
void IotTaskPool_FreeJob(void *ptr)
Free an IotTaskPoolJob_t. This function should have the same same signature as [malloc]....
_taskPoolCache_t::freeCount
uint32_t freeCount
A counter to track the number of jobs in the cache.
Definition: iot_taskpool_internal.h:234
_taskPool_t::dispatchSignal
SemaphoreHandle_t dispatchSignal
The synchronization object on which threads are waiting for incoming jobs.
Definition: iot_taskpool_internal.h:252
IotTaskPool_MallocTimerEvent
void * IotTaskPool_MallocTimerEvent(size_t size)
Allocate an _taskPoolTimerEvent_t. This function should have the same signature as [malloc].
_taskPool_t::timerBuffer
StaticTimer_t timerBuffer
The timer buffer.
Definition: iot_taskpool_internal.h:256
_taskPoolTimerEvent_t
Represents an operation that is subject to a timer.
Definition: iot_taskpool_internal.h:282
_taskPoolCache_t::freeList
IotListDouble_t freeList
A list ot hold cached jobs.
Definition: iot_taskpool_internal.h:232
_taskPoolJob_t::pUserContext
void * pUserContext
The user provided context.
Definition: iot_taskpool_internal.h:271
_taskPoolJob_t::status
IotTaskPoolJobStatus_t status
The status for the job.
Definition: iot_taskpool_internal.h:273
IotTaskPoolJobStatus_t
IotTaskPoolJobStatus_t
Status codes of task pool Job.
Definition: iot_taskpool_types.h:156
_taskPool_t
The task pool data structure keeps track of the internal state and the signals for the dispatcher thr...
Definition: iot_taskpool_internal.h:245
_taskPool_t::dispatchQueue
IotDeQueue_t dispatchQueue
The queue for the jobs waiting to be executed.
Definition: iot_taskpool_internal.h:247
_taskPoolJob_t::flags
uint32_t flags
Internal flags.
Definition: iot_taskpool_internal.h:272
IotTaskPool_MallocJob
void * IotTaskPool_MallocJob(size_t size)
Allocate an IotTaskPoolJob_t. This function should have the same signature as [malloc].
_taskPool_t::timer
TimerHandle_t timer
The timer for deferred jobs.
Definition: iot_taskpool_internal.h:255
_taskPool_t::running
bool running
A flag to track whether the task pool is operational or should shut down.
Definition: iot_taskpool_internal.h:257
_taskPool_t::activeThreads
uint32_t activeThreads
The number of threads in the task pool at any given time.
Definition: iot_taskpool_internal.h:250
_taskPoolJob_t::userCallback
IotTaskPoolRoutine_t userCallback
The user provided callback.
Definition: iot_taskpool_internal.h:270
_taskPoolTimerEvent_t::expirationTime
TickType_t expirationTime
When this event should be processed.
Definition: iot_taskpool_internal.h:285
iot_taskpool.h
User-facing functions of the task pool library.
iot_static_memory.h
_taskPool_t::dispatchSignalBuffer
StaticSemaphore_t dispatchSignalBuffer
The semaphore buffer.
Definition: iot_taskpool_internal.h:253
_taskPool_t::jobsCache
_taskPoolCache_t jobsCache
A cache to re-use jobs in order to limit memory allocations.
Definition: iot_taskpool_internal.h:249
_taskPool_t::timerEventsList
IotListDouble_t timerEventsList
The timeouts queue for all deferred jobs waiting to be executed.
Definition: iot_taskpool_internal.h:248
IotTaskPool_FreeTimerEvent
void IotTaskPool_FreeTimerEvent(void *ptr)
Free an _taskPoolTimerEvent_t. This function should have the same signature as[ free ].
_taskPool_t::startStopSignalBuffer
StaticSemaphore_t startStopSignalBuffer
The semaphore buffer.
Definition: iot_taskpool_internal.h:254
_taskPoolCache_t
Task pool jobs cache.
Definition: iot_taskpool_internal.h:230
_taskPoolTimerEvent_t::job
IotTaskPoolJob_t job
The task pool job associated with this event.
Definition: iot_taskpool_internal.h:286
iot_logging_setup.h
IotTaskPoolJob_t
struct _taskPoolJob * IotTaskPoolJob_t
Opaque handle of a Task Pool Job.
Definition: iot_taskpool_types.h:246
_taskPoolJob_t::link
IotLink_t link
The link to insert the job in the dispatch queue.
Definition: iot_taskpool_internal.h:269
_taskPool_t::priority
int32_t priority
The priority for all task pool threads.
Definition: iot_taskpool_internal.h:251
_taskPoolTimerEvent_t::link
IotLink_t link
List link member.
Definition: iot_taskpool_internal.h:284