You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/02/09 13:38:47 UTC

[incubator-nuttx] branch pr236 updated (934f468 -> 8bcb541)

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a change to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git.


    from 934f468  Run all .c and .h files in PR235 through tools/nxstyle.
     new c8ead97  sched/init: Remove the duplicated tcb flag/cpu setting in nx_smp_start
     new 487e945  sim/arch: Fix up_head.c:122:11:implicit declaration of function 'up_cpu_index'
     new 9a571bb  arch/sim: Set idle thread stack size to zero
     new bad448b  arch/sim: Replace pthread mutex with atomic function for up_testset
     new 583506d  arch/sim: Reuse the main thread as the simulated CPU0
     new 599f05c  arch/sim: fix up_idle call the wrong pthread_yield version
     new 8bcb541  sim/README.txt: Update the SMP related section

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 arch/sim/src/Makefile              |   4 +-
 arch/sim/src/sim/up_cpuidlestack.c |   2 +-
 arch/sim/src/sim/up_head.c         |   9 +--
 arch/sim/src/sim/up_idle.c         |  35 ----------
 arch/sim/src/sim/up_internal.h     |  10 +--
 arch/sim/src/sim/up_simsmp.c       | 131 +++++++++----------------------------
 arch/sim/src/sim/up_smphook.c      |  85 ------------------------
 arch/sim/src/sim/up_smpsignal.c    |  20 ++++++
 arch/sim/src/sim/up_testset.c      |  26 +++-----
 boards/sim/sim/sim/README.txt      |  16 +++++
 sched/init/nx_smpstart.c           |  26 --------
 sched/init/nx_start.c              |   6 +-
 12 files changed, 83 insertions(+), 287 deletions(-)
 delete mode 100644 arch/sim/src/sim/up_smphook.c


[incubator-nuttx] 04/07: arch/sim: Replace pthread mutex with atomic function for up_testset

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit bad448bb29e2aa66d0bd46c3d02dd7d254210159
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Feb 9 12:44:07 2020 +0800

    arch/sim: Replace pthread mutex with atomic function for up_testset
    
    up_testset may be called from signal hander but it isn't safe to call pthread API in that context
    
    Change-Id: Ic1213b200f6e9fb01b16cb245d067732b00d3e14
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 arch/sim/src/sim/up_testset.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/arch/sim/src/sim/up_testset.c b/arch/sim/src/sim/up_testset.c
index 38d53b9..d0c8d5c 100644
--- a/arch/sim/src/sim/up_testset.c
+++ b/arch/sim/src/sim/up_testset.c
@@ -38,7 +38,10 @@
  ****************************************************************************/
 
 #include <stdint.h>
-#include <pthread.h>
+
+#ifdef CONFIG_SMP
+#  include <stdatomic.h>
+#endif
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -58,14 +61,6 @@
 typedef uint8_t spinlock_t;
 
 /****************************************************************************
- * Private Data
- ****************************************************************************/
-
-#ifdef CONFIG_SMP
-static pthread_mutex_t g_tsmutex = PTHREAD_MUTEX_INITIALIZER;
-#endif
-
-/****************************************************************************
  * Public Functions
  ****************************************************************************/
 
