更新时间:2024-01-16 08:08:41下载pdf
TuyaOS 框架封装了统一的线程管理接口,您可跨平台(RTOS & Linux 环境)调用。
tal_thread.h
/**
* @brief thread priority
*
*/
typedef enum {
THREAD_PRIO_0 = 5, /*High*/
THREAD_PRIO_1 = 4,
THREAD_PRIO_2 = 3,
THREAD_PRIO_3 = 2,
THREAD_PRIO_4 = 1,
THREAD_PRIO_5 = 0,
THREAD_PRIO_6 = 0, /*Low*/
} THREAD_PRIO_E;
/**
* @brief thread parameters
*
*/
typedef struct {
UINT_T stackDepth; // stack size
UINT8_T priority; // thread priority
CHAR_T *thrdname; // thread name
} THREAD_CFG_T
/**
* @brief thread process function
*
*/
typedef VOID (*THREAD_FUNC_CB)(PVOID_T args);
/**
* @brief thread enter function
*
*/
typedef VOID(*THREAD_ENTER_CB)(VOID);
/**
* @brief thread exit function
*
*/
typedef VOID(*THREAD_EXIT_CB)(VOID); // thread extract
/**
* @brief create and start a Tuya SDK thread
*
* @param[in] enter: the function called before the thread process called. It can be null
* @param[in] exit: the function called after the thread process called. It can be null
* @param[in] func: the main thread process function
* @param[in] func_args: the args of the pThrdFunc. It can be null
* @param[in] cfg: the param of creating a thread
* @param[out] handle: the Tuya SDK thread context
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tal_thread_create_and_start(THREAD_HANDLE *handle,
CONST THREAD_ENTER_CB enter,
CONST THREAD_EXIT_CB exit,
CONST THREAD_FUNC_CB func,
CONST PVOID_T func_args,
CONST THREAD_CFG_T *cfg);
该函数调用后,会将线程状态设置成 THREAD_STATE_STOP
状态。等待处理函数退出后,再调用线程退出回调,并且将状态设置为 THREAD_STATE_DELETE
状态,最后释放创建的资源。
/**
* @brief stop and free a Tuya SDK thread
*
* @param[in] handle: the input thread context
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tal_thread_delete(CONST THREAD_HANDLE handle);
typedef enum {
THREAD_STATE_EMPTY = 0,
THREAD_STATE_RUNNING,
THREAD_STATE_STOP,
THREAD_STATE_DELETE,
} THREAD_STATE_E;
/**
* @brief get the thread context running status
*
* @param[in] thrdHandle: the input thread context
* @return the thread status
*/
THREAD_STATE_E tal_thread_get_state(CONST THREAD_HANDLE handle);
/**
* @brief check the function caller is in the input thread context
*
* @param[in] handle: the input thread context
* @param[in] bl: run in self space
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tal_thread_is_self(CONST THREAD_HANDLE handle, BOOL_T *bl);
该 API 用于诊断线程运行状态,例如在发生线程运行卡住情况下,需要打印线程的调用栈。
/**
* @brief diagnose the thread (dump task stack, etc.)
*
* @param[in] thrdHandle: the input thread context
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tal_thread_diagnose(CONST THREAD_HANDLE handle);
/**
* @brief diagnose the thread (dump task stack, etc.)
*
* @param[in] thrdHandle: the input thread context
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tal_thread_diagnose(CONST THREAD_HANDLE handle);
示例代码可参考框架内自带的 TuyaOS 示例集合 tuya_demo_examples
中的 system_thread
。
如果堆栈大小设置不合理,TuyaOS 框架会 10 分钟一次打印线程堆栈水线情况,可以根据该值合理调整线程堆栈大小。
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