diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-01-30 18:49:31 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-01-30 18:49:31 +0000 |
commit | 013eef130f1bc2cab1e3f4022d8498fb98bb29bf (patch) | |
tree | a6455ca9fb6a1aaab3f305b1bf5e937bff704f59 /nuttx/sched | |
parent | 802ee953dd39863a9b296d8ffbfcc428f2d6fc4f (diff) | |
download | px4-nuttx-013eef130f1bc2cab1e3f4022d8498fb98bb29bf.tar.gz px4-nuttx-013eef130f1bc2cab1e3f4022d8498fb98bb29bf.tar.bz2 px4-nuttx-013eef130f1bc2cab1e3f4022d8498fb98bb29bf.zip |
errno was clobbered by mm_trysemaphore when task exists
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@591 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/sched')
-rw-r--r-- | nuttx/sched/Makefile | 2 | ||||
-rw-r--r-- | nuttx/sched/get_errno_ptr.c | 70 | ||||
-rw-r--r-- | nuttx/sched/os_internal.h | 33 | ||||
-rw-r--r-- | nuttx/sched/task_delete.c | 65 | ||||
-rw-r--r-- | nuttx/sched/task_deletecurrent.c | 141 |
5 files changed, 229 insertions, 82 deletions
diff --git a/nuttx/sched/Makefile b/nuttx/sched/Makefile index f2ba461df..ad5286f01 100644 --- a/nuttx/sched/Makefile +++ b/nuttx/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/nuttx/sched/get_errno_ptr.c b/nuttx/sched/get_errno_ptr.c index 82eacb762..36598caae 100644 --- a/nuttx/sched/get_errno_ptr.c +++ b/nuttx/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/nuttx/sched/os_internal.h b/nuttx/sched/os_internal.h index f1ec07b56..e3dffb207 100644 --- a/nuttx/sched/os_internal.h +++ b/nuttx/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/nuttx/sched/task_delete.c b/nuttx/sched/task_delete.c index 691b7c57e..369f601ed 100644 --- a/nuttx/sched/task_delete.c +++ b/nuttx/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/nuttx/sched/task_deletecurrent.c b/nuttx/sched/task_deletecurrent.c new file mode 100644 index 000000000..8246bb471 --- /dev/null +++ b/nuttx/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; +} + |