@@ -92,12 +87,12 @@ static pthread_mutex_t g_tsmutex = PTHREAD_MUTEX_INITIALIZER;
 spinlock_t up_testset(volatile spinlock_t *lock)
 {
 #ifdef CONFIG_SMP
-  /* In the multi-CPU SMP case, we use a mutex to assure that the following
-   * test and set is atomic.
+  /* In the multi-CPU SMP case, we use atomic operation to assure that the
+   * following test and set is atomic.
    */
 
-  pthread_mutex_lock(&g_tsmutex);
-#endif
+  return atomic_exchange(lock, SP_LOCKED);
+#else
 
   /* In the non-SMP case, the simulation is implemented with a single thread
    * the test-and-set operation is inherently atomic.
@@ -105,9 +100,6 @@ spinlock_t up_testset(volatile spinlock_t *lock)
 
   spinlock_t ret = *lock;
   *lock = SP_LOCKED;
-
-#ifdef CONFIG_SMP
-  pthread_mutex_unlock(&g_tsmutex);
-#endif
   return ret;
+#endif
 }


[incubator-nuttx] 01/07: sched/init: Remove the duplicated tcb flag/cpu setting in nx_smp_start

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit c8ead97841014bfb6fe6fb86de521e2a18652ed0
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Feb 9 14:39:06 2020 +0800

    sched/init: Remove the duplicated tcb flag/cpu setting in nx_smp_start
    
    the same thing is already done in nx_start
    
    Change-Id: I864ba7d52ae3cc10c86a28978eb3c1a814dd0529
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 sched/init/nx_smpstart.c | 26 --------------------------
 sched/init/nx_start.c    |  6 +-----
 2 files changed, 1 insertion(+), 31 deletions(-)

diff --git a/sched/init/nx_smpstart.c b/sched/init/nx_smpstart.c
index 3ff923d..88de54a 100644
--- a/sched/init/nx_smpstart.c
+++ b/sched/init/nx_smpstart.c
@@ -56,24 +56,6 @@
 #ifdef CONFIG_SMP
 
 /****************************************************************************
- * Private Types
- ****************************************************************************/
-
-struct nx_tcballoc_s
-{
-  struct task_tcb_s tcb;  /* IDLE task TCB */
-  FAR char *idleargv[2];  /* Argument list */
-};
-
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-#if CONFIG_TASK_NAME_SIZE < 1
-static const char g_idlename[] = "CPUn Idle"
-#endif
-
-/****************************************************************************
  * Public Functions
  ****************************************************************************/
 
@@ -219,14 +201,6 @@ int nx_smp_start(void)
        */
 
       up_initial_state(tcb);
-
-      /* Set the task flags to indicate that this is a kernel thread and that
-       * this task is locked to this CPU.
-       */
-
-      tcb->flags = (TCB_FLAG_TTYPE_KERNEL | TCB_FLAG_NONCANCELABLE |
-                    TCB_FLAG_CPU_LOCKED);
-      tcb->cpu   = cpu;
     }
 
   /* Then start all of the other CPUs after we have completed the memory
diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c
index 9d4d35e..0117fb3 100644
--- a/sched/init/nx_start.c
+++ b/sched/init/nx_start.c
@@ -389,11 +389,7 @@ static FAR char *g_idleargv[1][2];
 
 void nx_start(void)
 {
-#ifdef CONFIG_SMP
-  int cpu;
-#else
-# define cpu 0
-#endif
+  int cpu = 0;
   int i;
 
   sinfo("Entry\n");


[incubator-nuttx] 07/07: sim/README.txt: Update the SMP related section

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 8bcb541d2de7af93dbc85f4b02b6d237dae8b9f0
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Feb 9 19:18:40 2020 +0800

    sim/README.txt: Update the SMP related section
    
    Change-Id: I81db2697c133a1941f7bb6033d57b0ca18c9d9c0
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 boards/sim/sim/sim/README.txt | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/boards/sim/sim/sim/README.txt b/boards/sim/sim/sim/README.txt
index 722d228..d6673ee 100644
--- a/boards/sim/sim/sim/README.txt
+++ b/boards/sim/sim/sim/README.txt
@@ -288,6 +288,9 @@ SMP
   result is undefined:
   http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1318.htm
 
+  Update: The dead lock is due to up_testset call pthread API for synchronization
+  inside the signal handler. After swiching to atomic API, the problem get resolved.
+
   You can enable SMP for ostest configuration by enabling:
 
     +CONFIG_SPINLOCK=y
@@ -322,6 +325,19 @@ SMP
   often simuart_post() will be called from CPU1 and it will try to restart NSH
   on CPU0 and, again, the same quirkiness occurs.
 
+  Update: Only CPU0 call up_idle now, other CPUs have a simple idle loop:
+
+    /* The idle Loop */
+
+    for (; ; )
+      {
+        /* Give other pthreads/CPUs a shot */
+
+        pthread_yield();
+      }
+
+  So it isn't a problem any more.
+
   But for example, this command:
 
     nsh> sleep 1 &


