AWS IoT Device SDK C: Task Pool
Task pool library
Return to main page ↑
iot_taskpool_internal.h
Go to the documentation of this file.
1 /*
2  * IoT Common V1.1.0
3  * Copyright (C) 2019 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 
29 #ifndef IOT_TASKPOOL_INTERNAL_H_
30 #define IOT_TASKPOOL_INTERNAL_H_
31 
32 /* The config header is always included first. */
33 #include "iot_config.h"
34 
35 /* Task pool include. */
36 #include "iot_error.h"
37 #include "iot_taskpool.h"
38 
39 /* Establish a few convenience macros to handle errors in a standard way. */
40 
44 #define TASKPOOL_SUCCEEDED( x ) ( ( x ) == IOT_TASKPOOL_SUCCESS )
45 
49 #define TASKPOOL_FAILED( x ) ( ( x ) != IOT_TASKPOOL_SUCCESS )
50 
54 #define TASKPOOL_GOTO_CLEANUP() IOT_GOTO_CLEANUP()
55 
59 #define TASKPOOL_FUNCTION_ENTRY( result ) IOT_FUNCTION_ENTRY( IotTaskPoolError_t, result )
60 
64 #define TASKPOOL_ON_ERROR_GOTO_CLEANUP( expr ) \
65  { if( TASKPOOL_FAILED( status = ( expr ) ) ) { IOT_GOTO_CLEANUP(); } \
66  }
67 
71 #define TASKPOOL_ON_NULL_ARG_GOTO_CLEANUP( ptr ) IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ptr != NULL ) )
72 
76 #define TASKPOOL_ON_ARG_ERROR_GOTO_CLEANUP( expr ) IOT_VALIDATE_PARAMETER( IOT_TASKPOOL, ( ( expr ) == false ) )
77 
81 #define TASKPOOL_SET_AND_GOTO_CLEANUP( expr ) IOT_SET_AND_GOTO_CLEANUP( expr )
82 
86 #define TASKPOOL_FUNCTION_CLEANUP() IOT_FUNCTION_CLEANUP_BEGIN()
87 
91 #define TASKPOOL_FUNCTION_CLEANUP_END() IOT_FUNCTION_CLEANUP_END()
92 
96 #define TASKPOOL_NO_FUNCTION_CLEANUP() IOT_FUNCTION_EXIT_NO_CLEANUP()
97 
101 #define TASKPOOL_NO_FUNCTION_CLEANUP_NOLABEL() return status
102 
112 #if IOT_TASKPOOL_ENABLE_ASSERTS == 1
113  #ifndef IotTaskPool_Assert
114  #ifdef Iot_DefaultAssert
115  #define IotTaskPool_Assert( expression ) Iot_DefaultAssert( expression )
116  #else
117  #error "Asserts are enabled for Task Pool, but IotTaskPool_Assert is not defined"
118  #endif
119  #endif
120 #else /* if IOT_TASKPOOL_ENABLE_ASSERTS == 1 */
121  #define IotTaskPool_Assert( expression )
122 #endif /* if IOT_TASKPOOL_ENABLE_ASSERTS == 1 */
123 
124 /* Configure logs for TASKPOOL functions. */
125 #ifdef IOT_LOG_LEVEL_TASKPOOL
126  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_TASKPOOL
127 #else
128  #ifdef IOT_LOG_LEVEL_GLOBAL
129  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
130  #else
131  #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
132  #endif
133 #endif
134 
135 #define LIBRARY_LOG_NAME ( "TASKPOOL" )
136 #include "iot_logging_setup.h"
137 
138 /*
139  * Provide default values for undefined memory allocation functions based on
140  * the usage of dynamic memory allocation.
141  */
142 #if IOT_STATIC_MEMORY_ONLY == 1
143  #include "iot_static_memory.h"
144 
149  void * IotTaskPool_MallocTaskPool( size_t size );
150 
155  void IotTaskPool_FreeTaskPool( void * ptr );
156 
161  void * IotTaskPool_MallocJob( size_t size );
162 
168  void IotTaskPool_FreeJob( void * ptr );
169 
174  void * IotTaskPool_MallocTimerEvent( size_t size );
175 
180  void IotTaskPool_FreeTimerEvent( void * ptr );
181 
182 #else /* if IOT_STATIC_MEMORY_ONLY == 1 */
183  #include <stdlib.h>
184 
185  #ifndef IotTaskPool_MallocTaskPool
186  #ifdef Iot_DefaultMalloc
187  #define IotTaskPool_MallocTaskPool Iot_DefaultMalloc
188  #else
189  #error "No malloc function defined for IotTaskPool_MallocTaskPool"
190  #endif
191  #endif
192 
193  #ifndef IotTaskPool_FreeTaskPool
194  #ifdef Iot_DefaultFree
195  #define IotTaskPool_FreeTaskPool Iot_DefaultFree
196  #else
197  #error "No free function defined for IotTaskPool_FreeTaskPool"
198  #endif
199  #endif
200 
201  #ifndef IotTaskPool_MallocJob
202  #ifdef Iot_DefaultMalloc
203  #define IotTaskPool_MallocJob Iot_DefaultMalloc
204  #else
205  #error "No malloc function defined for IotTaskPool_MallocJob"
206  #endif
207  #endif
208 
209  #ifndef IotTaskPool_FreeJob
210  #ifdef Iot_DefaultFree
211  #define IotTaskPool_FreeJob Iot_DefaultFree
212  #else
213  #error "No free function defined for IotTaskPool_FreeJob"
214  #endif
215  #endif
216 
217  #ifndef IotTaskPool_MallocTimerEvent
218  #ifdef Iot_DefaultMalloc
219  #define IotTaskPool_MallocTimerEvent Iot_DefaultMalloc
220  #else
221  #error "No malloc function defined for IotTaskPool_MallocTimerEvent"
222  #endif
223  #endif
224 
225  #ifndef IotTaskPool_FreeTimerEvent
226  #ifdef Iot_DefaultFree
227  #define IotTaskPool_FreeTimerEvent Iot_DefaultFree
228  #else
229  #error "No free function defined for IotTaskPool_FreeTimerEvent"
230  #endif
231  #endif
232 #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
233 
234 /* ---------------------------------------------------------------------------------------------- */
235 
242 #define IOT_TASK_POOL_INTERNAL_STATIC ( ( uint32_t ) 0x00000001 ) /* Flag to mark a job as user-allocated. */
243 
252 typedef struct _taskPoolCache
253 {
256  uint32_t freeCount;
258 
267 typedef struct _taskPool
268 {
272  uint32_t minThreads;
273  uint32_t maxThreads;
274  uint32_t activeThreads;
275  uint32_t activeJobs;
276  uint32_t stackSize;
277  int32_t priority;
282 } _taskPool_t;
283 
291 typedef struct _taskPoolJob
292 {
295  void * pUserContext;
296  uint32_t flags;
299 
306 typedef struct _taskPoolTimerEvent
307 {
309  uint64_t expirationTime;
312 
313 #endif /* ifndef IOT_TASKPOOL_INTERNAL_H_ */
_taskPoolCache_t jobsCache
A cache to re-use jobs in order to limit memory allocations.
Definition: iot_taskpool_internal.h:271
IotLink_t link
List link member.
Definition: iot_taskpool_internal.h:308
_taskPoolJob_t * pJob
The task pool job associated with this event.
Definition: iot_taskpool_internal.h:310
uint32_t maxThreads
The maximum number of threads for the task pool.
Definition: iot_taskpool_internal.h:273
The task pool data structure keeps track of the internal state and the signals for the dispatcher thr...
Definition: iot_taskpool_internal.h:267
User-facing functions of the task pool library.
IotDeQueue_t dispatchQueue
The queue for the jobs waiting to be executed.
Definition: iot_taskpool_internal.h:269
int32_t priority
The priority for all task pool threads.
Definition: iot_taskpool_internal.h:277
Task pool jobs cache.
Definition: iot_taskpool_internal.h:252
_IotSystemTimer_t IotTimer_t
void IotTaskPool_FreeTaskPool(void *ptr)
Free an _taskPool_t. This function should have the same signature as [malloc].
Definition: iot_taskpool_static_memory.c:89
void IotTaskPool_FreeJob(void *ptr)
Free an IotTaskPoolJob_t. This function should have the same same signature as [malloc]. (http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html).
Definition: iot_taskpool_static_memory.c:124
IotTaskPoolJobStatus_t status
The status for the job.
Definition: iot_taskpool_internal.h:297
void IotTaskPool_FreeTimerEvent(void *ptr)
Free an _taskPoolTimerEvent_t. This function should have the same signature as[ free ]...
Definition: iot_taskpool_static_memory.c:159
IotTaskPoolJobStatus_t
Status codes of task pool Job.
Definition: iot_taskpool_types.h:98
void * pUserContext
The user provided context.
Definition: iot_taskpool_internal.h:295
uint32_t flags
Internal flags.
Definition: iot_taskpool_internal.h:296
uint32_t activeJobs
The number of active jobs in the task pool at any given time.
Definition: iot_taskpool_internal.h:275
_IotSystemSemaphore_t IotSemaphore_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:291
void * IotTaskPool_MallocTaskPool(size_t size)
Allocate an _taskPool_t. This function should have the same signature as [malloc].
Definition: iot_taskpool_static_memory.c:67
IotTaskPoolRoutine_t userCallback
The user provided callback.
Definition: iot_taskpool_internal.h:294
_IotSystemMutex_t IotMutex_t
IotListDouble_t freeList
A list of hold cached jobs.
Definition: iot_taskpool_internal.h:254
uint32_t freeCount
A counter to track the number of jobs in the cache.
Definition: iot_taskpool_internal.h:256
void * IotTaskPool_MallocJob(size_t size)
Allocate an IotTaskPoolJob_t. This function should have the same signature as [malloc].
Definition: iot_taskpool_static_memory.c:101
IotListDouble_t timerEventsList
The timeouts queue for all deferred jobs waiting to be executed.
Definition: iot_taskpool_internal.h:270
uint64_t expirationTime
When this event should be processed.
Definition: iot_taskpool_internal.h:309
IotLink_t link
The link to insert the job in the dispatch queue.
Definition: iot_taskpool_internal.h:293
IotTimer_t timer
The timer for deferred jobs.
Definition: iot_taskpool_internal.h:280
IotMutex_t lock
The lock to protect the task pool data structure access.
Definition: iot_taskpool_internal.h:281
Represents an operation that is subject to a timer.
Definition: iot_taskpool_internal.h:306
void * IotTaskPool_MallocTimerEvent(size_t size)
Allocate an _taskPoolTimerEvent_t. This function should have the same signature as [malloc]...
Definition: iot_taskpool_static_memory.c:136
IotSemaphore_t dispatchSignal
The synchronization object on which threads are waiting for incoming jobs.
Definition: iot_taskpool_internal.h:278
uint32_t minThreads
The minimum number of threads for the task pool.
Definition: iot_taskpool_internal.h:272
IotSemaphore_t startStopSignal
The synchronization object for threads to signal start and stop condition.
Definition: iot_taskpool_internal.h:279
void(* IotTaskPoolRoutine_t)(IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext)
Callback type for a user callback.
Definition: iot_taskpool_types.h:203
uint32_t activeThreads
The number of threads in the task pool at any given time.
Definition: iot_taskpool_internal.h:274
uint32_t stackSize
The stack size for all task pool threads.
Definition: iot_taskpool_internal.h:276