Use weak functions in the RTEMS core?

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Apr 8 08:41:21 UTC 2020


On 08/04/2020 10:16, Sebastian Huber wrote:

> Hello,
>
> weak functions are already used in several places in RTEMS, but not in 
> RTEMS core components. In <rtems/score/basedefs.h> we have currently 
> only a RTEMS_WEAK_ALIAS() macro:
>
> testsuites/sptests/spmisc01/init.c:int weak_alias_func(void) 
> RTEMS_WEAK_ALIAS(noinline_func);
> cpukit/libfs/src/defaults/default_mmap.c:) RTEMS_WEAK_ALIAS( 
> rtems_filesystem_default_mmap );
> cpukit/libfs/src/defaults/default_poll.c:) RTEMS_WEAK_ALIAS( 
> rtems_filesystem_default_poll );
> cpukit/libfs/src/defaults/default_kqfilter.c:) RTEMS_WEAK_ALIAS( 
> rtems_filesystem_default_kqfilter );
> cpukit/include/rtems/score/basedefs.h:  #define RTEMS_WEAK_ALIAS( 
> _target ) __attribute__((__weak__, __alias__(#_target)))
> cpukit/include/rtems/score/basedefs.h:  #define RTEMS_WEAK_ALIAS( 
> _target )
>
> It was introduced to allow libbsd to override implementations.
>
> I would like to use weak functions in two places. Firstly, the thread 
> creation which has a dependency on thread deletion. Secondly, the C 
> Program Heap allocation which has a dependency on deallocation (free). 
> These dependencies could be satisfied by weak default implementations 
> which do nothing. In case the application actually deletes threads or 
> deallocates memory, then the dependencies can be satisfy by a strong 
> implementation which actually performs the necessary work.
>
> An alternative to weak functions would be to use a global function 
> pointer which is initialized to the default implementation (or NULL). 
> In case the application actually deletes threads or deallocates 
> memory, then a system initialization handler could set the function 
> pointer to the proper implementations.
>
> What do you think about adding this to basedefs.h?
>
>
> /**
>  * @brief Instructs the compiler to define a weak function.
>  *
>  * Use this attribute for function definitions.  Do not use it for 
> function
>  * declarations.
>  */
> #if defined(__GNUC__)
>   #define RTEMS_WEAK __attribute__((__weak__))
> #else
>   #define RTEMS_WEAK
> #endif

The benefit of using weak functions is that

* the linker can garbage collect the weak functions if a strong 
implementation is provided and

* there is no global function pointer which must be changed and which 
can be overwritten.



More information about the devel mailing list