questions about mutex and critical sections

OBDave
Posts: 14
Joined: Wed May 08, 2019 10:34 pm

questions about mutex and critical sections

Postby OBDave » Tue Dec 24, 2019 9:39 pm

I am trying to understand how mutexes are used (and why) in the context of esp32 critical sections. Googling hasn't led me to any clear answer. The examples I've found all seem to either allocate the mutex as a stack variable, or malloc it. Either way, its scope ends up being limited. For example:

Code: Select all

blah();
blah();
{
	portMUX_TYPE mutex = portMUX_INITIALIZER_UNLOCKED;

	portENTER_CRITICAL(&mutex);
	access_to_some_common_resource();
	portEXIT_CRITICAL(&mutex);
}
blah();
I don't understand how this does anything useful. The mutex object is a stack variable whose lifetime does not extend beyond the scope of this code. How can this possibly protect the shared common resource from other sections of code, without also sharing the semaphor / mutex object?

It seems to me that you would want to make the mutex object global, so that everyone who shares the common resource has to attempt to grab the same mutex object first. So that would imply making the thing global, and putting the definition of it in a shared .h file.

But I don't see any evidence anyone's doing it that way.

So what is it that I'm not understanding?

Thanks in advance!

User avatar
shabtronic
Posts: 49
Joined: Sun Nov 03, 2019 1:33 pm

Re: questions about mutex and critical sections

Postby shabtronic » Tue Dec 24, 2019 10:27 pm

This is the classic reference book for learning preemptive concurrency

http://greenteapress.com/semaphores/Lit ... phores.pdf

along with "Lock Free Queues".


Have fun!

OBDave
Posts: 14
Joined: Wed May 08, 2019 10:34 pm

Re: questions about mutex and critical sections

Postby OBDave » Tue Dec 24, 2019 10:55 pm

Thanks. I'm aware of this paper.

The esp32 is a slightly different animal. Forcing synchronization between the two processors requires a little help from the rtos. Fortunately, that already exists, in the form of the mutex mechanism that was ported from freeRTOS, with modification to make it work properly in the esp32's unique mutiprocessor environment. I'm just trying to figure out how to use it correctly.

ESP_Sprite
Posts: 9769
Joined: Thu Nov 26, 2015 4:08 am

Re: questions about mutex and critical sections

Postby ESP_Sprite » Wed Dec 25, 2019 1:06 am

Allocating a mutex as a stack variable indeed makes no sense. Even for recurrency, each task will have a separate mutex, making the entire thing pointless. Allocating one using malloc() is fine, as long as it's only done once and all enter/exit-critical functions use that mux. Where did you spot those examples, by the way?

szguyer
Posts: 19
Joined: Thu Jan 11, 2018 5:41 pm

Re: questions about mutex and critical sections

Postby szguyer » Wed Dec 25, 2019 4:04 am

If it was declared static it would work (although that is not the case in the example).

Who is online

Users browsing this forum: Bing [Bot], ESP_Sprite, tomasmcguinness and 74 guests