Worker Thread Utility

group group_worker_thread_util

Worker thread utility that allows functions to be run a different thread context.

This utility can be used to delegate work that is not timing critical. For example, scheduling work in interrupt handlers to keep handler execution times low or if some work needs to be done at a different priority.

Defines

CY_WORKER_THREAD_DEFAULT_NAME

< Default worker thread name

CY_WORKER_DEFAULT_ENTRIES

Default number of work items in the queue.

CY_WORKER_THREAD_ERR_THREAD_INVALID

Additional work cannot be enqueued because the worker thread has been terminated.

This can occur if cy_worker_thread_create was not called or cy_worker_thread_delete was called before calling cy_worker_thread_enqueue

Typedefs

void() cy_worker_thread_func_t (void *arg)

Worker thread function call prototype.

Enums

enum cy_worker_thread_state_t

cy_worker_thread_state_t: Thread state enumeration.

Values:

enumerator CY_WORKER_THREAD_INVALID

Worker Thread is in invalid state.

enumerator CY_WORKER_THREAD_VALID

Worker Thread is in valid state.

enumerator CY_WORKER_THREAD_ENQUEUING

Worker Thread is adding to the queue.

enumerator CY_WORKER_THREAD_TERMINATING

Worker Thread is starting to terminate.

enumerator CY_WORKER_THREAD_JOIN_COMPLETE

Worker Thread join is complete.

Functions

cy_rslt_t cy_worker_thread_create(cy_worker_thread_info_t *new_worker, const cy_worker_thread_params_t *params)

Create worker thread to handle running callbacks in a separate thread.

note

Calling this function twice on the same thread object ( cy_worker_thread_info_t) without calling cy_worker_thread_delete will cause memory leakage.

Return

The status of the worker thread creation request.

Parameters
  • [out] new_worker: pointer to cy_worker_thread_info_t structure to be filled when created.

  • [in] params: pointer to requested parameters for starting worker thread.

cy_rslt_t cy_worker_thread_delete(cy_worker_thread_info_t *old_worker)

Delete worker thread.

note

This function will wait for the thread to complete all pending work in the queue and exit before returning.

Return

The status of the deletion of the worker thread.

Parameters

cy_rslt_t cy_worker_thread_enqueue(cy_worker_thread_info_t *worker_info, cy_worker_thread_func_t *work_func, void *arg)

Queue work on a worker thread.

Call the given function in the worker thread context.

note

If the thread priority is below that of the current thread, you must yield to allow the worker thread to run. This can be done by calling cy_rtos_delay_milliseconds or by waiting on an RTOS object in all higher priority threads.

Return

The status of the queueing of work.

Parameters
  • [in] worker_info: pointer to worker_thread used to run function

  • [in] work_func: function to run

  • [in] arg: opaque arg to be used in function call

struct cy_worker_thread_params_t
#include <cy_worker_thread.h>

Worker Thread Parameters.

struct cy_worker_thread_info_t
#include <cy_worker_thread.h>

Worker Thread Information.