async_task.h
1 /*
2  * async_task.h
3  *
4  * Created on: 3 May 2017
5  * Author: billy
6  */
7 
8 #ifndef CORE_SERVER_TASK_INCLUDE_ASYNC_TASK_H_
9 #define CORE_SERVER_TASK_INCLUDE_ASYNC_TASK_H_
10 
12 #include "typedefs.h"
13 #include "linked_list.h"
14 #include "sync_data.h"
15 #include "memory_allocations.h"
16 #include "event_consumer.h"
17 
18 struct AsyncTasksManager;
19 
24 typedef struct AsyncTask
25 {
27  char *at_name_s;
28 
34 
37 
44  void *(*at_run_fn) (void *data_p);
45 
49  void *at_data_p;
50 
55 
58 
59 
60 
65 
66 } AsyncTask;
67 
68 
72 typedef struct AsyncTaskNode
73 {
76 
79 
87 
88 
89 #ifdef __cplusplus
90 extern "C"
91 {
92 #endif
93 
94 
107 GRASSROOTS_TASK_API AsyncTask *AllocateAsyncTask (const char *name_s, struct AsyncTasksManager *manager_p, bool add_flag);
108 
109 
117 GRASSROOTS_TASK_API void FreeAsyncTask (AsyncTask *task_p);
118 
119 
134 GRASSROOTS_TASK_API bool InitialiseAsyncTask (AsyncTask *task_p, const char *name_s, struct AsyncTasksManager *manager_p, bool add_flag);
135 
136 
144 GRASSROOTS_TASK_API void ClearAsyncTask (AsyncTask *task_p);
145 
146 
147 
148 
155 GRASSROOTS_TASK_API void CloseAsyncTask (AsyncTask *task_p);
156 
157 
169 GRASSROOTS_TASK_API bool SetAsyncTaskSyncData (AsyncTask *task_p, SyncData *sync_data_p, MEM_FLAG mem);
170 
182 GRASSROOTS_TASK_API void SetAsyncTaskRunData (AsyncTask *task_p, void *(*run_fn) (void *data_p), void *data_p);
183 
184 
185 
194 GRASSROOTS_TASK_API bool IsAsyncTaskRunning (const AsyncTask *task_p);
195 
196 
205 GRASSROOTS_TASK_API bool RunAsyncTask (AsyncTask *task_p);
206 
207 
214 GRASSROOTS_TASK_API bool CloseAllAsyncTasks (void);
215 
216 
226 GRASSROOTS_TASK_API AsyncTaskNode *AllocateAsyncTaskNode (AsyncTask *task_p, MEM_FLAG mem);
227 
228 
235 GRASSROOTS_TASK_API void FreeAsyncTaskNode (ListItem *node_p);
236 
237 
247 GRASSROOTS_TASK_API void SetAsyncTaskConsumer (AsyncTask *task_p, EventConsumer *consumer_p, MEM_FLAG mem);
248 
249 
256 GRASSROOTS_TASK_API void RunEventConsumerFromAsyncTask (AsyncTask *task_p);
257 
258 
259 
260 
261 
262 #ifdef __cplusplus
263 }
264 #endif
265 
266 
267 #endif /* CORE_SERVER_TASK_INCLUDE_ASYNC_TASK_H_ */
event_consumer.h
A doubly-linked node that points to its predecessor and successor.
Definition: linked_list.h:43
void ClearAsyncTask(AsyncTask *task_p)
Clear an AsyncTask.
void CloseAsyncTask(AsyncTask *task_p)
Stop a the given AsyncTask if it is currently running.
MEM_FLAG
An enum specifying the particular status of a piece of dynamically allocated memory for a particular ...
Definition: memory_allocations.h:36
bool SetAsyncTaskSyncData(AsyncTask *task_p, SyncData *sync_data_p, MEM_FLAG mem)
Set the SyncData for an AsyncTask.
bool RunAsyncTask(AsyncTask *task_p)
Run an AsyncTask.
AsyncTaskNode * AllocateAsyncTaskNode(AsyncTask *task_p, MEM_FLAG mem)
Create an AsyncTaskNode.
MEM_FLAG atn_mem
The memory flag indicating what action to perform upon atn_task_p when this AsyncTaskNode is freed.
Definition: async_task.h:85
EventConsumer * at_consumer_p
An EventConsumer to notify when the AsyncTask has finished running.
Definition: async_task.h:54
MEM_FLAG at_sync_data_mem
The memory flag indicating the ownership of the SyncData.
Definition: async_task.h:36
A doubly-linked list.
struct AsyncTasksManager * at_manager_p
The AsyncTasksManager that has this AsyncTask.
Definition: async_task.h:64
bool InitialiseAsyncTask(AsyncTask *task_p, const char *name_s, struct AsyncTasksManager *manager_p, bool add_flag)
Initialise an AsyncTask.
A datatype for storing AsyncTasks in a collection.
Definition: async_tasks_manager.h:24
void * at_data_p
Any custom data that will be passed to at_fun_fn.
Definition: async_task.h:49
struct SyncData * at_sync_data_p
The SyncData used to coordinate data access between the AsyncTask and other processes.
Definition: async_task.h:33
void FreeAsyncTask(AsyncTask *task_p)
Free an AsyncTask.
AsyncTask * atn_task_p
The AsyncTask to store on the list.
Definition: async_task.h:78
char * at_name_s
The name of the AsyncTask.
Definition: async_task.h:27
void SetAsyncTaskRunData(AsyncTask *task_p, void *(*run_fn)(void *data_p), void *data_p)
Set the callback function that an AsyncTask will call when it is ran.
AsyncTask * AllocateAsyncTask(const char *name_s, struct AsyncTasksManager *manager_p, bool add_flag)
Create an AsyncTask.
A datatype to use to run tasks asynchronously.
Definition: async_task.h:24
bool IsAsyncTaskRunning(const AsyncTask *task_p)
Check whether an AsyncTask is currently running.
void SetAsyncTaskConsumer(AsyncTask *task_p, EventConsumer *consumer_p, MEM_FLAG mem)
Set the EventConsumer for a given AsyncTask.
MEM_FLAG at_consumer_mem
The memory flag indicating the ownership of the EventConsumer.
Definition: async_task.h:57
A datatype that allows data to be shared between tasks (e.g.
Definition: unix_sync_data.h:30
void FreeAsyncTaskNode(ListItem *node_p)
Free an AsyncTaskNode.
A datatype to allow storing AsynTasks on a LinkedList.
Definition: async_task.h:72
ListItem atn_node
The base list node.
Definition: async_task.h:75
void RunEventConsumerFromAsyncTask(AsyncTask *task_p)
Run the EventConsumer for the given AsyncTask.
An EvenetConsumer is a datatype to allow an object to be notified when an AsyncTask has completed run...
Definition: event_consumer.h:44