Thread Management

Last Updated on : 2024-01-17 05:45:11download

This topic describes the unified thread management APIs that can be used across platforms, including RTOS and Linux.

Features

  • Create thread
  • Delete thread
  • Get thread state
  • Checks if a specified thread is the currently running thread
  • Diagnose thread exception

Reference the header

tal_thread.h

API description

Create thread

/**
 * @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);

Delete thread

After this function is called, the thread state will be set to THREAD_STATE_STOP. After the handler exits, call the thread exit callback and set the state to THREAD_STATE_DELETE. Then, release the created resources.

/**
 * @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);

Get thread state

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

Check if a thread is the current thread

/**
 * @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);

Diagnose thread running

This API is used to diagnose the running status of threads. For example, print the call stack when a thread is stuck during execution.

/**
 * @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);

Print thread stack usage

/**
 * @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);

Example

system_thread in TuyaOS example collection tuya_demo_examples contains the complete example code.

FAQs

How can I set an appropriate stack size?

If the stack size is inappropriate, the TuyaOS framework will print the water mark of the thread’s stack usage every 10 minutes. You can adjust the stack size accordingly.