diff --git a/ChangeLog b/ChangeLog
index c812bf773869760df2a1685204386de449bfff48..ad31d368368e6d1b93f2b1e6d4cb46c52f7cf160 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -306,4 +306,6 @@
 	* Add support for Windows native toolchains that cannot follow Cygwin soft links
 	* Modified serial driver interface to handle hardware with non-16550A-like
 	  interrupt architecture (like the Z16F)
-	* Added a "dumb" serial console driver to simply OS bringup
+	* Added a "dumb" serial console driver to simplify OS bringup
+	* Corrected a bug that caused the errno value of one task to be clobbered
+	  when a different task exits.  Effects all architectures.
diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html
index 2c7d6531a9f11f0f3275a1de93ab23fee3fdfd22..0fdeffaee2df5f4bfa3157e24860d0e680dc1d14 100644
--- a/Documentation/NuttX.html
+++ b/Documentation/NuttX.html
@@ -8,7 +8,7 @@
   <tr align="center" bgcolor="#e4e4e4">
     <td>
       <h1><big><font color="#3c34ec"><i>NuttX RTOS</i></font></big></h1>
-      <p>Last Updated: January 9, 2008</p>
+      <p>Last Updated: January 30, 2008</p>
     </td>
   </tr>
 </table>
@@ -984,7 +984,9 @@ Other memory:
 	* Add support for Windows native toolchains that cannot follow Cygwin soft links
 	* Modified serial driver interface to handle hardware with non-16550A-like
 	  interrupt architecture (like the Z16F)
-	* Added a &quot;dumb&quot; serial console driver to simply OS bringup
+	* Added a &quot;dumb&quot; serial console driver to simplify OS bringup
+	* Corrected a bug that caused the errno value of one task to be clobbered
+	  when a different task exits.  Effects all architectures.
 </pre></ul>
 
 <table width ="100%">
diff --git a/arch/arm/src/common/up_exit.c b/arch/arm/src/common/up_exit.c
index e760b00cf6d098c488975aa7c6ace9303f701dd9..2660ed80bbb047cc4370946c4e2be54c84957e2d 100644
--- a/arch/arm/src/common/up_exit.c
+++ b/arch/arm/src/common/up_exit.c
@@ -58,17 +58,16 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Private Funtions
+ * Private Functions
  ****************************************************************************/
 
 /****************************************************************************
  * Name: _up_dumponexit
  *
  * Description:
- *   Dump the state of all tasks whenever on task exits.  This
- *   is debug instrumentation that was added to check file-
- *   related reference counting but could be useful again
- *   sometime in the future.
+ *   Dump the state of all tasks whenever on task exits.  This is debug
+ *   instrumentation that was added to check file-related reference counting
+ *   but could be useful again sometime in the future.
  *
  ****************************************************************************/
 
@@ -133,13 +132,15 @@ static void _up_dumponexit(FAR _TCB *tcb, FAR void *arg)
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
  ****************************************************************************/
 
 void _exit(int status)
 {
-  _TCB* tcb = (_TCB*)g_readytorun.head;
+  _TCB* tcb;
 
   /* Disable interrupts.  They will be restored when the next
    * task is started.
@@ -150,41 +151,13 @@ void _exit(int status)
   lldbg("TCB=%p exitting\n", tcb);
 
 #if defined(CONFIG_DUMP_ON_EXIT) && defined(CONFIG_DEBUG)
-  dbg("Other tasks:\n");
+  lldbg("Other tasks:\n");
   sched_foreach(_up_dumponexit, NULL);
 #endif
 
-  /* Remove the tcb task from the ready-to-run list.  We can
-   * ignore the return value because we know that a context
-   * switch is needed.
-   */
-
-  (void)sched_removereadytorun(tcb);
-
-  /* We are not in a bad stack-- the head of the ready to run task list
-   * does not correspond to the thread that is running.  Disabling pre-
-   * emption on this TCB should be enough to keep things stable.
-   */
-
-  sched_lock();
-
-  /* Move the TCB to the specified blocked task list and delete it */
-
-  sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
-  task_delete(tcb->pid);
-
-  /* If there are any pending tasks, then add them to the g_readytorun
-   * task list now
-   */
-
-  if (g_pendingtasks.head)
-    {
-      (void)sched_mergepending();
-    }
-
-  /* Now calling sched_unlock() should have no effect */
+  /* Destroy the task at the head of the ready to run list. */
 
-  sched_unlock();
+  (void)task_deletecurrent();
 
   /* Now, perform the context switch to the new ready-to-run task at the
    * head of the list.
diff --git a/arch/c5471/src/up_exit.c b/arch/c5471/src/up_exit.c
index 2ef47bdfbc102c8f6e9451c64f79990f7d88c00f..4d117fce4bf43bad09d75c2457c2953697be8016 100644
--- a/arch/c5471/src/up_exit.c
+++ b/arch/c5471/src/up_exit.c
@@ -1,7 +1,7 @@
-/************************************************************
+/****************************************************************************
  * up_exit.c
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 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.
  *
@@ -31,11 +31,11 @@
  * 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 <sys/types.h>
@@ -49,28 +49,27 @@
 #include <nuttx/fs.h>
 #endif
 
-/************************************************************
+/****************************************************************************
  * Private Definitions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Data
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
- * Private Funtions
- ************************************************************/
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: _up_dumponexit
  *
  * Description:
- *   Dump the state of all tasks whenever on task exits.  This
- *   is debug instrumentation that was added to check file-
- *   related reference counting but could be useful again
- *   sometime in the future.
+ *   Dump the state of all tasks whenever on task exits.  This is debug
+ *   instrumentation that was added to check file-related reference counting
+ *   but could be useful again sometime in the future.
  *
- ************************************************************/
+ ****************************************************************************/
 
 #if defined(CONFIG_DUMP_ON_EXIT) && defined(CONFIG_DEBUG)
 static void _up_dumponexit(FAR _TCB *tcb, FAR void *arg)
@@ -113,23 +112,30 @@ static void _up_dumponexit(FAR _TCB *tcb, FAR void *arg)
 }
 #endif
 
