Skip to content

Statically allocate ARMCC required mutex objects #5687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions rtos/TARGET_CORTEX/mbed_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,6 @@ void __rt_entry (void) {
mbed_start_main();
}

typedef void *mutex;
mutex _static_mutexes[OS_MUTEX_NUM] = {NULL};

/* ARM toolchain requires dynamically created mutexes to enforce thread safety. There's
up to 8 static mutexes, protecting atexit, signalinit, stdin, stdout, stderr, stream_list,
fp_trap_init and the heap. Additionally for each call to fopen one extra mutex will be
Expand All @@ -436,6 +433,12 @@ mutex _static_mutexes[OS_MUTEX_NUM] = {NULL};
worry about freeing the allocated memory as library mutexes are only freed when the
application finishes executing.
*/

typedef void *mutex;
#define OS_MUTEX_STATIC_NUM 8
mutex _static_mutexes[OS_MUTEX_STATIC_NUM] = {NULL};
mbed_rtos_storage_mutex_t _static_mutexes_mem[OS_MUTEX_STATIC_NUM] = {NULL};

int _mutex_initialize(mutex *m)
{
osMutexAttr_t attr;
Expand All @@ -445,10 +448,13 @@ int _mutex_initialize(mutex *m)

mutex *slot = NULL;
core_util_critical_section_enter();
for (int i = 0; i < OS_MUTEX_NUM; i++) {
for (int i = 0; i < OS_MUTEX_STATIC_NUM; i++) {
if (_static_mutexes[i] == NULL) {
_static_mutexes[i] = (mutex)-1; // dummy value to reserve slot
slot = &_static_mutexes[i];
//Use the static attrs
attr.cb_size = sizeof(mbed_rtos_storage_mutex_t);
attr.cb_mem = &_static_mutexes_mem[i];
break;
}
}
Expand Down Expand Up @@ -482,7 +488,7 @@ int _mutex_initialize(mutex *m)
void _mutex_free(mutex *m) {
mutex *slot = NULL;
core_util_critical_section_enter();
for (int i = 0; i < OS_MUTEX_NUM; i++) {
for (int i = 0; i < OS_MUTEX_STATIC_NUM; i++) {
if (_static_mutexes[i] == *m) {
slot = &_static_mutexes[i];
break;
Expand Down
6 changes: 0 additions & 6 deletions rtos/TARGET_CORTEX/mbed_rtx_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@
#error "OS Tickrate must be 1000 for system timing"
#endif

#if defined (__CC_ARM) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
/* ARM toolchain uses up to 8 static mutexes, any further mutexes will be allocated on the heap. */
#define OS_MUTEX_OBJ_MEM 1
#define OS_MUTEX_NUM 8
#endif

#if !defined(OS_STACK_WATERMARK) && (defined(MBED_STACK_STATS_ENABLED) && MBED_STACK_STATS_ENABLED)
#define OS_STACK_WATERMARK 1
#endif
Expand Down