Skip to main content

tkl_semaphore | Semaphore

The file tkl_semaphore.c is used for creating and managing semaphores, facilitating task synchronization or event notification in embedded systems or multi-tasking operating systems. This file provides interfaces for creating semaphores, waiting on semaphores, posting semaphores, and releasing semaphores. This file is also automatically generated by the TuyaOS and includes specified areas for you to implement code.

API description

tkl_semaphore_create_init

OPERATE_RET tkl_semaphore_create_init(TKL_SEM_HANDLE *handle, uint32_t sem_cnt, uint32_t sem_max);
  • Feature: Creates and initializes a counting semaphore.

  • Parameters:

    • handle: The output parameter used to receive the created semaphore handle.
    • sem_cnt: The initial count of the semaphore.
    • sem_max: The maximum count of semaphores.
  • Return value: OPRT_OK indicates that a semaphore was successfully created. Any other value indicates an error. For detailed error codes, refer to tuya_error_code.h.

tkl_semaphore_wait

OPERATE_RET tkl_semaphore_wait(const TKL_SEM_HANDLE handle, uint32_t timeout);
  • Feature: Waits on a semaphore.

  • Parameters:

    • handle: The handle to a semaphore.
    • timeout: The wait timeout duration in milliseconds. TKL_SEM_WAIT_FOREVER indicates waiting indefinitely until the semaphore is acquired.
  • Return value: OPRT_OK indicates the semaphore was successfully obtained. OPRT_OS_ADAPTER_SEM_WAIT_TIMEOUT indicates a timeout occurred. Any other return value indicates an error. For detailed error codes, refer to tuya_error_code.h.

tkl_semaphore_post

OPERATE_RET tkl_semaphore_post(const TKL_SEM_HANDLE handle);
  • Feature: Posts (signals) a semaphore, incrementing its count.

  • Parameters: handle: The handle to a semaphore.

  • Return value: OPRT_OK indicates that a semaphore was successfully posted. Any other value indicates an error. For detailed error codes, refer to tuya_error_code.h.

tkl_semaphore_release

OPERATE_RET tkl_semaphore_release(const TKL_SEM_HANDLE handle);
  • Feature: Releases and deletes a semaphore.

  • Parameters: handle: The handle to a semaphore.

  • Return value: OPRT_OK indicates that the resources were successfully released. Any other value indicates an error. For detailed error codes, refer to tuya_error_code.h.