Merge pull request #1765 from c1728p9/thread_safe

Thread safe
pull/1832/head
Martin Kojtal 2016-06-01 18:01:18 +01:00
commit 02b0267143
4 changed files with 120 additions and 8 deletions

View File

@ -565,4 +565,16 @@ char* mbed_gets(char*s, int size, FILE *_file){
#endif
}
#if defined (__ICCARM__)
// Stub out locks when an rtos is not present
extern "C" WEAK void __iar_system_Mtxinit(__iar_Rmtx *mutex) {}
extern "C" WEAK void __iar_system_Mtxdst(__iar_Rmtx *mutex) {}
extern "C" WEAK void __iar_system_Mtxlock(__iar_Rmtx *mutex) {}
extern "C" WEAK void __iar_system_Mtxunlock(__iar_Rmtx *mutex) {}
extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {}
extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {}
extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {}
extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {}
#endif
} // namespace mbed

View File

@ -54,13 +54,13 @@
#define OS_TCB_SIZE 52
#define OS_TMR_SIZE 8
#if defined (__CC_ARM) && !defined (__MICROLIB)
typedef void *OS_ID;
typedef uint32_t OS_TID;
typedef uint32_t OS_MUT[4];
typedef uint32_t OS_RESULT;
#if defined (__CC_ARM) && !defined (__MICROLIB)
#define runtask_id() rt_tsk_self()
#define mutex_init(m) rt_mut_init(m)
#define mutex_wait(m) os_mut_wait(m,0xFFFFU)
@ -190,6 +190,77 @@ uint16_t const mp_tmr_size = 0U;
extern void *__libspace_start;
#endif
#if defined (__ICCARM__)
static osMutexId std_mutex_id_sys[_MAX_LOCK] = {0};
static OS_MUT std_mutex_sys[_MAX_LOCK] = {0};
#define _FOPEN_MAX 10
static osMutexId std_mutex_id_file[_FOPEN_MAX] = {0};
static OS_MUT std_mutex_file[_FOPEN_MAX] = {0};
void __iar_system_Mtxinit(__iar_Rmtx *mutex) /* Initialize a system lock */
{
osMutexDef_t def;
uint32_t index;
for (index = 0; index < _MAX_LOCK; index++) {
if (0 == std_mutex_id_sys[index]) {
def.mutex = &std_mutex_sys[index];
std_mutex_id_sys[index] = osMutexCreate(&def);
*mutex = (__iar_Rmtx*)&std_mutex_id_sys[index];
return;
}
}
// This should never happen
error("Not enough mutexes\n");
}
void __iar_system_Mtxdst(__iar_Rmtx *mutex)/*Destroy a system lock */
{
osMutexDelete(*(osMutexId*)*mutex);
*mutex = 0;
}
void __iar_system_Mtxlock(__iar_Rmtx *mutex) /* Lock a system lock */
{
osMutexWait(*(osMutexId*)*mutex, osWaitForever);
}
void __iar_system_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a system lock */
{
osMutexRelease(*(osMutexId*)*mutex);
}
void __iar_file_Mtxinit(__iar_Rmtx *mutex)/*Initialize a file lock */
{
osMutexDef_t def;
uint32_t index;
for (index = 0; index < _FOPEN_MAX; index++) {
if (0 == std_mutex_id_file[index]) {
def.mutex = &std_mutex_file[index];
std_mutex_id_file[index] = osMutexCreate(&def);
*mutex = (__iar_Rmtx*)&std_mutex_id_file[index];
return;
}
}
// The variable _FOPEN_MAX needs to be increased
error("Not enough mutexes\n");
}
void __iar_file_Mtxdst(__iar_Rmtx *mutex) /* Destroy a file lock */
{
osMutexDelete(*(osMutexId*)*mutex);
*mutex = 0;
}
void __iar_file_Mtxlock(__iar_Rmtx *mutex) /* Lock a file lock */
{
osMutexWait(*(osMutexId*)*mutex, osWaitForever);
}
void __iar_file_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a file lock */
{
osMutexRelease(*(osMutexId*)*mutex);
}
#endif
/*----------------------------------------------------------------------------
* RTX Optimizations (empty functions)
@ -553,11 +624,18 @@ __asm void __rt_entry (void) {
#elif defined (__GNUC__)
osMutexDef(malloc_mutex);
static osMutexId malloc_mutex_id;
osMutexDef(env_mutex);
static osMutexId env_mutex_id;
extern void __libc_fini_array(void);
extern void __libc_init_array (void);
extern int main(int argc, char **argv);
void pre_main(void) {
malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex));
env_mutex_id = osMutexCreate(osMutex(env_mutex));
atexit(__libc_fini_array);
__libc_init_array();
main(0, NULL);
@ -565,8 +643,6 @@ void pre_main(void) {
__attribute__((naked)) void software_init_hook (void) {
__asm (
".syntax unified\n"
".thumb\n"
"bl osKernelInitialize\n"
#ifdef __MBED_CMSIS_RTOS_CM
"bl set_main_stack\n"
@ -580,6 +656,29 @@ __attribute__((naked)) void software_init_hook (void) {
);
}
// Opaque declaration of _reent structure
struct _reent;
void __malloc_lock( struct _reent *_r )
{
osMutexWait(malloc_mutex_id, osWaitForever);
}
void __malloc_unlock( struct _reent *_r )
{
osMutexRelease(malloc_mutex_id);
}
void __env_lock( struct _reent *_r )
{
osMutexWait(env_mutex_id, osWaitForever);
}
void __env_unlock( struct _reent *_r )
{
osMutexRelease(env_mutex_id);
}
#elif defined (__ICCARM__)
extern void* __vector_table;

View File

@ -183,7 +183,8 @@ class GCC_ARM(GCC):
GCC.__init__(self, target, options, notify, macros, silent, GCC_ARM_PATH, extra_verbose=extra_verbose)
# Use latest gcc nanolib
self.ld.append("--specs=nano.specs")
if "thread-safe" not in self.options:
self.ld.append("--specs=nano.specs")
if target.name in ["LPC1768", "LPC4088", "LPC4088_DM", "LPC4330", "UBLOX_C027", "LPC2368"]:
self.ld.extend(["-u _printf_float", "-u _scanf_float"])
elif target.name in ["RZ_A1H", "VK_RZ_A1H", "ARCH_MAX", "DISCO_F407VG", "DISCO_F429ZI", "DISCO_F469NI", "NUCLEO_F401RE", "NUCLEO_F410RB", "NUCLEO_F411RE", "NUCLEO_F446RE", "ELMO_F411RE", "MTS_MDOT_F411RE", "MTS_DRAGONFLY_F411RE", "DISCO_F746NG"]:

View File

@ -66,10 +66,10 @@ class IAR(mbedToolchain):
self.asm = [join(IAR_BIN, "iasmarm")] + ["--cpu", cpuchoice]
if not "analyze" in self.options:
self.cc = [main_cc] + c_flags
self.cppc = [main_cc, "--c++", "--no_rtti", "--no_exceptions"] + c_flags
self.cppc = [main_cc, "--c++", "--no_rtti", "--no_exceptions", "--guard_calls"] + c_flags
else:
self.cc = [join(GOANNA_PATH, "goannacc"), '--with-cc="%s"' % main_cc.replace('\\', '/'), "--dialect=iar-arm", '--output-format="%s"' % self.GOANNA_FORMAT] + c_flags
self.cppc = [join(GOANNA_PATH, "goannac++"), '--with-cxx="%s"' % main_cc.replace('\\', '/'), "--dialect=iar-arm", '--output-format="%s"' % self.GOANNA_FORMAT] + ["--c++", "--no_rtti", "--no_exceptions"] + c_flags
self.cppc = [join(GOANNA_PATH, "goannac++"), '--with-cxx="%s"' % main_cc.replace('\\', '/'), "--dialect=iar-arm", '--output-format="%s"' % self.GOANNA_FORMAT] + ["--c++", "--no_rtti", "--no_exceptions", "--guard_calls"] + c_flags
self.ld = join(IAR_BIN, "ilinkarm")
self.ar = join(IAR_BIN, "iarchive")
self.elf2bin = join(IAR_BIN, "ielftool")
@ -114,7 +114,7 @@ class IAR(mbedToolchain):
self.default_cmd([self.ar, lib_path] + objects)
def link(self, output, objects, libraries, lib_dirs, mem_map):
args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"]
args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization", "--threaded_lib"]
self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries))
@hook_tool