Skip to content

Commit 02b0267

Browse files
committed
Merge pull request #1765 from c1728p9/thread_safe
Thread safe
2 parents 2db84da + 5b23d9b commit 02b0267

File tree

4 files changed

+120
-8
lines changed

4 files changed

+120
-8
lines changed

hal/common/retarget.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,16 @@ char* mbed_gets(char*s, int size, FILE *_file){
565565
#endif
566566
}
567567

568+
#if defined (__ICCARM__)
569+
// Stub out locks when an rtos is not present
570+
extern "C" WEAK void __iar_system_Mtxinit(__iar_Rmtx *mutex) {}
571+
extern "C" WEAK void __iar_system_Mtxdst(__iar_Rmtx *mutex) {}
572+
extern "C" WEAK void __iar_system_Mtxlock(__iar_Rmtx *mutex) {}
573+
extern "C" WEAK void __iar_system_Mtxunlock(__iar_Rmtx *mutex) {}
574+
extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {}
575+
extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {}
576+
extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {}
577+
extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {}
578+
#endif
579+
568580
} // namespace mbed

rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
#define OS_TCB_SIZE 52
5555
#define OS_TMR_SIZE 8
5656

57-
#if defined (__CC_ARM) && !defined (__MICROLIB)
58-
5957
typedef void *OS_ID;
6058
typedef uint32_t OS_TID;
6159
typedef uint32_t OS_MUT[4];
6260
typedef uint32_t OS_RESULT;
6361

62+
#if defined (__CC_ARM) && !defined (__MICROLIB)
63+
6464
#define runtask_id() rt_tsk_self()
6565
#define mutex_init(m) rt_mut_init(m)
6666
#define mutex_wait(m) os_mut_wait(m,0xFFFFU)
@@ -190,6 +190,77 @@ uint16_t const mp_tmr_size = 0U;
190190
extern void *__libspace_start;
191191
#endif
192192

193+
#if defined (__ICCARM__)
194+
static osMutexId std_mutex_id_sys[_MAX_LOCK] = {0};
195+
static OS_MUT std_mutex_sys[_MAX_LOCK] = {0};
196+
#define _FOPEN_MAX 10
197+
static osMutexId std_mutex_id_file[_FOPEN_MAX] = {0};
198+
static OS_MUT std_mutex_file[_FOPEN_MAX] = {0};
199+
void __iar_system_Mtxinit(__iar_Rmtx *mutex) /* Initialize a system lock */
200+
{
201+
osMutexDef_t def;
202+
uint32_t index;
203+
for (index = 0; index < _MAX_LOCK; index++) {
204+
if (0 == std_mutex_id_sys[index]) {
205+
def.mutex = &std_mutex_sys[index];
206+
std_mutex_id_sys[index] = osMutexCreate(&def);
207+
*mutex = (__iar_Rmtx*)&std_mutex_id_sys[index];
208+
return;
209+
}
210+
}
211+
// This should never happen
212+
error("Not enough mutexes\n");
213+
}
214+
215+
void __iar_system_Mtxdst(__iar_Rmtx *mutex)/*Destroy a system lock */
216+
{
217+
osMutexDelete(*(osMutexId*)*mutex);
218+
*mutex = 0;
219+
}
220+
221+
void __iar_system_Mtxlock(__iar_Rmtx *mutex) /* Lock a system lock */
222+
{
223+
osMutexWait(*(osMutexId*)*mutex, osWaitForever);
224+
}
225+
226+
void __iar_system_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a system lock */
227+
{
228+
osMutexRelease(*(osMutexId*)*mutex);
229+
}
230+
231+
void __iar_file_Mtxinit(__iar_Rmtx *mutex)/*Initialize a file lock */
232+
{
233+
osMutexDef_t def;
234+
uint32_t index;
235+
for (index = 0; index < _FOPEN_MAX; index++) {
236+
if (0 == std_mutex_id_file[index]) {
237+
def.mutex = &std_mutex_file[index];
238+
std_mutex_id_file[index] = osMutexCreate(&def);
239+
*mutex = (__iar_Rmtx*)&std_mutex_id_file[index];
240+
return;
241+
}
242+
}
243+
// The variable _FOPEN_MAX needs to be increased
244+
error("Not enough mutexes\n");
245+
}
246+
247+
void __iar_file_Mtxdst(__iar_Rmtx *mutex) /* Destroy a file lock */
248+
{
249+
osMutexDelete(*(osMutexId*)*mutex);
250+
*mutex = 0;
251+
}
252+
253+
void __iar_file_Mtxlock(__iar_Rmtx *mutex) /* Lock a file lock */
254+
{
255+
osMutexWait(*(osMutexId*)*mutex, osWaitForever);
256+
}
257+
258+
void __iar_file_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a file lock */
259+
{
260+
osMutexRelease(*(osMutexId*)*mutex);
261+
}
262+
263+
#endif
193264

