Freddie Chopin
2018-08-24 22:25:44 UTC
Do I need to implement them (how?)
Do you need to implement them - no, but using any of the functionswhich needs them will be allowed only from single thread (maybe with
exception of malloc/free if you provide __malloc_lock/unlock()).
I think that implementations from my RTOS will answer your "how"
question:
https://github.com/DISTORTEC/distortos/blob/master/source/newlib/locking.cpp
Why are they needed and what do they do?
They are not needed (; They implement all the locking for newlib. Themost frequent use-case is stdio - each file has its own lock and
there's also a global lock for accessing the list of free FILE
structures (when using fopen() and similar). Without these locks using
anything from stdio is basically allowed from one thread only.
In typical usage of libc - where it is really a part of the operating
system - the locks are implemented "natively", because newlib has a
"port" for that OS (this is a case with Linux, RTEMS, quite likely with
Cygwin). But in bare-metal this approach is not practical - neither
newlib wants to have "ports" for hundreds of RTOSes, nor will anyone
maintain hundreds of newlib forks with such ports. And you would need a
special toolchain for each RTOS + target combination... That's why
there are these "retargetable" locks, which you can implement as
callbacks directly in your project.
Regards,
FCh