线程管理

更新时间:2024-01-16 08:08:41下载pdf

TuyaOS 框架封装了统一的线程管理接口,您可跨平台(RTOS & Linux 环境)调用。

功能描述

  • 创建线程
  • 删除线程
  • 获取线程状态
  • 判断线程是否是当前运行线程
  • 诊断线程异常的原因

关联头文件

tal_thread.h

API 说明

创建线程

/**
 * @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 分钟一次打印线程堆栈水线情况,可以根据该值合理调整线程堆栈大小。