Merge pull request #4731 from bulislaw/fix_armc_mutexes

Boot: Make ARMC library mutexes dynamic
pull/4803/head
Jimmy Brisson 2017-07-24 10:54:47 -05:00 committed by GitHub
commit e7f27aa34f
3 changed files with 48 additions and 4 deletions

View File

@ -160,6 +160,8 @@
*
*/
#include <stdlib.h>
#include "cmsis.h"
#include "mbed_rtx.h"
#include "mbed_rtos_storage.h"
@ -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 ********************/

View File

@ -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);
@ -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);
}
@ -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);
}
@ -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);
}

View File

@ -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