cancel
Showing results for 
Search instead for 
Did you mean: 

HAL and concurrency.

ubaldot
Senior

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? 
 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".