Skip to main content

tkl_thread | Thread

The tkl_thread interface creates, terminates, and inspects operating-system threads (tasks) in a multitasking environment. It is the kernel abstraction layer (TKL) port each platform implements on top of its RTOS; TAL and application code call it for portable task management.

Typesโ€‹

typedef void *TKL_THREAD_HANDLE;
typedef void (*THREAD_FUNC_T)(void *);
TypeDescription
TKL_THREAD_HANDLEOpaque handle to a thread.
THREAD_FUNC_TThread entry function. Receives the arg passed at creation.

Thread priority is one of the values defined in tuya_cloud_types.h, from TKL_THREAD_PRI_LOWEST (0) through TKL_THREAD_PRI_NORMAL (4) to TKL_THREAD_PRI_HIGHEST (8).

tkl_thread_createโ€‹

OPERATE_RET tkl_thread_create(TKL_THREAD_HANDLE *thread, const char *name, uint32_t stack_size,
uint32_t priority, const THREAD_FUNC_T func, void *const arg);

Creates a thread.

ParameterDescription
threadOutput parameter. Receives the handle of the created thread.
nameThread name.
stack_sizeStack size of the thread, in bytes.
priorityThread priority. See the priority values in tuya_cloud_types.h.
funcThe main thread function.
argArgument passed to func. Can be NULL.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_releaseโ€‹

OPERATE_RET tkl_thread_release(const TKL_THREAD_HANDLE thread);

Terminates a thread and releases its resources.

ParameterDescription
threadHandle of the thread to terminate.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_get_watermarkโ€‹

OPERATE_RET tkl_thread_get_watermark(const TKL_THREAD_HANDLE thread, uint32_t *watermark);

Gets the thread stack watermark (the minimum free stack space observed).

ParameterDescription
threadThread handle.
watermarkOutput parameter. Receives the watermark in bytes.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_get_idโ€‹

OPERATE_RET tkl_thread_get_id(TKL_THREAD_HANDLE *thread);

Gets the handle of the calling thread.

ParameterDescription
threadOutput parameter. Receives the current thread handle.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_set_self_nameโ€‹

OPERATE_RET tkl_thread_set_self_name(const char *name);

Sets the name of the calling thread.

ParameterDescription
nameNew name for the thread.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_is_selfโ€‹

OPERATE_RET tkl_thread_is_self(TKL_THREAD_HANDLE thread, BOOL_T *is_self);

Checks whether the specified thread is the calling thread.

ParameterDescription
threadThread handle.
is_selfOutput parameter. Set to TRUE if thread is the calling thread.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_get_priorityโ€‹

OPERATE_RET tkl_thread_get_priority(TKL_THREAD_HANDLE thread, int *priority);

Gets the priority of a thread.

ParameterDescription
threadThread handle. NULL refers to the current thread.
priorityOutput parameter. Receives the thread priority.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_set_priorityโ€‹

OPERATE_RET tkl_thread_set_priority(TKL_THREAD_HANDLE thread, int priority);

Sets the priority of a thread.

ParameterDescription
threadThread handle. NULL refers to the current thread.
priorityNew thread priority.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

tkl_thread_diagnoseโ€‹

OPERATE_RET tkl_thread_diagnose(TKL_THREAD_HANDLE thread);

Diagnoses a thread, for example by dumping its task stack.

ParameterDescription
threadThread handle.
  • Return value: OPRT_OK on success. Any other value indicates an error; see tuya_error_code.h.

See alsoโ€‹