-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add __io_lock() and __io_unlock() functions to lock io functions #67
base: master
Are you sure you want to change the base?
Conversation
… as printf in order to avoid collision between multiple threads/cores.
@sivasivarajah : the PR should make locking optional (using #define) And in my experience, you will run in problems with simple locks.
|
Codecov Report
@@ Coverage Diff @@
## master #67 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 1 1
Lines 359 359
=====================================
Hits 359 359
Continue to review full report at Codecov.
|
@ledvinap Yes you are right. Those locks should be optional, I added flags to enable or not locks PRINTF_ENABLE_LOCK. About the issues you raised :
In my implementation for FreeRTOS for our chip(RISC-V GAP8), I have a lock function to handle cores(HW) and threads(SW), since we have 9 cores that can print in parallel, and then the Doing this guarantees :
|
Thanks a lot for the PR. But actually I don't like it so much. I'm using printf with a lot of FreeRTOS projects, too. IMHO it's better, if you create an inline function like |
@@ -43,12 +43,16 @@ extern "C" { | |||
/** | |||
* Lock the printf to avoid collision when multiple threads/cores try to output strings/characters. | |||
* This function can implement a mutex, semaphore or whatever lock. | |||
* | |||
* \note PRINTF_ENABLE_LOCK should be defined in order to activate lock function. | |||
*/ | |||
void __io_lock(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be wrapped in PRINTF_IO_LOCK preprocessor checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually was using the format introduced by Marco to enable features.
Here PRINTF_ENABLE_LOCK
is a macro to add in Makefile, and if it is defined, then PRINTF_IO_LOCK
will be enabled and lock and unlock functions activated.
Hi Marco, my apologies for this late reply.
That is true, a threadsafe driver should be enough.
Yes I agree for debugging, wrapping the lock in a debug function will be simple and easy. But I want to give user the possibility to print from application side.
Instead of __io_lock()? If so why not. |
Great implementation of printf. It is useful for our project in FreeRTOS.
We are currently using it, and would like to improve it adding lock and unlock functions at high level in functions like printf, sprintf to avoid collisions.
That is because multiple threads could call printf without having locks above printf.
The solution might be to use to have locks in the function _putchar, but it is very costly in terms of performance and time.
This kind of lock is better since it allows to send a full string before another thread/core messes up with it.
And of course the locks can be defined by users. Depending on architectures and/or OS, they can be mutexex, semaphores or simple locks using TAS/CAS,...