Skip to content

Instantly share code, notes, and snippets.

@thomask77
Last active March 27, 2024 12:34
Show Gist options
  • Save thomask77/65591d78070ace68885d3fd05cdebe3a to your computer and use it in GitHub Desktop.
Save thomask77/65591d78070ace68885d3fd05cdebe3a to your computer and use it in GitHub Desktop.
newlib retarget locks for FreeRTOS (static version)
// -------------------- Retarget Locks --------------------
//
// share/doc/gcc-arm-none-eabi/pdf/libc.pdf:
//
// Newlib was configured to allow the target platform to provide the locking routines and
// static locks at link time. As such, a dummy default implementation of these routines and
// static locks is provided for single-threaded application to link successfully out of the box on
// bare-metal systems.
//
// For multi-threaded applications the target platform is required to provide an implementa-
// tion for *all* these routines and static locks. If some routines or static locks are missing, the
// link will fail with doubly defined symbols.
//
// (see also newlib/libc/misc/lock.c)
//
#include <sys/lock.h>
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
StaticSemaphore_t __lock___sinit_recursive_mutex;
StaticSemaphore_t __lock___sfp_recursive_mutex;
StaticSemaphore_t __lock___atexit_recursive_mutex;
StaticSemaphore_t __lock___at_quick_exit_mutex;
StaticSemaphore_t __lock___malloc_recursive_mutex;
StaticSemaphore_t __lock___env_recursive_mutex;
StaticSemaphore_t __lock___tz_mutex;
StaticSemaphore_t __lock___dd_hash_mutex;
StaticSemaphore_t __lock___arc4random_mutex;
__attribute__((constructor))
static void init_retarget_locks(void)
{
xSemaphoreCreateRecursiveMutexStatic(&__lock___sinit_recursive_mutex );
xSemaphoreCreateRecursiveMutexStatic(&__lock___sfp_recursive_mutex );
xSemaphoreCreateRecursiveMutexStatic(&__lock___atexit_recursive_mutex);
xSemaphoreCreateMutexStatic (&__lock___at_quick_exit_mutex );
// xSemaphoreCreateRecursiveMutexStatic(&__lock___malloc_recursive_mutex); // see below
xSemaphoreCreateRecursiveMutexStatic(&__lock___env_recursive_mutex );
xSemaphoreCreateMutexStatic (&__lock___tz_mutex );
xSemaphoreCreateMutexStatic (&__lock___dd_hash_mutex );
xSemaphoreCreateMutexStatic (&__lock___arc4random_mutex );
}
// Special case for malloc/free. Without this, the full
// malloc_recursive_mutex would be used, which is much slower.
//
void __malloc_lock(struct _reent *r)
{
(void)r;
taskENTER_CRITICAL();
}
void __malloc_unlock(struct _reent *r)
{
(void)r;
taskEXIT_CRITICAL();
}
void __retarget_lock_init(_LOCK_T *lock_ptr)
{
*lock_ptr = xSemaphoreCreateMutex();
}
void __retarget_lock_init_recursive(_LOCK_T *lock_ptr)
{
*lock_ptr = xSemaphoreCreateRecursiveMutex();
}
void __retarget_lock_close(_LOCK_T lock)
{
vSemaphoreDelete(lock);
}
void __retarget_lock_close_recursive(_LOCK_T lock)
{
vSemaphoreDelete(lock);
}
void __retarget_lock_acquire(_LOCK_T lock)
{
xSemaphoreTake(lock, portMAX_DELAY);
}
void __retarget_lock_acquire_recursive(_LOCK_T lock)
{
xSemaphoreTakeRecursive(lock, portMAX_DELAY);
}
int __retarget_lock_try_acquire(_LOCK_T lock)
{
return xSemaphoreTake(lock, 0);
}
int __retarget_lock_try_acquire_recursive(_LOCK_T lock)
{
return xSemaphoreTakeRecursive(lock, 0);
}
void __retarget_lock_release(_LOCK_T lock)
{
xSemaphoreGive(lock);
}
void __retarget_lock_release_recursive(_LOCK_T lock)
{
xSemaphoreGiveRecursive(lock);
}
@william-ml-leslie
Copy link

does this live before or after libc on the linker command line?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment