Skip to content

Thread safe #1765

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 3 commits into from
Jun 1, 2016
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
12 changes: 12 additions & 0 deletions hal/common/retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
107 changes: 103 additions & 4 deletions rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -553,20 +624,25 @@ __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);
}

__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"
Expand All @@ -580,6 +656,29 @@ __attribute__((naked)) void software_init_hook (void) {
);
}

// Opaque declaration of _reent structure
struct _reent;

void __malloc_lock( struct _reent *_r )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the formatting : ( struct _reent *_r ), no spacing around ( ) - (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;
Expand Down
3 changes: 2 additions & 1 deletion workspace_tools/toolchains/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
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"]:
Expand Down
6 changes: 3 additions & 3 deletions workspace_tools/toolchains/iar.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: we need to clean up IAR flags. otherwise when you export you wont get all these new flags in the project file 😭

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")
Expand Down Expand Up @@ -114,7 +114,7 @@ def archive(self, objects, lib_path):
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
Expand Down