Commit a57d83eb authored by Oliver Horst's avatar Oliver Horst
Browse files

[chg] Re-organized and cleaned-up code structure/files

parent 37d0aa18
......@@ -4,7 +4,5 @@ target_sources(
freertos
#
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/_fini.c"
"${CMAKE_CURRENT_LIST_DIR}/_init.c"
"${CMAKE_CURRENT_LIST_DIR}/syscall_table.S"
)
int _fini(void)
{
return 0;
}
#include <sys/lock.h>
extern void init_locks();
int _init(void)
{
return 0;
}
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/core/semphr.h"
#include "freertos/core/task.h"
#include <sys/lock.h>
void __retarget_lock_init(_LOCK_T *lock);
void __retarget_lock_init_recursive(_LOCK_T *lock);
void __retarget_lock_close(_LOCK_T lock);
void __retarget_lock_close_recursive(_LOCK_T lock);
void __retarget_lock_acquire(_LOCK_T lock);
void __retarget_lock_acquire_recursive(_LOCK_T lock);
int __retarget_lock_try_acquire(_LOCK_T lock);
int __retarget_lock_try_acquire_recursive(_LOCK_T lock);
void __retarget_lock_release(_LOCK_T lock);
void __retarget_lock_release_recursive(_LOCK_T lock);
struct __lock {
QueueHandle_t handle;
};
struct __lock __lock___sinit_recursive_mutex;
struct __lock __lock___sfp_recursive_mutex;
struct __lock __lock___atexit_recursive_mutex;
struct __lock __lock___at_quick_exit_mutex;
struct __lock __lock___malloc_recursive_mutex;
struct __lock __lock___env_recursive_mutex;
struct __lock __lock___tz_mutex;
struct __lock __lock___dd_hash_mutex;
struct __lock __lock___arc4random_mutex;
static void lazyInitLock(struct __lock *lock, size_t recursive)
{
QueueHandle_t excl_handle = __atomic_load_n( &(lock->handle), __ATOMIC_SEQ_CST );
if ( !excl_handle )
{
QueueHandle_t new_handle;
if ( recursive )
{
new_handle = xSemaphoreCreateRecursiveMutex();
}
else
{
new_handle = xSemaphoreCreateMutex();
}
if ( !__atomic_compare_exchange_n(
&(lock->handle),
&excl_handle,
new_handle,
0,
__ATOMIC_SEQ_CST,
__ATOMIC_SEQ_CST
)
)
{
vSemaphoreDelete ( new_handle );
}
}
}
static int prvAcquireLock(struct __lock *lock, TickType_t xBlockTime)
{
int uReturn = 0;
if ( !lock )
return uReturn;
lazyInitLock( lock, 0 );
while ( ! (uReturn = xSemaphoreTake ( lock->handle, xBlockTime) ) )
{
taskYIELD ();
}
return uReturn;
}
static int prvAcquireLockRecursive(struct __lock *lock, TickType_t xBlockTime)
{
int uReturn = 0;
if ( !lock )
return uReturn;
lazyInitLock( lock, 1 );
while ( ! (uReturn = xSemaphoreTakeRecursive ( lock->handle, xBlockTime ) ) )
{
taskYIELD ();
}
return uReturn;
}
void __retarget_lock_init(_LOCK_T *lock)
{
if ( lock )
{
(*lock)->handle = xSemaphoreCreateMutex();
}
}
void __retarget_lock_init_recursive(_LOCK_T *lock)
{
if ( lock )
{
(*lock)->handle = xSemaphoreCreateRecursiveMutex();
}
}
void __retarget_lock_close(_LOCK_T lock)
{
if ( !lock )
return;
QueueHandle_t excl_handle = __atomic_load_n( &(lock->handle), __ATOMIC_SEQ_CST );
while (excl_handle
&& !__atomic_compare_exchange_n(
&(lock->handle),
&excl_handle,
0,
0,
__ATOMIC_SEQ_CST,
__ATOMIC_SEQ_CST
));
if ( excl_handle )
{
vSemaphoreDelete ( excl_handle );
}
}
void __retarget_lock_close_recursive(_LOCK_T lock)
{
__retarget_lock_close( lock );
}
void __retarget_lock_acquire(_LOCK_T lock)
{
prvAcquireLock( lock, portMAX_DELAY );
}
void __retarget_lock_acquire_recursive(_LOCK_T lock)
{
prvAcquireLockRecursive( lock, portMAX_DELAY );
}
int __retarget_lock_try_acquire(_LOCK_T lock)
{
int uReturn;
uReturn = prvAcquireLock( lock, 0 );
return uReturn;
}
int __retarget_lock_try_acquire_recursive(_LOCK_T lock)
{
int uReturn;
uReturn = prvAcquireLockRecursive( lock, 0 );
return uReturn;
}
void __retarget_lock_release(_LOCK_T lock)
{
if ( !lock )
return;
QueueHandle_t excl_handle = __atomic_load_n( &(lock->handle), __ATOMIC_SEQ_CST );
if ( excl_handle )
{
xSemaphoreGive( excl_handle );
}
}
void __retarget_lock_release_recursive(_LOCK_RECURSIVE_T lock)
{
if ( !lock )
return;
QueueHandle_t excl_handle = __atomic_load_n( &(lock->handle), __ATOMIC_SEQ_CST );
if ( excl_handle )
{
xQueueGiveMutexRecursive( excl_handle );
}
}
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment