🏆 Join the TuyaOpen AI Hardware Contest! 100 days to build the future of AI devicesLearn More →
Skip to main content

tkl_mutex | Mutex Lock

The file tkl_mutex.c is used for creating and managing mutexes in embedded systems or multitasking operating systems to ensure synchronized access to resources. The functions defined in the file can create mutexes, lock them, attempt to lock them, unlock them, and destroy them. This file is automatically generated by the TuyaOS and allows developers to add or modify code implementations in specified areas.

API Description​

tkl_mutex_create_init​

OPERATE_RET tkl_mutex_create_init(TKL_MUTEX_HANDLE *handle);

Function​

Creates and initializes a mutex.

Parameters​

  • handle: Output parameter used to store the handle of the created mutex.

Return Value​

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

tkl_mutex_lock​

OPERATE_RET tkl_mutex_lock(const TKL_MUTEX_HANDLE handle);

Function​

Locks a mutex. If the mutex is already locked by another task, the task calling this function will be suspended until the mutex becomes available.

Parameters​

  • handle: Input parameter, the handle of the mutex.

Return Value​

A return value of OPRT_OK indicates that the mutex was successfully locked, while failure returns the corresponding error code. Refer to tuya_error_code.h for detailed information.

tkl_mutex_trylock​

OPERATE_RET tkl_mutex_trylock(const TKL_MUTEX_HANDLE mutexHandle);

Function​

Attempts to lock a mutex without suspending the calling task if the mutex is occupied.

Parameters​

  • mutexHandle: Input parameter, the handle of the mutex.

Return Value​

A return value of OPRT_OK indicates that the mutex was successfully attempted to be locked, while error values indicate an error. Refer to tuya_error_code.h for specific error codes.

tkl_mutex_unlock​

OPERATE_RET tkl_mutex_unlock(const TKL_MUTEX_HANDLE handle);

Function​

Unlocks a locked mutex.

Parameters​

  • handle: Input parameter, the handle of the mutex.

Return Value​

A return value of OPRT_OK indicates that the mutex was successfully unlocked, while other values indicate an error. Refer to tuya_error_code.h for detailed information.

tkl_mutex_release​

OPERATE_RET tkl_mutex_release(const TKL_MUTEX_HANDLE handle);

Function​

Releases and deletes a mutex.

Parameters​

  • handle: Input parameter, the handle of the mutex.

Return Value​

A return value of OPRT_OK indicates that the resources were successfully released, while other values indicate an error. Refer to tuya_error_code.h for error information.