Page 1 of 1

Does ESP_LOG/stdout has mutex inside?

Posted: Wed Jul 13, 2022 3:08 am
by jhnlmn
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

Re: Does ESP_LOG/stdout has mutex inside?

Posted: Thu Jul 14, 2022 7:14 am
by ESP_igrr
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.

Re: Does ESP_LOG/stdout has mutex inside?

Posted: Thu Jul 14, 2022 10:27 pm
by jhnlmn
Thank you for detailed explanation.
So, in short, write(STDOUT_FILENO, ..) is atomic and cannot be interrupted by ESP_LOGx or printf, right?