[incubator-nuttx] 02/07: sim/arch: Fix up_head.c:122:11:implicit declaration of function 'up_cpu_index'

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 487e945362d571b011dff32ee734ca869daf3ebd
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Feb 9 14:49:24 2020 +0800

    sim/arch: Fix up_head.c:122:11:implicit declaration of function 'up_cpu_index'
    
    Change-Id: I2b73eda6924d21c065e520c697b8d2b49ffc30a0
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 arch/sim/src/sim/up_head.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/sim/src/sim/up_head.c b/arch/sim/src/sim/up_head.c
index 8667f41..b2aa0c8 100644
--- a/arch/sim/src/sim/up_head.c
+++ b/arch/sim/src/sim/up_head.c
@@ -46,6 +46,7 @@
 #include <assert.h>
 
 #include <nuttx/init.h>
+#include <nuttx/arch.h>
 #include <nuttx/board.h>
 #include <nuttx/syslog/syslog_rpmsg.h>
 


[incubator-nuttx] 03/07: arch/sim: Set idle thread stack size to zero

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 9a571bb116bf85b0e53f28a119fac67203a760c9
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Feb 9 20:12:51 2020 +0800

    arch/sim: Set idle thread stack size to zero
    
    to avoid ps generate "Segmentation fault" because do_stackcheck
    check the stack color if the stack size doesn't equal zero
    BTW, it is better to set idle task stack size to zero since idle
    task use the host thread and the stack size from config isn't the
    actual value.
    
    Change-Id: I82c2a8358df7cc9a7a4f200f033df1444096d688
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 arch/sim/src/sim/up_cpuidlestack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/sim/src/sim/up_cpuidlestack.c b/arch/sim/src/sim/up_cpuidlestack.c
index 04aed42..d7e3746 100644
--- a/arch/sim/src/sim/up_cpuidlestack.c
+++ b/arch/sim/src/sim/up_cpuidlestack.c
@@ -99,7 +99,7 @@ int up_cpu_idlestack(int cpu, FAR struct tcb_s *tcb, size_t stack_size)
 {
   /* REVISIT:  I don't think anything is needed here */
 
-  tcb->adj_stack_size = stack_size;
+  tcb->adj_stack_size  = 0;
   tcb->stack_alloc_ptr = NULL;
   tcb->adj_stack_ptr   = NULL;
   return OK;


[incubator-nuttx] 06/07: arch/sim: fix up_idle call the wrong pthread_yield version

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 599f05c4b40ed66c10ce77e26bda887ec998875c
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Feb 9 14:36:02 2020 +0800

    arch/sim: fix up_idle call the wrong pthread_yield version
    
    The logic want the host version but link to NuttX version
    
    Change-Id: I41d7d294d38c9ef6fbbf0f63268fffe637099eb1
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 arch/sim/src/Makefile           |  4 +-
 arch/sim/src/sim/up_idle.c      | 35 -----------------
 arch/sim/src/sim/up_internal.h  |  9 +----
 arch/sim/src/sim/up_simsmp.c    | 24 ++++++------
 arch/sim/src/sim/up_smphook.c   | 85 -----------------------------------------
 arch/sim/src/sim/up_smpsignal.c | 20 ++++++++++
 6 files changed, 36 insertions(+), 141 deletions(-)

diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile
index 6e05767..3ea0c70 100644
--- a/arch/sim/src/Makefile
+++ b/arch/sim/src/Makefile
@@ -89,8 +89,8 @@ ifeq ($(CONFIG_SPINLOCK),y)
 endif
 
 ifeq ($(CONFIG_SMP),y)
-  CSRCS += up_smpsignal.c up_smphook.c up_cpuidlestack.c
-  REQUIREDOBJS += up_smpsignal$(OBJEXT) up_smphook$(OBJEXT)
+  CSRCS += up_smpsignal.c up_cpuidlestack.c
+  REQUIREDOBJS += up_smpsignal$(OBJEXT)
   HOSTCFLAGS += -DCONFIG_SMP=1 -DCONFIG_SMP_NCPUS=$(CONFIG_SMP_NCPUS)
   HOSTSRCS += up_simsmp.c
   STDLIBS += -lpthread
diff --git a/arch/sim/src/sim/up_idle.c b/arch/sim/src/sim/up_idle.c
index 1848d45..2b53cbf 100644
--- a/arch/sim/src/sim/up_idle.c
+++ b/arch/sim/src/sim/up_idle.c
@@ -40,15 +40,10 @@
 
 #include <nuttx/config.h>
 
-#include <pthread.h>
 #include <time.h>
 
 #include <nuttx/arch.h>
 
-#ifdef CONFIG_SMP
-#  include <nuttx/spinlock.h>
-#endif
-
 #include "up_internal.h"
 
 /****************************************************************************
@@ -94,26 +89,6 @@ extern void up_x11update(void);
 
 void up_idle(void)
 {
-#ifdef CONFIG_SMP
-  /* In the SMP configuration, only one CPU should do these operations.  It
-   * should not matter which, however.
-   */
-
-  static volatile spinlock_t lock SP_SECTION = SP_UNLOCKED;
-
-  /* The one that gets the lock is the one that executes the IDLE operations */
-
-  if (up_testset(&lock) != SP_UNLOCKED)
-    {
-      /* We didn't get it... Give other pthreads/CPUs a shot and try again
-       * later.
-       */
-
-      pthread_yield();
-      return;
-    }
-#endif
-
 #ifdef CONFIG_SCHED_TICKLESS
   /* Driver the simulated interval timer */
 
@@ -192,14 +167,4 @@ void up_idle(void)
     }
 #endif
 #endif
