Skip to content

Fixed mutex assert in armcc fopen and related memory leak #5526

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
Nov 27, 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
44 changes: 41 additions & 3 deletions rtos/TARGET_CORTEX/mbed_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
#include "cmsis_os2.h"
#include "mbed_toolchain.h"
#include "mbed_error.h"
#include "mbed_critical.h"
#if defined(__IAR_SYSTEMS_ICC__ ) && (__VER__ >= 8000000)
#include <DLib_Threads.h>
#endif
Expand Down Expand Up @@ -423,6 +424,7 @@ void __rt_entry (void) {
}

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,
Expand All @@ -441,9 +443,23 @@ int _mutex_initialize(mutex *m)
attr.name = "ARM toolchain mutex";
attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust;

*m = osMutexNew(&attr);
if (*m != NULL) {
return 1;
mutex *slot = NULL;
core_util_critical_section_enter();
for (int i = 0; i < OS_MUTEX_NUM; i++) {
if (_static_mutexes[i] == NULL) {
_static_mutexes[i] = (mutex)-1; // dummy value to reserve slot
slot = &_static_mutexes[i];
break;
}
}
core_util_critical_section_exit();

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

/* Mutex pool exhausted, try using HEAP */
Expand All @@ -463,6 +479,28 @@ int _mutex_initialize(mutex *m)
return 1;
}

void _mutex_free(mutex *m) {
mutex *slot = NULL;
core_util_critical_section_enter();
for (int i = 0; i < OS_MUTEX_NUM; i++) {
if (_static_mutexes[i] == *m) {
slot = &_static_mutexes[i];
break;
}
}
core_util_critical_section_exit();

osMutexDelete(*m);

// if no slot reserved for mutex, must have been dynamically allocated
if (!slot) {
free(m);
} else {
*slot = NULL;
}

}

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

Expand Down
12 changes: 9 additions & 3 deletions rtos/TARGET_CORTEX/rtx5/RTX/Source/rtx_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,27 +609,33 @@ __WEAK int _mutex_initialize(mutex *m) {
}

// Acquire mutex
#if !defined(__ARMCC_VERSION) || __ARMCC_VERSION < 6010050
__USED
#endif
void _mutex_acquire(mutex *m);
void _mutex_acquire(mutex *m) {
__WEAK void _mutex_acquire(mutex *m) {
if (os_kernel_is_active()) {
osMutexAcquire(*m, osWaitForever);
}
}

// Release mutex
#if !defined(__ARMCC_VERSION) || __ARMCC_VERSION < 6010050
__USED
#endif
void _mutex_release(mutex *m);
void _mutex_release(mutex *m) {
__WEAK void _mutex_release(mutex *m) {
if (os_kernel_is_active()) {
osMutexRelease(*m);
}
}

// Free mutex
#if !defined(__ARMCC_VERSION) || __ARMCC_VERSION < 6010050
__USED
#endif
void _mutex_free(mutex *m);
void _mutex_free(mutex *m) {
__WEAK void _mutex_free(mutex *m) {
osMutexDelete(*m);
}

Expand Down