194265
/*----------------------------------------------------------------------------
195266
* RTX Optimizations (empty functions)
@@ -553,20 +624,25 @@ __asm void __rt_entry (void) {
553624

554625
#elif defined (__GNUC__)
555626

627+
osMutexDef(malloc_mutex);
628+
static osMutexId malloc_mutex_id;
629+
osMutexDef(env_mutex);
630+
static osMutexId env_mutex_id;
631+
556632
extern void __libc_fini_array(void);
557633
extern void __libc_init_array (void);
558634
extern int main(int argc, char **argv);
559635

560636
void pre_main(void) {
637+
malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex));
638+
env_mutex_id = osMutexCreate(osMutex(env_mutex));
561639
atexit(__libc_fini_array);
562640
__libc_init_array();
563641
main(0, NULL);
564642
}
565643

566644
__attribute__((naked)) void software_init_hook (void) {
567645
__asm (
568-
".syntax unified\n"
569-
".thumb\n"
570646
"bl osKernelInitialize\n"
571647
#ifdef __MBED_CMSIS_RTOS_CM
572648
"bl set_main_stack\n"
@@ -580,6 +656,29 @@ __attribute__((naked)) void software_init_hook (void) {
580656
);
581657
}
582658

659+
// Opaque declaration of _reent structure
660+
struct _reent;
661+
662+
void __malloc_lock( struct _reent *_r )
663+
{
664+
osMutexWait(malloc_mutex_id, osWaitForever);
665+
}
666+
667+
void __malloc_unlock( struct _reent *_r )
668+
{
669+
osMutexRelease(malloc_mutex_id);
670+
}
671+
672+
void __env_lock( struct _reent *_r )
673+
{
674+
osMutexWait(env_mutex_id, osWaitForever);
675+
}
676+
677+
void __env_unlock( struct _reent *_r )
678+
{
679+
osMutexRelease(env_mutex_id);
680+
}
681+
583682
#elif defined (__ICCARM__)
584683

585684
extern void* __vector_table;

workspace_tools/toolchains/gcc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
183183
GCC.__init__(self, target, options, notify, macros, silent, GCC_ARM_PATH, extra_verbose=extra_verbose)
184184

185185
# Use latest gcc nanolib
186-
self.ld.append("--specs=nano.specs")
186+
if "thread-safe" not in self.options:
187+
self.ld.append("--specs=nano.specs")
187188
if target.name in ["LPC1768", "LPC4088", "LPC4088_DM", "LPC4330", "UBLOX_C027", "LPC2368"]:
188189
self.ld.extend(["-u _printf_float", "-u _scanf_float"])
189190
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"]:

workspace_tools/toolchains/iar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
6666
self.asm = [join(IAR_BIN, "iasmarm")] + ["--cpu", cpuchoice]
6767
if not "analyze" in self.options:
6868
self.cc = [main_cc] + c_flags
69-
self.cppc = [main_cc, "--c++", "--no_rtti", "--no_exceptions"] + c_flags
69+
self.cppc = [main_cc, "--c++", "--no_rtti", "--no_exceptions", "--guard_calls"] + c_flags
7070
else:
7171
self.cc = [join(GOANNA_PATH, "goannacc"), '--with-cc="%s"' % main_cc.replace('\\', '/'), "--dialect=iar-arm", '--output-format="%s"' % self.GOANNA_FORMAT] + c_flags
72-
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
72+
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
7373
self.ld = join(IAR_BIN, "ilinkarm")
7474
self.ar = join(IAR_BIN, "iarchive")
7575
self.elf2bin = join(IAR_BIN, "ielftool")
@@ -114,7 +114,7 @@ def archive(self, objects, lib_path):
114114
self.default_cmd([self.ar, lib_path] + objects)
115115

116116
def link(self, output, objects, libraries, lib_dirs, mem_map):
117-
args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"]
117+
args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization", "--threaded_lib"]
118118
self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries))
119119

120120
@hook_tool

0 commit comments

Comments
 (0)