-
-#ifdef CONFIG_SMP
-  /* Release the spinlock */
-
-  lock = SP_UNLOCKED;
-
-  /* Give other pthreads/CPUs a shot */
-
-  pthread_yield();
-#endif
 }
diff --git a/arch/sim/src/sim/up_internal.h b/arch/sim/src/sim/up_internal.h
index 2be6e0b..6f90e2d 100644
--- a/arch/sim/src/sim/up_internal.h
+++ b/arch/sim/src/sim/up_internal.h
@@ -245,14 +245,7 @@ void sim_cpu0_start(void);
 /* up_smpsignal.c ***********************************************************/
 
 #ifdef CONFIG_SMP
-void sim_cpu_pause(int cpu, FAR volatile spinlock_t *wait,
-                   FAR volatile unsigned char *paused);
-#endif
-
-/* up_smphook.c *************************************************************/
-
-#ifdef CONFIG_SMP
-void sim_smp_hook(void);
+void up_cpu_started(void);
 #endif
 
 /* up_tickless.c ************************************************************/
diff --git a/arch/sim/src/sim/up_simsmp.c b/arch/sim/src/sim/up_simsmp.c
index a314418..5b67954 100644
--- a/arch/sim/src/sim/up_simsmp.c
+++ b/arch/sim/src/sim/up_simsmp.c
@@ -61,10 +61,6 @@
 
 typedef unsigned char spinlock_t;
 
