Skip to main content
Graduate II
February 18, 2024
Solved

HAL and concurrency.

  • February 18, 2024
  • 1 reply
  • 993 views

I am wondering if HAL functions have some builtin protection mechanism or if I have to wrap them around a  semaphore/mutex take/give. 

For example, if two tasks call `HAL_ADC_Start(&hadc1);` or `HAL_UART_Receive_IT`, what will happen? Shall I use a semaphore/mutex at OS level? 
 

    This topic has been closed for replies.
    Best answer by TDK

    In the vast majority of cases, HAL functions on independent peripherals can be called in different threads with no issues in terms of code errors or race conditions or other bad behavior. This is true for your example of HAL_ADC_Start and HAL_UART_Receive_IT.

    In general, only one thread should access a given peripheral. You shouldn't call HAL_UART_Receive_IT in two different threads. The lock mechanism in HAL is not thread-safe, although it does provide some minimal and superficial protection.

    1 reply

    TDKAnswer
    Super User
    February 18, 2024

    In the vast majority of cases, HAL functions on independent peripherals can be called in different threads with no issues in terms of code errors or race conditions or other bad behavior. This is true for your example of HAL_ADC_Start and HAL_UART_Receive_IT.

    In general, only one thread should access a given peripheral. You shouldn't call HAL_UART_Receive_IT in two different threads. The lock mechanism in HAL is not thread-safe, although it does provide some minimal and superficial protection.