-/************************************************************
- * Public Funtions
- ************************************************************/
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: _exit
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
- ************************************************************/
+ ****************************************************************************/
 
 void _exit(int status)
 {
-  _TCB* tcb = (_TCB*)g_readytorun.head;
-  irqstate_t flags = irqsave();
+  _TCB* tcb;
+
+  /* Disable interrupts.  They will be restored when the next
+   * task is started.
+   */
+
+  (void)irqsave();
 
   lldbg("TCB=%p exitting\n", tcb);
 
@@ -138,37 +144,9 @@ void _exit(int status)
   sched_foreach(_up_dumponexit, NULL);
 #endif
 
-  /* Remove the tcb task from the ready-to-run list.  We can
-   * ignore the return value because we know that a context
-   * switch is needed.
-   */
-
-  (void)sched_removereadytorun(tcb);
-
-  /* We are not in a bad stack-- the head of the ready to run task list
-   * does not correspond to the thread that is running.  Disabling pre-
-   * emption on this TCB should be enough to keep things stable.
-   */
-
-  sched_lock();
-
-  /* Move the TCB to the specified blocked task list and delete it */
-
-  sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
-  task_delete(tcb->pid);
-
-  /* If there are any pending tasks, then add them to the g_readytorun
-   * task list now
-   */
-
-  if (g_pendingtasks.head)
-    {
-      (void)sched_mergepending();
-    }
-
-  /* Now calling sched_unlock() should have no effect */
+  /* Destroy the task at the head of the ready to run list. */
 
-  sched_unlock();
+  (void)task_deletecurrent();
 
   /* Now, perform the context switch to the new ready-to-run task at the
    * head of the list.
diff --git a/arch/dm320/src/up_exit.c b/arch/dm320/src/up_exit.c
index bb05b1c533142c95a3281914262457d6f9245f31..a95b8855b4a11478e36faf8d6ee74157b21912a1 100644
--- a/arch/dm320/src/up_exit.c
+++ b/arch/dm320/src/up_exit.c
@@ -1,7 +1,7 @@
-/************************************************************
+/****************************************************************************
  * up_exit.c
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 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.
  *
@@ -31,11 +31,11 @@
  * 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 <sys/types.h>
@@ -49,28 +49,27 @@
 #include <nuttx/fs.h>
 #endif
 
-/************************************************************
+/****************************************************************************
  * Private Definitions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Data
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
- * Private Funtions
- ************************************************************/
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: _up_dumponexit
  *
  * Description:
- *   Dump the state of all tasks whenever on task exits.  This
- *   is debug instrumentation that was added to check file-
- *   related reference counting but could be useful again
- *   sometime in the future.
+ *   Dump the state of all tasks whenever on task exits.  This is debug
+ *   instrumentation that was added to check file-related reference counting
+ *   but could be useful again sometime in the future.
  *
- ************************************************************/
+ ****************************************************************************/
 
 #if defined(CONFIG_DUMP_ON_EXIT) && defined(CONFIG_DEBUG)
 static void _up_dumponexit(FAR _TCB *tcb, FAR void *arg)
@@ -112,22 +111,24 @@ static void _up_dumponexit(FAR _TCB *tcb, FAR void *arg)
 }
 #endif
 
