Mstdlib-1.24.0
|
Typedefs | |
typedef struct M_thread_pipeline_task | M_thread_pipeline_task_t |
typedef struct M_thread_pipeline_steps | M_thread_pipeline_steps_t |
typedef struct M_thread_pipeline | M_thread_pipeline_t |
typedef M_bool(* | M_thread_pipeline_task_cb) (M_thread_pipeline_task_t *task) |
typedef void(* | M_thread_pipeline_taskfinish_cb) (M_thread_pipeline_task_t *task, M_thread_pipeline_result_t result) |
Enumerations | |
enum | M_thread_pipeline_flags_t { M_THREAD_PIPELINE_FLAG_NONE = 0 , M_THREAD_PIPELINE_FLAG_NOABORT = 1 << 0 } |
enum | M_thread_pipeline_result_t { M_THREAD_PIPELINE_RESULT_SUCCESS = 1 , M_THREAD_PIPELINE_RESULT_FAIL = 2 , M_THREAD_PIPELINE_RESULT_ABORT = 3 } |
Functions | |
M_thread_pipeline_steps_t * | M_thread_pipeline_steps_create (void) |
M_bool | M_thread_pipeline_steps_insert (M_thread_pipeline_steps_t *steps, M_thread_pipeline_task_cb task_cb) |
void | M_thread_pipeline_steps_destroy (M_thread_pipeline_steps_t *steps) |
M_thread_pipeline_t * | M_thread_pipeline_create (const M_thread_pipeline_steps_t *steps, int flags, M_thread_pipeline_taskfinish_cb finish_cb) |
M_bool | M_thread_pipeline_task_insert (M_thread_pipeline_t *pipeline, M_thread_pipeline_task_t *task) |
void | M_thread_pipeline_wait (M_thread_pipeline_t *pipeline, size_t queue_limit) |
size_t | M_thread_pipeline_queue_count (M_thread_pipeline_t *pipeline) |
M_bool | M_thread_pipeline_status (M_thread_pipeline_t *pipeline) |
void | M_thread_pipeline_destroy (M_thread_pipeline_t *pipeline) |
Implementation of a thread pipeline. Useful if there are a series of tasks which must be completed in order, and each task has one or more CPU or I/O intensive steps. This allows handoff to a dedicated thread for each step, while ensuring each task result is processed in a serialized manner. For CPU intensive workloads this helps in spreading load across multiple CPU cores, and also allows I/O to be embedded into a step that can run without blocking CPU.
Example:
typedef struct M_thread_pipeline_task M_thread_pipeline_task_t |
typedef struct M_thread_pipeline_steps M_thread_pipeline_steps_t |
typedef struct M_thread_pipeline M_thread_pipeline_t |
typedef M_bool(* M_thread_pipeline_task_cb) (M_thread_pipeline_task_t *task) |
User-defined callback for each step
[in] | task | User-defined task data to be operated on |
typedef void(* M_thread_pipeline_taskfinish_cb) (M_thread_pipeline_task_t *task, M_thread_pipeline_result_t result) |
User-defined, and required, callback at the completion of each task. This may be called:
[in] | task | User-defined task structure |
[in] | result | the result of the task, one of M_thread_pipeline_result_t. |
Result codes passed to M_thread_pipeline_taskfinish_cb()
Enumerator | |
---|---|
M_THREAD_PIPELINE_RESULT_SUCCESS | Task completed successfully |
M_THREAD_PIPELINE_RESULT_FAIL | Task failed – record error in user-defined task structure |
M_THREAD_PIPELINE_RESULT_ABORT | Task was forcibly aborted due to a failure of another task, or M_thread_pipeline_destroy() was called before completion |
M_thread_pipeline_steps_t * M_thread_pipeline_steps_create | ( | void | ) |
Initialize an empty pipeline step list
M_bool M_thread_pipeline_steps_insert | ( | M_thread_pipeline_steps_t * | steps, |
M_thread_pipeline_task_cb | task_cb | ||
) |
Insert a step into the task pipeline
[in] | steps | Initialized pipeline steps structure from M_thread_pipeline_steps_create() |
[in] | task_cb | Task to perform |
void M_thread_pipeline_steps_destroy | ( | M_thread_pipeline_steps_t * | steps | ) |
Destroy the task step list initialized with M_thread_pipeline_steps_create()
[in] | steps | Initialized piipeline steps structure from M_thread_pipeline_steps_create() |
M_thread_pipeline_t * M_thread_pipeline_create | ( | const M_thread_pipeline_steps_t * | steps, |
int | flags, | ||
M_thread_pipeline_taskfinish_cb | finish_cb | ||
) |
Initialize the thread pipeline with the various steps to be performed for each task. This will spawn one thread per step and immediately start all threads. There is no additional function to start the pipeline other than to insert each task to be processed.
[in] | steps | Pointer to steps to perform for each task. The passed in pointer is internally duplicated, so it may be destroyed immediately after this function returns. |
[in] | flags | One or more pipeline flags from M_thread_pipeline_flags_t |
[in] | finish_cb | Callback to be called after each task is completed. At a minimum, this should free the memory assocated with the task pointer. The finish_cb is not called from the same thread as enqueued it so proper thread concurrency protections (e.g. mutexes) must be in place. |
M_bool M_thread_pipeline_task_insert | ( | M_thread_pipeline_t * | pipeline, |
M_thread_pipeline_task_t * | task | ||
) |
Insert a task into the thread pipeline.
This function will enqueue tasks into an internal task list indefinitely and will not block. If it is desirable to cap the enqueued task list, please see M_thread_pipeline_wait() and M_thread_pipeline_queue_count().
[in] | pipeline | Pointer to the initialized thread pipeline returned from M_thread_pipeline_create(). |
[in] | task | User-defined task structure describing task to be perfomed for each step. It is the responsibility of the user to define their own private struct M_thread_pipeline_task with all members and necessary state tracking to perform each step callback. It is guaranteed that no more than 1 step will be accessing this structure in parallel. |
void M_thread_pipeline_wait | ( | M_thread_pipeline_t * | pipeline, |
size_t | queue_limit | ||
) |
Wait pipeline tasks/steps to complete to the task queue limit specified.
[in] | pipeline | Pipeline initialized with M_thread_pipeline_create() |
[in] | queue_limit | Will block until the queued task list is reduced to at least this size. Use 0 to wait until all tasks are completed. |
size_t M_thread_pipeline_queue_count | ( | M_thread_pipeline_t * | pipeline | ) |
Count of queued tasks, this includes the task currently being processed if any.
[in] | pipeline | Pipeline initialized with M_thread_pipeline_create() |
M_bool M_thread_pipeline_status | ( | M_thread_pipeline_t * | pipeline | ) |
Retrieve if the pipeline is in a good state. The only time a pipeline will not be in a good state is if a step failed.
void M_thread_pipeline_destroy | ( | M_thread_pipeline_t * | pipeline | ) |
Destroy the thread pipeline. If there are any outstanding tasks/steps, they will be aborted and return an abort error code to their finish_cb
[in] | pipeline | Pipeline initialized with M_thread_pipeline_create() |