diff --git a/ChangeLog b/ChangeLog
index c812bf7738..ad31d36836 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 2c7d6531a9..0fdeffaee2 100644
--- a/Documentation/NuttX.html
+++ b/Documentation/NuttX.html
@@ -8,7 +8,7 @@
NuttX RTOS
- Last Updated: January 9, 2008
+ Last Updated: January 30, 2008
|
@@ -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 "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/arch/arm/src/common/up_exit.c b/arch/arm/src/common/up_exit.c
index e760b00cf6..2660ed80bb 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.
- */
+ /* Destroy the task at the head of the ready to run list. */
- (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 */
-
- 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 2ef47bdfbc..4d117fce4b 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
*
* 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
#include
@@ -49,28 +49,27 @@
#include
#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.
- */
+ /* Destroy the task at the head of the ready to run list. */
- (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 */
-
- 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 bb05b1c533..a95b8855b4 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
*
* 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
#include
@@ -49,28 +49,27 @@
#include
#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.
- */
+ /* Destroy the task at the head of the ready to run list. */
- (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 */
-
- 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 b938d93047..227225596c 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
*
* 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
#include
@@ -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.
- */
+ /* Destroy the task at the head of the ready to run list. */
- (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 */
-
- 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 8ab8f92cf4..5c2cd3dc42 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
*
* 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
#include
@@ -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.
- */
+ /* Destroy the task at the head of the ready to run list. */
- (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 */
-
- 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 52a9b148b2..b2e18e22bb 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.
- */
+ /* Destroy the task at the head of the ready to run list. */
- (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 */
-
- 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 b7f5e62433..d8c94fece9 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.
- */
+ /* Destroy the task at the head of the ready to run list. */
- (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 */
-
- 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 3eef7b15d7..2c83f1f2af 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
@@ -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 7386979c88..4548b33655 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
@@ -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 f2ba461dfc..ad5286f016 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 82eacb7627..36598caae9 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
*
* 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
#include
@@ -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 f1ec07b56e..e3dffb2074 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
*
* 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
#include
#include
-/************************************************************
+/****************************************************************************
* 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 691b7c57e7..369f601ed0 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
*
* 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
@@ -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 0000000000..8246bb4713
--- /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
+ *
+ * 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
+
+#include
+#include
+#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;
+}
+