sim SMP: CPU0 should not be the main thread. That introduces an asymmetry since the main thread has no pthread_t ID. Instead, start an additional pthread for CPU0. The main thread will just wait and do nothing in this case

This commit is contained in:
Gregory Nutt 2016-02-18 14:54:48 -06:00
parent 08e2fc0be8
commit 526c90986a
4 changed files with 93 additions and 9 deletions

View File

@ -32,6 +32,7 @@ pthread_create NXpthread_create
pthread_getspecific NXpthread_getspecific
pthread_key_create NXpthread_key_create
pthread_kill NXpthread_kill
pthread_join NXpthread_join
pthread_mutex_destroy NXpthread_mutex_destroy
pthread_mutex_init NXpthread_mutex_init
pthread_mutex_lock NXpthread_mutex_lock

View File

@ -91,7 +91,15 @@ int main(int argc, char **argv, char **envp)
if (setjmp(g_simabort) == 0)
{
#ifdef CONFIG_SMP
/* Start the CPU0 emulation. This should not return. */
sim_cpu0_start();
#else
/* Start the Nuttx emulation. This should not return. */
os_start();
#endif
}
/* Restore the original terminal mode and return the exit code */

View File

@ -218,7 +218,7 @@ void up_longjmp(xcpt_reg_t *jb, int val) noreturn_function;
#ifdef CONFIG_SMP
int sim_cpu0_initialize(void);
int sim_cpustart(int cpu, main_t idletask);
void sim_cpu0_start(void);
#endif
/* up_smpsignal.c *********************************************************/

View File

@ -86,6 +86,7 @@ static volatile spinlock_t g_sim_cpuwait[CONFIG_SMP_NCPUS];
* NuttX domain function prototypes
****************************************************************************/
void os_start(void) __attribute__ ((noreturn));
void sim_cpu_pause(int cpu, volatile spinlock_t *wait,
volatile unsigned char *paused);
@ -93,6 +94,54 @@ void sim_cpu_pause(int cpu, volatile spinlock_t *wait,
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: sim_cpu0_trampoline
*
* Description:
* This is a pthread task entry point. This (host) pthread is used to
* simulate a CPU0. It simply calls OS start.
*
* Input Parameters:
* arg - Standard pthread argument
*
* Returned Value:
* This function does not return
*
****************************************************************************/
static void *sim_cpu0_trampoline(void *arg)
{
sigset_t set;
int ret;
/* Set the CPU number zero for the CPU thread */
ret = pthread_setspecific(g_cpukey, (const void *)0);
if (ret != 0)
{
return NULL;
}
/* Make sure the SIGUSR1 is not masked */
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
ret = pthread_sigmask(SIG_UNBLOCK, &set, NULL);
if (ret < 0)
{
return NULL;
}
/* Give control to os_start */
os_start();
/* The os_start() should not return */
return NULL;
}
/****************************************************************************
* Name: sim_idle_trampoline
*
@ -209,14 +258,6 @@ int sim_cpu0_initialize(void)
return -ret;
}
/* Set the CPU number zero for the main thread */
ret = pthread_setspecific(g_cpukey, (const void *)0);
if (ret != 0)
{
return -ret;
}
/* Register the common signal handler for all threads */
act.sa_sigaction = sim_handle_signal;
@ -243,6 +284,40 @@ int sim_cpu0_initialize(void)
return 0;
}
/****************************************************************************
* Name: sim_cpu0_start
*
* Description:
* Start CPU0 and initialize the operating system.
*
* Input Parameters:
* None
*
* Returned Value:
* Does not return
*
****************************************************************************/
void sim_cpu0_start(void)
{
void *value;
int ret;
/* Start the CPU0 emulation thread. This is analogous to power-up reset
* of CPU0 in a multi-CPU hardware model.
*/
ret = pthread_create(&g_sim_cputhread[0], NULL, sim_cpu0_trampoline, NULL);
if (ret == 0)
{
/* The CPU0 emulation thread should never return, the main thread will
* wait just in case.
*/
(void)pthread_join(g_sim_cputhread[0], &value);
}
}
/****************************************************************************
* Name: up_cpu_index
*