-/* Task entry point type */
-
-typedef int (*main_t)(int argc, char **argv);
-
 struct sim_cpuinfo_s
 {
   int cpu;                /* CPU number */
@@ -99,9 +95,9 @@ volatile spinlock_t g_cpu_paused[CONFIG_SMP_NCPUS];
  * NuttX domain function prototypes
  ****************************************************************************/
 
-void nx_start(void) __attribute__ ((noreturn));
-void up_cpu_paused(int cpu);
-void sim_smp_hook(void);
+void nx_start(void);
+void up_cpu_started(void);
+int up_cpu_paused(int cpu);
 
 /****************************************************************************
  * Private Functions
@@ -156,14 +152,20 @@ static void *sim_idle_trampoline(void *arg)
 
   pthread_mutex_unlock(&cpuinfo->mutex);
 
-  /* Give control to the IDLE task via the nasty little sim_smp_hook().
-   * sim_smp_hook() is logically a part of this function but needs to be
+  /* up_cpu_started() is logically a part of this function but needs to be
    * inserted in the path because in needs to access NuttX domain definitions.
    */
 
-  sim_smp_hook();
+  up_cpu_started();
 
-  /* The IDLE task will not return.  This is just to keep the compiler happy */
+  /* The idle Loop */
+
+  for (; ; )
+    {
+      /* Give other pthreads/CPUs a shot */
+
+      pthread_yield();
+    }
 
   return NULL;
 }
diff --git a/arch/sim/src/sim/up_smphook.c b/arch/sim/src/sim/up_smphook.c
deleted file mode 100644
index e94625c..0000000
--- a/arch/sim/src/sim/up_smphook.c
+++ /dev/null
@@ -1,85 +0,0 @@
-/****************************************************************************
- * arch/sim/src/sim/up_simhook.c
- *
- *   Copyright (C) 2016 Gregory Nutt. All rights reserved.
- *   Author: Gregory Nutt <gn...@nuttx.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- * 3. Neither the name NuttX nor the names of its contributors may be
- *    used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <assert.h>
-
-#include <nuttx/sched.h>
-
-#include "sched/sched.h"
-
-#ifdef CONFIG_SMP
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: sim_smp_hook
- *
- * Description:
- *   This is a mindless little hook between sim_idle_trampoline() and the
- *   real IDLE task logic.  This is only needed because we need to access
- *   elements of the TCB in the simulation domain but sim_idle_trampoline()
- *   lies in the host domain.
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   None (should not return)
- *
- ****************************************************************************/
-
-void sim_smp_hook(void)
-{
-  struct tcb_s *tcb = this_task();
-  DEBUGASSERT(tcb != NULL && tcb->start != NULL);
-
-  /* Transfer control to the "real" thread start-up routine */
-
-  tcb->start();
-
-  /* That function should not return. */
-
-  DEBUGPANIC();
-}
-
-#endif /* CONFIG_SMP */
diff --git a/arch/sim/src/sim/up_smpsignal.c b/arch/sim/src/sim/up_smpsignal.c
index da1d8bb..126269c 100644
--- a/arch/sim/src/sim/up_smpsignal.c
+++ b/arch/sim/src/sim/up_smpsignal.c
@@ -40,6 +40,7 @@
 #include <nuttx/config.h>
 
 #include <nuttx/sched.h>
+#include <nuttx/sched_note.h>
 #include <nuttx/spinlock.h>
 
 #include "sched/sched.h"
@@ -158,4 +159,23 @@ int up_cpu_paused(int cpu)
   return OK;
 }
 
+/****************************************************************************
+ * Name: up_cpu_started
+ *
+ * Description:
+ *   Notify the current cpu start sucessfully.
+ *
+ ****************************************************************************/
+
+void up_cpu_started(void)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION
+  FAR struct tcb_s *tcb = this_task();
+
+  /* Announce that the IDLE task has started */
+
+  sched_note_start(tcb);
+#endif
+}
+
 #endif /* CONFIG_SMP */


[incubator-nuttx] 05/07: arch/sim: Reuse the main thread as the simulated CPU0

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr236
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 583506dd1ead970bf462f0f81efb4ada2cf358e1
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Feb 9 17:56:44 2020 +0800

    arch/sim: Reuse the main thread as the simulated CPU0
    
    Change-Id: I34b5b412b8aba2181074357f0b5b800b392deb42
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 arch/sim/src/sim/up_head.c     |   8 +--
 arch/sim/src/sim/up_internal.h |   1 -
 arch/sim/src/sim/up_simsmp.c   | 107 +++++++----------------------------------
 3 files changed, 19 insertions(+), 97 deletions(-)

