Does ESP_LOG/stdout has mutex inside?

jhnlmn
Posts: 15
Joined: Wed Mar 03, 2021 4:22 am

Does ESP_LOG/stdout has mutex inside?

Postby jhnlmn » Wed Jul 13, 2022 3:08 am

It appears that ESP_LOGx routines eventually call "write(STDOUT_FILENO, ..)", which is located in ROM.
Does implementation of write in ROM uses a mutex to avoid corruption?
Can I assume that buffer passed to write(STDOUT_FILENO, ...) will be written atomically without being interrupted by ESP_LOGs from other tasks?
My tests indicate that it does, but it will be nice to know for sure.
If there is a mutex, is there a way to acquire it by some other API?

Thank you

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Does ESP_LOG/stdout has mutex inside?

Postby ESP_igrr » Thu Jul 14, 2022 7:14 am

Hi jhnlmn,

The locking happens at two levels:

- in stdio (part of the C standard library), all streams (FILE* structures) contain a lock. It is held whenever an stdio function is doing anything with the stream.

- at the VFS driver layer, there is usually some kind of a lock for each file descriptor or a global lock for the entire driver. In case of UART VFS driver (used for console output by default) there is one lock per UART port. (I.e. each of /dev/uart/0,1,2 have their own locks)

It is possible to acquire the stream lock from outside, if memory serves well the function name Is flockfile.

There is no way to acquire the 2nd lock from outside, I'm afraid...


One more note about 'open' implementation. This function is an alias of esp_vfs_open, if the vfs component is included into the build. There are indeed some ROM hooks, but the purpose is not to "hide" open in ROM but to let newlib functions from ROM access the VFS interface.

jhnlmn
Posts: 15
Joined: Wed Mar 03, 2021 4:22 am

Re: Does ESP_LOG/stdout has mutex inside?

Postby jhnlmn » Thu Jul 14, 2022 10:27 pm

Thank you for detailed explanation.
So, in short, write(STDOUT_FILENO, ..) is atomic and cannot be interrupted by ESP_LOGx or printf, right?

Who is online

Users browsing this forum: Baidu [Spider] and 147 guests