sched: Call c++ global variables constructor inside nxtask_startup

to avoid the similar code spread around each application

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I8967d647eaf2ecae47f29f83e7fa322ef1b42a02
This commit is contained in:
Xiang Xiao
2020-07-01 01:58:58 +08:00
committed by patacongo
parent e9c7df4769
commit 8153e31753
6 changed files with 125 additions and 30 deletions
+20
View File
@@ -1033,6 +1033,26 @@ void nxtask_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook,
FAR void *arg);
#endif
/********************************************************************************
* Name: nxtask_startup
*
* Description:
* This function is the user-space, task startup function. It is called
* from up_task_start() in user-mode.
*
* Input Parameters:
* entrypt - The user-space address of the task entry point
* argc and argv - Standard arguments for the task entry point
*
* Returned Value:
* None. This function does not return.
*
********************************************************************************/
#ifndef CONFIG_BUILD_KERNEL
void nxtask_startup(main_t entrypt, int argc, FAR char *argv[]);
#endif
/********************************************************************************
* Internal vfork support. The overall sequence is:
*
+1 -23
View File
@@ -117,8 +117,7 @@ struct userspace_s
/* Task/thread startup routines */
CODE void (*task_startup)(main_t entrypt, int argc, FAR char *argv[])
noreturn_function;
CODE void (*task_startup)(main_t entrypt, int argc, FAR char *argv[]);
#ifndef CONFIG_DISABLE_PTHREAD
CODE void (*pthread_startup)(pthread_startroutine_t entrypt,
pthread_addr_t arg);
@@ -152,27 +151,6 @@ extern "C"
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: nxtask_startup
*
* Description:
* This function is the user-space, task startup function. It is called
* from up_task_start() in user-mode.
*
* Input Parameters:
* entrypt - The user-space address of the task entry point
* argc and argv - Standard arguments for the task entry point
*
* Returned Value:
* None. This function does not return.
*
****************************************************************************/
#ifndef __KERNEL__
void nxtask_startup(main_t entrypt, int argc, FAR char *argv[])
noreturn_function;
#endif
/****************************************************************************
* Name: pthread_startup
*
+1 -1
View File
@@ -45,7 +45,7 @@ ifeq ($(CONFIG_SMP),y)
CSRCS += sched_cpucount.c
endif
ifeq ($(CONFIG_BUILD_PROTECTED),y)
ifneq ($(CONFIG_BUILD_KERNEL),y)
CSRCS += task_startup.c
endif
+91 -3
View File
@@ -39,12 +39,94 @@
#include <nuttx/config.h>
#include <sched.h>
#include <stdlib.h>
#include <assert.h>
#include <nuttx/userspace.h>
#ifndef CONFIG_BUILD_KERNEL
#if defined(CONFIG_BUILD_PROTECTED) && !defined(__KERNEL__)
/****************************************************************************
* Private Types
****************************************************************************/
/* This type defines one entry in initialization array */
typedef CODE void (*initializer_t)(void);
/****************************************************************************
* External References
****************************************************************************/
/* _sinit and _einit are symbols exported by the linker script that mark the
* beginning and the end of the C++ initialization section.
*/
extern initializer_t _sinit;
extern initializer_t _einit;
/* _stext and _etext are symbols exported by the linker script that mark the
* beginning and the end of text.
*/
extern uintptr_t _stext;
extern uintptr_t _etext;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: cxx_initialize
*
* Description:
* If C++ and C++ static constructors are supported, then this function
* must be provided by board-specific logic in order to perform
* initialization of the static C++ class instances.
*
* This function should then be called in the application-specific
* user_start logic in order to perform the C++ initialization. NOTE
* that no component of the core NuttX RTOS logic is involved; this
* function definition only provides the 'contract' between application
* specific C++ code and platform-specific toolchain support.
*
****************************************************************************/
static void cxx_initialize(void)
{
#ifdef CONFIG_HAVE_CXXINITIALIZE
static int inited = 0;
if (inited == 0)
{
initializer_t *initp;
sinfo("_sinit: %p _einit: %p _stext: %p _etext: %p\n",
&_sinit, &_einit, &_stext, &_etext);
/* Visit each entry in the initialization table */
for (initp = &_sinit; initp != &_einit; initp++)
{
initializer_t initializer = *initp;
sinfo("initp: %p initializer: %p\n", initp, initializer);
/* Make sure that the address is non-NULL and lies in the text
* region defined by the linker script. Some toolchains may put
* NULL values or counts in the initialization table.
*/
if ((FAR void *)initializer >= (FAR void *)&_stext &&
(FAR void *)initializer < (FAR void *)&_etext)
{
sinfo("Calling %p\n", initializer);
initializer();
}
}
inited = 1;
}
#endif
}
/****************************************************************************
* Public Functions
@@ -70,6 +152,12 @@ void nxtask_startup(main_t entrypt, int argc, FAR char *argv[])
{
DEBUGASSERT(entrypt);
/* If C++ initialization for static constructors is supported, then do
* that first
*/
cxx_initialize();
/* Call the 'main' entry point passing argc and argv, calling exit()
* if/when the task returns.
*/
@@ -77,4 +165,4 @@ void nxtask_startup(main_t entrypt, int argc, FAR char *argv[])
exit(entrypt(argc, argv));
}
#endif /* CONFIG_BUILD_PROTECTED && !__KERNEL__ */
#endif /* !CONFIG_BUILD_KERNEL */
+8
View File
@@ -22,6 +22,14 @@ config HAVE_CXX
if HAVE_CXX
config HAVE_CXXINITIALIZE
bool "Have C++ initialization"
default n
---help---
The platform-specific logic includes support for initialization
of static C++ instances for this architecture and for the selected
toolchain (via up_cxxinitialize()).
config CXX_EXCEPTION
bool
+4 -3
View File
@@ -89,7 +89,7 @@
void nxtask_start(void)
{
FAR struct task_tcb_s *tcb = (FAR struct task_tcb_s *)this_task();
int exitcode;
int exitcode = EXIT_FAILURE;
int argc;
DEBUGASSERT((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != \
@@ -138,13 +138,14 @@ void nxtask_start(void)
if ((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
{
up_task_start(tcb->cmn.entry.main, argc, tcb->argv);
exitcode = EXIT_FAILURE; /* Should not get here */
}
else
#endif
{
exitcode = tcb->cmn.entry.main(argc, tcb->argv);
}
#else
nxtask_startup(tcb->cmn.entry.main, argc, tcb->argv);
#endif
/* Call exit() if/when the task returns */