Skip to content

Boot: Make ARMC library mutexes dynamic #4731

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
Jul 24, 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
43 changes: 43 additions & 0 deletions rtos/mbed_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
*
*/

#include <stdlib.h>

#include "cmsis.h"
#include "mbed_rtx.h"
#include "mbed_rtos_storage.h"
Expand Down Expand Up @@ -413,6 +415,47 @@ void __rt_entry (void) {
mbed_start_main();
}

typedef void *mutex;

/* 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
created.
mbed OS provides a RTX pool for 8 mutexes, to satisfy the static requirements. All
additional mutexes will be allocated on the heap. We can't use the heap allocation for
all the required mutexes, as the heap operations also require a mutex. We don't need to
worry about freeing the allocated memory as library mutexes are only freed when the
application finishes executing.
*/
int _mutex_initialize(mutex *m)
{
osMutexAttr_t attr;
memset(&attr, 0, sizeof(attr));
attr.name = "ARM toolchain mutex";
attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust;

*m = osMutexNew(&attr);
if (*m != NULL) {
return 1;
}

/* Mutex pool exhausted, try using HEAP */
attr.cb_size = sizeof(mbed_rtos_storage_mutex_t);
attr.cb_mem = (void*)malloc(attr.cb_size);
if (attr.cb_mem == NULL) {
osRtxErrorNotify(osRtxErrorClibSpace, m);
return 0;
}

*m = osMutexNew(&attr);
if (*m == NULL) {
osRtxErrorNotify(osRtxErrorClibMutex, m);
return 0;
}

return 1;
}

#endif /* ARMC */
#elif defined (__GNUC__) /******************** GCC ********************/

Expand Down
8 changes: 4 additions & 4 deletions rtos/rtx5/TARGET_CORTEX_M/rtx_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ typedef void *mutex;
// Initialize mutex
__USED
int _mutex_initialize(mutex *m);
int _mutex_initialize(mutex *m) {
__WEAK int _mutex_initialize(mutex *m) {
*m = osMutexNew(NULL);
if (*m == NULL) {
osRtxErrorNotify(osRtxErrorClibMutex, m);
Expand All @@ -609,7 +609,7 @@ int _mutex_initialize(mutex *m) {
// Acquire mutex
__USED
void _mutex_acquire(mutex *m);
void _mutex_acquire(mutex *m) {
__WEAK void _mutex_acquire(mutex *m) {
if (os_kernel_is_active()) {
osMutexAcquire(*m, osWaitForever);
}
Expand All @@ -618,7 +618,7 @@ void _mutex_acquire(mutex *m) {
// Release mutex
__USED
void _mutex_release(mutex *m);
void _mutex_release(mutex *m) {
__WEAK void _mutex_release(mutex *m) {
if (os_kernel_is_active()) {
osMutexRelease(*m);
}
Expand All @@ -627,7 +627,7 @@ void _mutex_release(mutex *m) {
// Free mutex
__USED
void _mutex_free(mutex *m);
void _mutex_free(mutex *m) {
__WEAK void _mutex_free(mutex *m) {
osMutexDelete(*m);
}

Expand Down
1 change: 1 addition & 0 deletions rtos/rtx5/mbed_rtx_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#define OS_DYNAMIC_MEM_SIZE 0

#if defined(__CC_ARM)
/* 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
Expand Down