-/************************************************************
- * Public Funtions
- ************************************************************/
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: _exit
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
- ************************************************************/
+ ****************************************************************************/
 
 void _exit(int status)
 {
-  _TCB* tcb = (_TCB*)g_readytorun.head;
+  _TCB* tcb;
 
   /* Disable interrupts.  They will be restored when the next
    * task is started.
@@ -138,41 +139,13 @@ void _exit(int status)
   lldbg("TCB=%p exitting\n", tcb);
 
 #if defined(CONFIG_DUMP_ON_EXIT) && defined(CONFIG_DEBUG)
-  dbg("Other tasks:\n");
+  lldbg("Other tasks:\n");
   sched_foreach(_up_dumponexit, NULL);
 #endif
 
-  /* Remove the tcb task from the ready-to-run list.  We can
-   * ignore the return value because we know that a context
-   * switch is needed.
-   */
-
-  (void)sched_removereadytorun(tcb);
-
-  /* We are not in a bad stack-- the head of the ready to run task list
-   * does not correspond to the thread that is running.  Disabling pre-
-   * emption on this TCB should be enough to keep things stable.
-   */
-
-  sched_lock();
-
-  /* Move the TCB to the specified blocked task list and delete it */
-
-  sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
-  task_delete(tcb->pid);
-
-  /* If there are any pending tasks, then add them to the g_readytorun
-   * task list now
-   */
-
-  if (g_pendingtasks.head)
-    {
-      (void)sched_mergepending();
-    }
-
-  /* Now calling sched_unlock() should have no effect */
+  /* Destroy the task at the head of the ready to run list. */
 
-  sched_unlock();
+  (void)task_deletecurrent();
 
   /* Now, perform the context switch to the new ready-to-run task at the
    * head of the list.
diff --git a/arch/pjrc-8051/src/up_exit.c b/arch/pjrc-8051/src/up_exit.c
index b938d930474ac5533c4dc052136a860e40c840ca..227225596c3ffb15e72515273d6b7d756e73eaef 100644
--- a/arch/pjrc-8051/src/up_exit.c
+++ b/arch/pjrc-8051/src/up_exit.c
@@ -1,7 +1,7 @@
-/************************************************************
+/****************************************************************************
  * up_exit.c
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 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.
  *
@@ -31,11 +31,11 @@
  * 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 <sys/types.h>
@@ -46,34 +46,36 @@
 #include "os_internal.h"
 #include "up_internal.h"
 
-/************************************************************
+/****************************************************************************
  * Private Definitions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Data
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
- * Private Funtions
- ************************************************************/
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
 
-/************************************************************
- * Public Funtions
- ************************************************************/
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: _exit
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
- ************************************************************/
+ ****************************************************************************/
 
 void _exit(int status)
 {
-  FAR _TCB* tcb = (FAR _TCB*)g_readytorun.head;
+  FAR _TCB* tcb;
 
   dbg("TCB=%p exitting\n", tcb);
 
@@ -83,37 +85,9 @@ void _exit(int status)
 
   EA = 0;
 
-  /* Remove the tcb task from the ready-to-run list.  We can
-   * ignore the return value because we know that a context
-   * switch is needed.
-   */
-
-  (void)sched_removereadytorun(tcb);
-
-  /* We are not in a bad stack-- the head of the ready to run task list
-   * does not correspond to the thread that is running.  Disabling pre-
-   * emption on this TCB should be enough to keep things stable.
-   */
-
-  sched_lock();
-
-  /* Move the TCB to the specified blocked task list and delete it */
-
-  sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
-  task_delete(tcb->pid);
-
-  /* If there are any pending tasks, then add them to the g_readytorun
-   * task list now
-   */
-
-  if (g_pendingtasks.head)
-    {
-      (void)sched_mergepending();
-    }
-
-  /* Now calling sched_unlock() should have no effect */
+  /* Destroy the task at the head of the ready to run list. */
 
-  sched_unlock();
+  (void)task_deletecurrent();
 
   /* Now, perform the context switch to the new ready-to-run task at the
    * head of the list.
diff --git a/arch/sim/src/up_exit.c b/arch/sim/src/up_exit.c
index 8ab8f92cf448f4c5587b5750ff0af73803fd3a42..5c2cd3dc4283e3e56a56c75188d8fb0a7572abfa 100644
--- a/arch/sim/src/up_exit.c
+++ b/arch/sim/src/up_exit.c
@@ -1,7 +1,7 @@
-/************************************************************
+/****************************************************************************
  * up_exit.c
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 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.
  *
@@ -31,11 +31,11 @@
  * 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 <sys/types.h>
@@ -45,68 +45,42 @@
 #include "os_internal.h"
 #include "up_internal.h"
 
-/************************************************************
+/****************************************************************************
  * Private Definitions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Data
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
- * Private Funtions
- ************************************************************/
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
 
-/************************************************************
- * Public Funtions
- ************************************************************/
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: _exit
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
- ************************************************************/
+ ****************************************************************************/
 
 void _exit(int status)
 {
-  _TCB* tcb = (_TCB*)g_readytorun.head;
+  _TCB* tcb;
 
   sdbg("TCB=%p exitting\n", tcb);
 
-  /* Remove the tcb task from the ready-to-run list.  We can
-   * ignore the return value because we know that a context
-   * switch is needed.
-   */
-
-  (void)sched_removereadytorun(tcb);
-
-  /* We are not in a bad stack-- the head of the ready to run task list
-   * does not correspond to the thread that is running.  Disabling pre-
-   * emption on this TCB should be enough to keep things stable.
-   */
-
-  sched_lock();
-
-  /* Move the TCB to the specified blocked task list and delete it */
-
-  sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
-  task_delete(tcb->pid);
-
-  /* If there are any pending tasks, then add them to the g_readytorun
-   * task list now.
-   */
-
-  if (g_pendingtasks.head)
-    {
-      (void)sched_mergepending();
-    }
-
-  /* Now calling sched_unlock() should have no effect */
+  /* Destroy the task at the head of the ready to run list. */
 
-  sched_unlock();
+  (void)task_deletecurrent();
 
   /* Now, perform the context switch to the new ready-to-run task at the
    * head of the list.
diff --git a/arch/z16/src/common/up_exit.c b/arch/z16/src/common/up_exit.c
index 52a9b148b2cea5bb4ae7b2b366ceb70449dfaa7e..b2e18e22bb3e87bd1db7511f8485ebca3f0c55e4 100644
--- a/arch/z16/src/common/up_exit.c
+++ b/arch/z16/src/common/up_exit.c
@@ -69,10 +69,9 @@
  * Name: _up_dumponexit
  *
  * Description:
- *   Dump the state of all tasks whenever on task exits.  This
- *   is debug instrumentation that was added to check file-
- *   related reference counting but could be useful again
- *   sometime in the future.
+ *   Dump the state of all tasks whenever on task exits.  This is debug
+ *   instrumentation that was added to check file-related reference counting
+ *   but could be useful again sometime in the future.
  *
  ****************************************************************************/
 
@@ -137,13 +136,15 @@ static void _up_dumponexit(FAR _TCB *tcb, FAR void *arg)
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
  ****************************************************************************/
 
 void _exit(int status)
 {
-  FAR _TCB* tcb = (FAR _TCB*)g_readytorun.head;
+  FAR _TCB* tcb;
 
   /* Disable interrupts.  Interrupts will remain disabled until
    * the new task is resumed below.
@@ -158,37 +159,9 @@ void _exit(int status)
   sched_foreach(_up_dumponexit, NULL);
 #endif
 
-  /* Remove the tcb task from the ready-to-run list.  We can
-   * ignore the return value because we know that a context
-   * switch is needed.
-   */
-
-  (void)sched_removereadytorun(tcb);
-
-  /* We are not in a bad stack-- the head of the ready to run task list
-   * does not correspond to the thread that is running.  Disabling pre-
-   * emption on this TCB should be enough to keep things stable.
-   */
-
-  sched_lock();
-
-  /* Move the TCB to the specified blocked task list and delete it */
-
-  sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
-  task_delete(tcb->pid);
-
-  /* If there are any pending tasks, then add them to the g_readytorun
-   * task list now
-   */
-
-  if (g_pendingtasks.head)
-    {
-      (void)sched_mergepending();
-    }
-
-  /* Now calling sched_unlock() should have no effect */
+  /* Destroy the task at the head of the ready to run list. */
 
-  sched_unlock();
+  (void)task_deletecurrent();
 
   /* Now, perform the context switch to the new ready-to-run task at the
    * head of the list.
diff --git a/arch/z80/src/common/up_exit.c b/arch/z80/src/common/up_exit.c
index b7f5e62433c5611e58f54ae1d0a50c4d0dc6c5a7..d8c94fece9bc38c521084fa03fbfd47604e4e75f 100644
--- a/arch/z80/src/common/up_exit.c
+++ b/arch/z80/src/common/up_exit.c
@@ -69,10 +69,9 @@
  * Name: _up_dumponexit
  *
  * Description:
- *   Dump the state of all tasks whenever on task exits.  This
- *   is debug instrumentation that was added to check file-
- *   related reference counting but could be useful again
- *   sometime in the future.
+ *   Dump the state of all tasks whenever on task exits.  This is debug
+ *   instrumentation that was added to check file-related reference counting
+ *   but could be useful again sometime in the future.
  *
  ****************************************************************************/
 
@@ -137,13 +136,15 @@ static void _up_dumponexit(FAR _TCB *tcb, FAR void *arg)
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
  ****************************************************************************/
 
 void _exit(int status)
 {
-  FAR _TCB* tcb = (FAR _TCB*)g_readytorun.head;
+  FAR _TCB* tcb;
 
   /* Disable interrupts.  Interrupts will remain disabled until
    * the new task is resumed below.
@@ -158,37 +159,9 @@ void _exit(int status)
   sched_foreach(_up_dumponexit, NULL);
 #endif
 
-  /* Remove the tcb task from the ready-to-run list.  We can
-   * ignore the return value because we know that a context
-   * switch is needed.
-   */
-
-  (void)sched_removereadytorun(tcb);
-
-  /* We are not in a bad stack-- the head of the ready to run task list
-   * does not correspond to the thread that is running.  Disabling pre-
-   * emption on this TCB should be enough to keep things stable.
-   */
-
-  sched_lock();
-
-  /* Move the TCB to the specified blocked task list and delete it */
-
-  sched_addblocked(tcb, TSTATE_TASK_INACTIVE);
-  task_delete(tcb->pid);
-
-  /* If there are any pending tasks, then add them to the g_readytorun
-   * task list now
-   */
-
-  if (g_pendingtasks.head)
-    {
-      (void)sched_mergepending();
-    }
-
-  /* Now calling sched_unlock() should have no effect */
+  /* Destroy the task at the head of the ready to run list. */
 
-  sched_unlock();
+  (void)task_deletecurrent();
 
   /* Now, perform the context switch to the new ready-to-run task at the
    * head of the list.
diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h
index 3eef7b15d7fde3514edabac1dc673c3f07576077..2c83f1f2af900c9ab73a2ad677081d5ed4fbfdf3 100644
--- a/include/nuttx/arch.h
+++ b/include/nuttx/arch.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * arch.h
+ * nuttx/arch.h
  *
  *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
@@ -53,12 +53,12 @@
  * Public Types
  ****************************************************************************/
 
+typedef CODE void (*sig_deliver_t)(FAR _TCB *tcb);
+
 /****************************************************************************
  * Public Variables
  ****************************************************************************/
 
-typedef CODE void (*sig_deliver_t)(FAR _TCB *tcb);
-
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -291,12 +291,13 @@ EXTERN void up_reprioritize_rtr(FAR _TCB *tcb, ubyte priority);
  *
  * Description:
  *   This function causes the currently executing task to cease
- *   to exist.  This is a special case of task_delete().
+ *   to exist.  This is a special case of task_delete() where the task to
+ *   be deleted is the currently executing task.  It is more complex because
+ *   a context switch must be perform to the the next ready to run task.
  *
- *   Unlike other UP APIs, this function may be called
- *   directly from user programs in various states.  The
- *   implementation of this function should diable interrupts
- *   before performing scheduling operations.
+ *   Unlike other UP APIs, this function may be called directly from user
+ *   programs in various states.  The implementation of this function should
+ *   disable interrupts before performing scheduling operations.
  *
  ****************************************************************************/
 /* Prototype is in unistd.h */
@@ -448,8 +449,7 @@ EXTERN void up_mdelay(unsigned int milliseconds);
 EXTERN void up_udelay(unsigned int microseconds);
 
 /****************************************************************************
- * Debug interfaces exported by the architecture-specific
- * logic
+ * Debug interfaces exported by the architecture-specific logic
  ****************************************************************************/
 
 /****************************************************************************
diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 7386979c88932fa92e529059187b925009d112ea..4548b33655ee443fdb06fa9894699007162606ea 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -1,5 +1,5 @@
 /********************************************************************************
- * sched.h
+ * nuttx/sched.h
  *
  *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
@@ -83,22 +83,22 @@
 
 enum tstate_e
 {
-  TSTATE_TASK_INVALID    = 0, /* INVALID - TCB has not yet been initialized */
-
+  TSTATE_TASK_INVALID    = 0, /* INVALID     - The TCB is is not in a valid state
+                               *               (Uninitialized or between context switches) */
   TSTATE_TASK_PENDING    = 1, /* READY_TO_RUN - Pending preemption unlock */
   TSTATE_TASK_READYTORUN = 2, /* READY-TO-RUN - But not running */
   TSTATE_TASK_RUNNING    = 3, /* READY_TO_RUN - And running */
 
-  TSTATE_TASK_INACTIVE   = 4, /* BLOCKED - Initialized but not yet activated */
-  TSTATE_WAIT_SEM        = 5  /* BLOCKED - Waiting for a semaphore */
+  TSTATE_TASK_INACTIVE   = 4, /* BLOCKED      - Initialized but not yet activated */
+  TSTATE_WAIT_SEM        = 5  /* BLOCKED      - Waiting for a semaphore */
 #ifndef CONFIG_DISABLE_SIGNALS
   ,
-  TSTATE_WAIT_SIG        = 6  /* BLOCKED - Waiting for a signal */
+  TSTATE_WAIT_SIG        = 6  /* BLOCKED      - Waiting for a signal */
 #endif
 #ifndef CONFIG_DISABLE_MQUEUE
   ,
-  TSTATE_WAIT_MQNOTEMPTY,     /* BLOCKED - Waiting for a MQ to become not empty. */
-  TSTATE_WAIT_MQNOTFULL       /* BLOCKED - Waiting for a MQ to become not full. */
+  TSTATE_WAIT_MQNOTEMPTY,     /* BLOCKED      - Waiting for a MQ to become not empty. */
+  TSTATE_WAIT_MQNOTFULL       /* BLOCKED      - Waiting for a MQ to become not full. */
 #endif
 };
 typedef enum tstate_e tstate_t;
diff --git a/sched/Makefile b/sched/Makefile
index f2ba461dfca9c54e19de629e073842cd43952032..ad5286f01689734c72282064d1611d7300f99889 100644
--- a/sched/Makefile
+++ b/sched/Makefile
@@ -43,7 +43,7 @@ MISC_SRCS	= os_start.c get_errno_ptr.c \
 		  sched_setupidlefiles.c sched_setuptaskfiles.c sched_setuppthreadfiles.c \
 		  sched_releasefiles.c
 TSK_SRCS	= task_create.c task_init.c task_setup.c task_activate.c \
-		  task_start.c task_delete.c task_restart.c \
+		  task_start.c task_delete.c task_deletecurrent.c task_restart.c \
 		  exit.c abort.c atexit.c getpid.c  \
 		  sched_addreadytorun.c sched_removereadytorun.c sched_addprioritized.c \
 		  sched_mergepending.c sched_addblocked.c sched_removeblocked.c \
diff --git a/sched/get_errno_ptr.c b/sched/get_errno_ptr.c
index 82eacb762747d3aeef3b8bc83b9c87aab6b59f24..36598caae9c0f2ca60bc10e3d4de9cf35f7f43d3 100644
--- a/sched/get_errno_ptr.c
+++ b/sched/get_errno_ptr.c
@@ -1,7 +1,7 @@
-/************************************************************
+/****************************************************************************
  * get_errno_ptr.c
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 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.
  *
@@ -31,11 +31,11 @@
  * 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 <sched.h>
@@ -45,17 +45,17 @@
 
 #undef get_errno_ptr
 
-/************************************************************
+/****************************************************************************
  * Private Data
- ************************************************************/
+ ****************************************************************************/
 
 static int g_irqerrno;
 
-/************************************************************
+/****************************************************************************
  * Public Functions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Function:  get_errno_ptr
  *
  * Description:
@@ -69,36 +69,44 @@ static int g_irqerrno;
  *
  * Assumptions:
  *
- ************************************************************/
+ ****************************************************************************/
 
 FAR int *get_errno_ptr(void)
 {
-  /* Check if this function was called from an interrupt
-   * handler.  In that case, we have to do things a little
-   * differently.
+  /* Check if this function was called from an interrupt handler.  In that
+   * case, we have to do things a little differently to prevent the interrupt
+   * handler from modifying the tasks errno value.
    */
 
-  if (up_interrupt_context())
+  if (!up_interrupt_context())
     {
-      /* Yes, we were called from an interrupt handler.  Do
-       * not permit access to the errno in the TCB of the
-       * interrupt task.  Instead, use a separate errno just
-       * for interrupt handlers.  Of course, this would have
-       * to change if we ever wanted to support nested
-       * interrupts.
+      /* We were called from the normal tasking context.  Verify that the
+       * task at the head of the ready-to-run list is actually running.  It
+       * may not be running during very brief times during context switching
+       * logic (see, for example, task_deletecurrent.c).
        */
 
-      return &g_irqerrno;
-    }
-  else
-    {
-      /* We were called from the normal tasking context.  Return
-       * a reference to the thread-private errno in the TCB.
-       */
+      FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head;
+      if (rtcb->task_state == TSTATE_TASK_RUNNING)
+        {
+          /* Yes.. the task is running normally.  Return a reference to the
+           * thread-private errno in the TCB of the running task.
+           */
 
-      FAR _TCB *ptcb = (FAR _TCB*)g_readytorun.head;
-      return &ptcb->errno;
+          return &rtcb->errno;
+        }
     }
+
+  /* We were called either from (1) an interrupt handler or (2) from normally
+   * code but in an unhealthy state.  In either event, do not permit access to
+   * the errno in the TCB of the task at the head of the ready-to-run list.
+   * Instead, use a separate errno just for interrupt handlers.  Of course, this
+   * would have to change if we ever wanted to support nested interrupts or if
+   * we really cared about the stability of the errno during those "unhealthy
+   * states."
+   */
+
+  return &g_irqerrno;
 }
 
 
diff --git a/sched/os_internal.h b/sched/os_internal.h
index f1ec07b56e87b058eeb2b390021863115ce49aad..e3dffb207484bb89be886dcdcad71fc5a09deb14 100644
--- a/sched/os_internal.h
+++ b/sched/os_internal.h
@@ -1,7 +1,7 @@
-/************************************************************
- * os_internal.h
+/****************************************************************************
+ * sched/os_internal.h
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 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.
  *
@@ -31,22 +31,22 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- ************************************************************/
+ ****************************************************************************/
 
 #ifndef __OS_INTERNAL_H
 #define __OS_INTERNAL_H
 
-/************************************************************
+/****************************************************************************
  * Included Files
- ************************************************************/
+ ****************************************************************************/
 
 #include <queue.h>
 #include <sched.h>
 #include <nuttx/kmalloc.h>
 
-/************************************************************
+/****************************************************************************
  * Definitions
- ************************************************************/
+ ****************************************************************************/
 
 /* OS CRASH CODES */
 
@@ -110,9 +110,9 @@ enum os_crash_codes_e
 #define _SET_TCB_ERRNO(t,e) \
   { (t)->errno = (e); }
 
-/************************************************************
+/****************************************************************************
  * Public Type Definitions
- ************************************************************/
+ ****************************************************************************/
 
 /* This structure defines the format of the hash table that
  * is used to (1) determine if a task ID is unique, and (2)
@@ -138,11 +138,11 @@ struct tasklist_s
 };
 typedef struct tasklist_s tasklist_t;
 
-/************************************************************
+/****************************************************************************
  * Global Variables
- ************************************************************/
+ ****************************************************************************/
 
-/* Declared in os_start.c ***********************************/
+/* Declared in os_start.c ***************************************************/
 
 /* The state of a task is indicated both by the task_state field
  * of the TCB and by a series of task lists.  All of these
@@ -234,15 +234,16 @@ extern pidhash_t g_pidhash[CONFIG_MAX_TASKS];
 
 extern const tasklist_t g_tasklisttable[NUM_TASK_STATES];
 
-/************************************************************
+/****************************************************************************
  * Public Function Prototypes
- ************************************************************/
+ ****************************************************************************/
 
 extern void    task_start(void);
 extern STATUS  task_schedsetup(FAR _TCB *tcb, int priority,
                                start_t start, main_t main);
 extern STATUS  task_argsetup(FAR _TCB *tcb, const char *name,
                              const char *argv[]);
+extern STATUS  task_deletecurrent(void);
 
 extern boolean sched_addreadytorun(FAR _TCB *rtrtcb);
 extern boolean sched_removereadytorun(FAR _TCB *rtrtcb);
diff --git a/sched/task_delete.c b/sched/task_delete.c
index 691b7c57e7d3519790291049363dc5083f6347ab..369f601ed0ae73b201270f3ee07057e36b46613f 100644
--- a/sched/task_delete.c
+++ b/sched/task_delete.c
@@ -1,7 +1,7 @@
-/************************************************************
- * task_delete.c
+/****************************************************************************
+ * sched/task_delete.c
  *
- *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,7 +14,7 @@
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
- * 3. Neither the name Gregory Nutt nor the names of its contributors may be
+ * 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.
  *
@@ -31,11 +31,11 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Included Files
- ************************************************************/
+ ****************************************************************************/
 
 #include  <nuttx/config.h>
 
@@ -46,41 +46,40 @@
 # include "sig_internal.h"
 #endif
 
-/************************************************************
+/****************************************************************************
  * Definitions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Type Declarations
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Global Variables
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Variables
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Function Prototypes
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Private Functions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Public Functions
- ************************************************************/
+ ****************************************************************************/
 
-/************************************************************
+/****************************************************************************
  * Name: task_delete
  *
  * Description:
- *   This function causes a specified task to cease to exist.
- *   Its  stack and TCB will be deallocated.  This function
- *   is the companion to task_create().
+ *   This function causes a specified task to cease to exist. Its  stack and
+ *   TCB will be deallocated.  This function is the companion to task_create().
  *
  * Inputs:
  *   pid - The task ID of the task to delete.  A pid of zero
@@ -89,10 +88,10 @@
  * Return Value:
  *   OK on success; or ERROR on failure
  *
- *   This function can fail if the provided pid does not
- *   correspond to a task (errno is not set)
+ *   This function can fail if the provided pid does not correspond to a
+ *   task (errno is not set)
  *
- ************************************************************/
+ ****************************************************************************/
 
 STATUS task_delete(pid_t pid)
 {
@@ -106,17 +105,15 @@ STATUS task_delete(pid_t pid)
    rtcb = (FAR _TCB*)g_readytorun.head;
    if (pid == 0 || pid == rtcb->pid)
      {
-       /* If it is, then what we really wanted to do was exit.
-        * Note that we don't bother to unlock the TCB since
-        * it will be going away.
+       /* If it is, then what we really wanted to do was exit. Note that we
+        * don't bother to unlock the TCB since it will be going away.
         */
 
        exit(EXIT_SUCCESS);
      }
 
-  /* Make sure the task does not become ready-to-run while
-   * we are futzing with its TCB by locking ourselves as the
-   * executing task.
+  /* Make sure the task does not become ready-to-run while we are futzing with
+   * its TCB by locking ourselves as the executing task.
    */
 
   sched_lock();
diff --git a/sched/task_deletecurrent.c b/sched/task_deletecurrent.c
new file mode 100644
index 0000000000000000000000000000000000000000..8246bb4713542a59e12a3d30c91ee456b7bf2304
--- /dev/null
+++ b/sched/task_deletecurrent.c
@@ -0,0 +1,141 @@
+/****************************************************************************
+ * sched/task_deletecurrent.c
+ *
+ *   Copyright (C) 2008 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * 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  <sys/types.h>
+#include  <sched.h>
+#include  "os_internal.h"
+#ifndef CONFIG_DISABLE_SIGNALS
+# include "sig_internal.h"
+#endif
+
+/****************************************************************************
+ * Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Type Declarations
+ ****************************************************************************/
+
+/****************************************************************************
+ * Global Variables
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Variables
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: task_delete
+ *
+ * Description:
+ *   This function causes the currently running task (i.e., the task at the
+ *   head of the ready-to-run list) to cease to exist.  This is a part of
+ *   the logic used to implement _exit().  The full implementation of _exit()
+ *   is architecture-dependent.  This function should never be called from
+ *   normal user code, but only from the architecture-specific implementation
+ *   of exit.
+ *
+ * Inputs:
+ *   None
+ *
+ * Return Value:
+ *   OK on success; or ERROR on failure
+ *
+ ****************************************************************************/
+
+STATUS task_deletecurrent(void)
+{
+  FAR _TCB  *dtcb = (FAR _TCB*)g_readytorun.head;
+  FAR _TCB  *rtcb;
+
+  /* Remove the TCB of the current task from the ready-to-run list.  A context
+   * switch will definitely be necessary -- that must be done by the
+   * architecture-specific logic.
+   *
+   * sched_removereadytorun will mark the task at the head of the ready-to-run
+   * with state == TSTATE_TASK_RUNNING
+   */
+
+  (void)sched_removereadytorun(dtcb);
+  rtcb = (FAR _TCB*)g_readytorun.head;
+
+  /* We are not in a bad state -- the head of the ready to run task list
+   * does not correspond to the thread that is running.  Disabling pre-
+   * emption on this TCB and marking the new ready-to-run task as not
+   * running (see, for example, get_errno_ptr()).
+   */
+
+  sched_lock();
+  rtcb->task_state = TSTATE_TASK_READYTORUN;
+
+  /* Move the TCB to the specified blocked task list and delete it */
+
+  sched_addblocked(dtcb, TSTATE_TASK_INACTIVE);
+  task_delete(dtcb->pid);
+  rtcb->task_state = TSTATE_TASK_RUNNING;
+
+  /* If there are any pending tasks, then add them to the ready-to-run
+   * task list now
+   */
+
+  if (g_pendingtasks.head)
+    {
+      (void)sched_mergepending();
+    }
+
+  /* Now calling sched_unlock() should have no effect */
+
+  sched_unlock();
+  return OK;
+}
+