Skip to content

Commit 5acdad9

Browse files
authored
Merge pull request #1907 from c1728p9/only_allow_one_thread_for_unsafe_stdlib
Only allow one thread for unsafe stdlib
2 parents f3c125f + d0b7b3b commit 5acdad9

File tree

8 files changed

+201
-74
lines changed

8 files changed

+201
-74
lines changed

hal/common/retarget.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,35 @@ extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {}
636636
extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {}
637637
extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {}
638638
extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {}
639+
#elif defined(__CC_ARM)
640+
// Do nothing
641+
#elif defined (__GNUC__)
642+
struct _reent;
643+
// Stub out locks when an rtos is not present
644+
extern "C" WEAK void __rtos_malloc_lock( struct _reent *_r ) {}
645+
extern "C" WEAK void __rtos_malloc_unlock( struct _reent *_r ) {}
646+
extern "C" WEAK void __rtos_env_lock( struct _reent *_r ) {}
647+
extern "C" WEAK void __rtos_env_unlock( struct _reent *_r ) {}
648+
649+
void __malloc_lock( struct _reent *_r )
650+
{
651+
__rtos_malloc_lock(_r);
652+
}
653+
654+
void __malloc_unlock( struct _reent *_r )
655+
{
656+
__rtos_malloc_unlock(_r);
657+
}
658+
659+
void __env_lock( struct _reent *_r )
660+
{
661+
__rtos_env_lock(_r);
662+
}
663+
664+
void __env_unlock( struct _reent *_r )
665+
{
666+
__rtos_env_unlock(_r);
667+
}
639668
#endif
640669

641670
} // namespace mbed

hal/targets.json

Lines changed: 128 additions & 65 deletions
Large diffs are not rendered by default.

rtos/rtx/TARGET_CORTEX_A/RTX_CM_lib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
5151
#define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
5252

53-
#define OS_TCB_SIZE 52
53+
#define OS_TCB_SIZE 60
5454
#define OS_TMR_SIZE 8
5555

5656
#if defined (__CC_ARM) && !defined (__MICROLIB)

rtos/rtx/TARGET_CORTEX_A/RTX_Conf_CA.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
* RTX User configuration part BEGIN
3939
*---------------------------------------------------------------------------*/
4040

41+
#if defined(MBED_RTOS_SINGLE_THREAD)
42+
#define OS_TASKCNT 1
43+
#define OS_TIMERS 0
44+
#endif
45+
4146
//-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
4247
//
4348
// <h>Thread Configuration

rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
5252
#define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
5353

54-
#define OS_TCB_SIZE 52
54+
#define OS_TCB_SIZE 60
5555
#define OS_TMR_SIZE 8
5656

5757
typedef void *OS_ID;
@@ -527,11 +527,18 @@ extern uint32_t __end__[];
527527
#endif
528528

529529
void set_main_stack(void) {
530+
uint32_t interrupt_stack_size = ((uint32_t)OS_MAINSTKSIZE * 4);
531+
uint32_t heap_plus_stack_size = ((uint32_t)INITIAL_SP - (uint32_t)HEAP_START) - interrupt_stack_size;
532+
// Main thread's stack is 1/4 of the heap
533+
uint32_t main_stack_size = heap_plus_stack_size / 4;
534+
// The main thread must be 4 byte aligned
535+
uint32_t main_stack_start = ((uint32_t)INITIAL_SP - interrupt_stack_size - main_stack_size) & ~0x7;
536+
530537
// That is the bottom of the main stack block: no collision detection
531-
os_thread_def_main.stack_pointer = HEAP_START;
538+
os_thread_def_main.stack_pointer = (uint32_t*)main_stack_start;
532539

533540
// Leave OS_MAINSTKSIZE words for the scheduler and interrupts
534-
os_thread_def_main.stacksize = (INITIAL_SP - (unsigned int)HEAP_START) - (OS_MAINSTKSIZE * 4);
541+
os_thread_def_main.stacksize = main_stack_size;
535542
}
536543

537544
#if defined (__CC_ARM)
@@ -677,22 +684,22 @@ __attribute__((naked)) void software_init_hook_rtos (void) {
677684
// Opaque declaration of _reent structure
678685
struct _reent;
679686

680-
void __malloc_lock( struct _reent *_r )
687+
void __rtos_malloc_lock( struct _reent *_r )
681688
{
682689
osMutexWait(malloc_mutex_id, osWaitForever);
683690
}
684691

685-
void __malloc_unlock( struct _reent *_r )
692+
void __rtos_malloc_unlock( struct _reent *_r )
686693
{
687694
osMutexRelease(malloc_mutex_id);
688695
}
689696

690-
void __env_lock( struct _reent *_r )
697+
void __rtos_env_lock( struct _reent *_r )
691698
{
692699
osMutexWait(env_mutex_id, osWaitForever);
693700
}
694701

695-
void __env_unlock( struct _reent *_r )
702+
void __rtos_env_unlock( struct _reent *_r )
696703
{
697704
osMutexRelease(env_mutex_id);
698705
}

rtos/rtx/TARGET_CORTEX_M/RTX_Conf_CM.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
* RTX User configuration part BEGIN
4040
*---------------------------------------------------------------------------*/
4141

42+
#if defined(MBED_RTOS_SINGLE_THREAD)
43+
#define OS_TASKCNT 1
44+
#define OS_TIMERS 0
45+
#endif
46+
4247
//-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
4348
//
4449
// <h>Thread Configuration

tools/toolchains/arm.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
242242
self.cppc += ["-D__MICROLIB", "--library_type=microlib"]
243243
self.ld += ["--library_type=microlib"]
244244

245+
# Only allow a single thread
246+
self.cc += ["-DMBED_RTOS_SINGLE_THREAD"]
247+
self.cppc += ["-DMBED_RTOS_SINGLE_THREAD"]
248+
245249
# We had to patch microlib to add C++ support
246250
# In later releases this patch should have entered mainline
247251
if ARM_MICRO.PATCHED_LIBRARY:

tools/toolchains/gcc.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,22 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
252252
GCC.__init__(self, target, options, notify, macros, silent, GCC_ARM_PATH, extra_verbose=extra_verbose)
253253

254254
# Use latest gcc nanolib
255-
if "thread-safe" not in self.options:
255+
if "big-build" in self.options:
256+
use_nano = False
257+
elif "small-build" in self.options:
258+
use_nano = True
259+
elif target.default_build == "standard":
260+
use_nano = False
261+
elif target.default_build == "small":
262+
use_nano = True
263+
else:
264+
use_nano = False
265+
266+
if use_nano:
256267
self.ld.append("--specs=nano.specs")
268+
self.cc += ["-DMBED_RTOS_SINGLE_THREAD"]
269+
self.cppc += ["-DMBED_RTOS_SINGLE_THREAD"]
270+
257271
if target.name in ["LPC1768", "LPC4088", "LPC4088_DM", "LPC4330", "UBLOX_C027", "LPC2368"]:
258272
self.ld.extend(["-u _printf_float", "-u _scanf_float"])
259273
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"]:

0 commit comments

Comments
 (0)