diff --git a/arch/sim/src/sim/up_head.c b/arch/sim/src/sim/up_head.c
index b2aa0c8..33e8687 100644
--- a/arch/sim/src/sim/up_head.c
+++ b/arch/sim/src/sim/up_head.c
@@ -81,13 +81,7 @@ int main(int argc, char **argv, char **envp)
   syslog_rpmsg_init_early("server", g_logbuffer, sizeof(g_logbuffer));
 #endif
 
-#ifdef CONFIG_SMP
-  /* In the SMP case, configure the main thread as CPU 0 */
-
-  sim_cpu0_initialize();
-#endif
-
-  /* Then start NuttX */
+  /* Start NuttX */
 
   if (setjmp(g_simabort) == 0)
     {
diff --git a/arch/sim/src/sim/up_internal.h b/arch/sim/src/sim/up_internal.h
index dc14430..2be6e0b 100644
--- a/arch/sim/src/sim/up_internal.h
+++ b/arch/sim/src/sim/up_internal.h
@@ -239,7 +239,6 @@ int up_hostusleep(unsigned int usec);
 /* up_simsmp.c **************************************************************/
 
 #ifdef CONFIG_SMP
-int  sim_cpu0_initialize(void);
 void sim_cpu0_start(void);
 #endif
 
diff --git a/arch/sim/src/sim/up_simsmp.c b/arch/sim/src/sim/up_simsmp.c
index 3dbd975..a314418 100644
--- a/arch/sim/src/sim/up_simsmp.c
+++ b/arch/sim/src/sim/up_simsmp.c
@@ -108,54 +108,6 @@ void sim_smp_hook(void);
  ****************************************************************************/
 
 /****************************************************************************
- * 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 nx_start() */
-
-  nx_start();
-
-  /* nx_start() should not return */
-
-  return NULL;
-}
-
-/****************************************************************************
  * Name: sim_idle_trampoline
  *
  * Description:
@@ -243,7 +195,7 @@ static void sim_handle_signal(int signo, siginfo_t *info, void *context)
  ****************************************************************************/
 
 /****************************************************************************
- * Name: sim_cpu0_initialize
+ * Name: sim_cpu0_start
  *
  * Description:
  *   Create the pthread-specific data key and set the indication of CPU0
@@ -253,23 +205,32 @@ static void sim_handle_signal(int signo, siginfo_t *info, void *context)
  *   None
  *
  * Returned Value:
- *   An integer index in the range of 0 through (CONFIG_SMP_NCPUS-1) that
- *   corresponds to the currently executing CPU.
+ *   Does not return
  *
  ****************************************************************************/
 
-int sim_cpu0_initialize(void)
+void sim_cpu0_start(void)
 {
   struct sigaction act;
   sigset_t set;
   int ret;
 
+  g_sim_cputhread[0] = pthread_self();
+
   /* Create the pthread key */
 
   ret = pthread_key_create(&g_cpukey, NULL);
   if (ret != 0)
     {
-      return -ret;
+      return;
+    }
+
+  /* Set the CPU number zero for the CPU thread */
+
+  ret = pthread_setspecific(g_cpukey, (const void *)0);
+  if (ret != 0)
+    {
+      return;
     }
 
   /* Register the common signal handler for all threads */
@@ -281,7 +242,7 @@ int sim_cpu0_initialize(void)
   ret = sigaction(SIGUSR1, &act, NULL);
   if (ret < 0)
     {
-      return -errno;
+      return;
     }
 
   /* Make sure the SIGUSR1 is not masked */
@@ -292,44 +253,12 @@ int sim_cpu0_initialize(void)
   ret = sigprocmask(SIG_UNBLOCK, &set, NULL);
   if (ret < 0)
     {
-      return -errno;
+      return;
     }
 
-  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.
-       */
+  /* Give control to nx_start() */
 
-      pthread_join(g_sim_cputhread[0], &value);
-    }
+  nx_start();
 }
 
 /****************************************************************************