bsp-microej-async-worker  0.2.1
bsp-microej-async-worker
microej_async_worker.h
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2018-2019 MicroEJ Corp. All rights reserved.
5  * This library is provided in source code for use, modification and test, subject to license terms.
6  * Any modification of the source code will break MicroEJ Corp. warranties on the whole library.
7  */
8 
9 #ifndef MICROEJ_ASYNC_WORKER_H
10 #define MICROEJ_ASYNC_WORKER_H
11 
118 #include <stdint.h>
119 #include "sni.h"
120 #include "osal.h"
121 
122 #ifdef __cplusplus
123  extern "C" {
124 #endif
125 
127 typedef enum {
128  MICROEJ_ASYNC_WORKER_OK,
129  MICROEJ_ASYNC_WORKER_ERROR,
130  MICROEJ_ASYNC_WORKER_INVALID_ARGS
132 
133 
136 
139 
153  void* params;
155  struct {
156  MICROEJ_ASYNC_WORKER_action_t action; // Pointer to the action to execute asynchronously.
157  int32_t thread_id; // Id of the Java thread that is waiting for this job to complete ; SNI_ERROR if no thread is waiting.
158  MICROEJ_ASYNC_WORKER_job_t* next_free_job; // Next in the free jobs linked list.
159  } _intern;
160 };
161 
170 typedef struct {
171  int32_t job_count; // Maximum number of jobs.
172  MICROEJ_ASYNC_WORKER_job_t* free_jobs; // Linked list of free jobs
173  void* params; // Pointer to params array. Length of this array is job_count.
174  int32_t params_sizeof; // Size of the params union
175  int32_t waiting_threads_length; // Length of the waiting_threads array
176  int32_t* waiting_threads; // Array of waiting threads (circular list)
177  uint16_t waiting_thread_offset; // Offset of the first waiting thread. If equals to free_waiting_thread_offset: no waiting thread
178  uint16_t free_waiting_thread_offset; // Offset of the first free slot in waiting_threads array
179  OSAL_queue_handle_t jobs_queue; // Linked list of jobs
180  OSAL_task_handle_t task; // The task that executes this worker.
181  OSAL_mutex_handle_t mutex; // Mutex used for critical sections.
183 
194 #define MICROEJ_ASYNC_WORKER_worker_declare(_name, _job_count, _param_type, _waiting_list_size)\
195  _param_type _name ## _params[_job_count];\
196  MICROEJ_ASYNC_WORKER_job_t _name ## _jobs[_job_count];\
197  int32_t _name ## _waiting_threads[_waiting_list_size+1];\
198  MICROEJ_ASYNC_WORKER_handle_t _name = {\
199  .job_count = _job_count,\
200  .free_jobs = _name ## _jobs,\
201  .params = _name ## _params,\
202  .params_sizeof = sizeof(_param_type),\
203  .waiting_threads_length = _waiting_list_size+1,\
204  .waiting_threads = _name ## _waiting_threads,\
205  .waiting_thread_offset = 0,\
206  .free_waiting_thread_offset = 0\
207  }
208 
209 
222 MICROEJ_ASYNC_WORKER_status_t MICROEJ_ASYNC_WORKER_initialize(MICROEJ_ASYNC_WORKER_handle_t* async_worker, uint8_t* name, OSAL_task_stack_t stack, int32_t priority);
223 
250 
251 
267 MICROEJ_ASYNC_WORKER_status_t MICROEJ_ASYNC_WORKER_free_job(MICROEJ_ASYNC_WORKER_handle_t* async_worker, MICROEJ_ASYNC_WORKER_job_t* job);
268 
294 MICROEJ_ASYNC_WORKER_status_t MICROEJ_ASYNC_WORKER_async_exec(MICROEJ_ASYNC_WORKER_handle_t* async_worker, MICROEJ_ASYNC_WORKER_job_t* job, MICROEJ_ASYNC_WORKER_action_t action, SNI_callback on_done_callback);
295 
317 
327 
328 #ifdef __cplusplus
329  }
330 #endif
331 
332 #endif // MICROEJ_ASYNC_WORKER_H
333 
MICROEJ_ASYNC_WORKER_status_t MICROEJ_ASYNC_WORKER_async_exec(MICROEJ_ASYNC_WORKER_handle_t *async_worker, MICROEJ_ASYNC_WORKER_job_t *job, MICROEJ_ASYNC_WORKER_action_t action, SNI_callback on_done_callback)
Executes the given job asynchronously.
MICROEJ_ASYNC_WORKER_job_t * MICROEJ_ASYNC_WORKER_get_job_done(void)
Returns the job that has been executed.
void(* MICROEJ_ASYNC_WORKER_action_t)(MICROEJ_ASYNC_WORKER_job_t *job)
Pointer to a function to call asynchronously.
void * params
Pointers to the parameters.
A job to execute in a worker.
MICROEJ_ASYNC_WORKER_status_t MICROEJ_ASYNC_WORKER_async_exec_no_wait(MICROEJ_ASYNC_WORKER_handle_t *async_worker, MICROEJ_ASYNC_WORKER_job_t *job, MICROEJ_ASYNC_WORKER_action_t action)
Executes the given job asynchronously.
MICROEJ_ASYNC_WORKER_status_t
Return codes list.
struct MICROEJ_ASYNC_WORKER_job::@0 _intern
Structure internal data. Must not be modified.
MICROEJ_ASYNC_WORKER_status_t MICROEJ_ASYNC_WORKER_free_job(MICROEJ_ASYNC_WORKER_handle_t *async_worker, MICROEJ_ASYNC_WORKER_job_t *job)
Frees a job previously allocated with MICROEJ_ASYNC_WORKER_allocate_job().
MICROEJ_ASYNC_WORKER_job_t * MICROEJ_ASYNC_WORKER_allocate_job(MICROEJ_ASYNC_WORKER_handle_t *async_worker, SNI_callback sni_retry_callback)
Allocates a new job for the given worker.
MICROEJ_ASYNC_WORKER_status_t MICROEJ_ASYNC_WORKER_initialize(MICROEJ_ASYNC_WORKER_handle_t *async_worker, uint8_t *name, OSAL_task_stack_t stack, int32_t priority)
Initializes and starts a worker previously declared with MICROEJ_ASYNC_WORKER_worker_declare() macro...