What are the __retarget_lock functions?

Freddie Chopin freddie_chopin@op.pl
Mon Aug 27 06:39:00 GMT 2018


On Fri, 2018-08-24 at 22:42 +0200, Thomas Kindler wrote:
> Do I need to implement them (how?)

Do you need to implement them - no, but using any of the functions
which 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. The
most 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



More information about the Newlib mailing list