Skip to main content

tkl_thread | Thread

The file tkl_thread.c provides APIs for creating, terminating, and obtaining information about operating system threads (tasks) in a multi-tasking environment suitable for Tuya Smart devices. This file is automatically generated by the TuyaOS, with certain areas marked for developers to customize and implement relevant logic.

API Description

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);

Function

Creates a task thread.

Parameters

  • thread: Output parameter used to return the handle of the created thread.
  • name: Name of the thread.
  • stack_size: Stack size of the thread.
  • priority: Priority of the thread.
  • func: Function pointer that the thread will execute.
  • arg: Argument passed to the thread function.

Return Value

A return value of OPRT_OK indicates that the thread was successfully created, while other values indicate an error. Refer to tuya_error_code.h for specific error codes.

tkl_thread_release

OPERATE_RET tkl_thread_release(const TKL_THREAD_HANDLE thread);

Function

Terminates a thread and releases its resources.

Parameters

  • thread: Handle of the thread to be terminated.

Return Value

A return value of OPRT_OK indicates success, while other values indicate an error. Refer to tuya_error_code.h for detailed information.

tkl_thread_get_watermark

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

Function

Obtains the highest water mark (minimum remaining stack space) of the thread stack.

Parameters

  • thread: Thread handle.
  • watermark: Output parameter, the highest water mark in bytes.

Return Value

A return value of OPRT_OK indicates success, while other values indicate failure.

tkl_thread_get_id

OPERATE_RET tkl_thread_get_id(TKL_THREAD_HANDLE *thread);

Function

Obtains the handle of the current thread.

Parameters

  • thread: Output parameter, handle of the current thread.

Return Value

Always returns OPRT_OK, indicating a successful call.

tkl_thread_set_self_name

OPERATE_RET tkl_thread_set_self_name(const char* name);

Function

Sets the name of the current thread.

Parameters

  • name: New name of the thread.

Return Value

A return value of OPRT_OK indicates success, while other values indicate an error.

tkl_thread_is_self

OPERATE_RET tkl_thread_is_self(TKL_THREAD_HANDLE thread, BOOL_T* is_self);

Function

Determines if the specified thread is the current thread.

Parameters

  • thread: Thread handle.
  • is_self: Output parameter, indicates whether it is the current thread.

Return Value

A return value of OPRT_OK indicates success, while other values indicate failure.

tkl_thread_get_priority

OPERATE_RET tkl_thread_get_priority(TKL_THREAD_HANDLE thread, int *priority);

Function

Obtains the priority of a thread.

Parameters

  • thread: Thread handle. If NULL, it indicates obtaining the priority of the current thread.
  • priority: Output parameter, used to return the thread priority.

Return Value

A return value of OPRT_OK indicates success, while other values indicate failure.

tkl_thread_set_priority

OPERATE_RET tkl_thread_set_priority(TKL_THREAD_HANDLE thread, int priority);

Function

Sets the priority of a thread.

Parameters

  • thread: Thread handle. If NULL, it indicates setting the priority of the current thread.
  • priority: New priority of the thread.

Return Value

A return value of OPRT_OK indicates success, while other values indicate failure.

tkl_thread_diagnose

OPERATE_RET tkl_thread_diagnose(TKL_THREAD_HANDLE thread);

Function

Diagnoses a thread, such as printing task stack information.

Parameters

  • thread: Thread handle.

Return Value

A return value of OPRT_OK indicates a successful diagnosis.