2008-01-30 19:49:31 +01:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_exit.c
|
2008-01-30 19:49:31 +01:00
|
|
|
*
|
2020-05-16 18:05:46 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2008-01-30 19:49:31 +01:00
|
|
|
*
|
2020-05-16 18:05:46 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-01-30 19:49:31 +01:00
|
|
|
*
|
2020-05-16 18:05:46 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-01-30 19:49:31 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-12-14 15:22:07 +01:00
|
|
|
#include <nuttx/config.h>
|
2008-01-30 19:49:31 +01:00
|
|
|
|
2023-12-14 15:22:07 +01:00
|
|
|
#include <sched.h>
|
2024-01-14 09:43:11 +01:00
|
|
|
#include <debug.h>
|
2014-08-09 00:44:08 +02:00
|
|
|
|
2023-12-14 15:22:07 +01:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
|
sched: task: Fix nxtask_exit() for SMP
Summary:
- During Wi-Fi audio streaming test, I found a deadlock in nxtask_exit()
- Actually, nxtask_exit() was called and tried to enter critical section
- In enter_critical_section(), there is a deadlock avoidance logic
- However, if switched to a new rtcb with irqcount=0, the logic did not work
- Because the 2nd critical section was treated as if it were the 1st one
- Actually, it tried to run the deadlock avoidance logic
- But nxtask_exit() was called with critical section (i.e. IRQ already disabled)
- So the logic did not work as expected because up_irq_restore() did not enable the IRQ.
- This commit fixes this issue by incrementing irqcount before calling nxtask_terminate()
- Also it adjusts g_cpu_irqlock and g_cpu_lockset
Impact:
- Affects SMP only
Testing:
- Tested with spresense:wifi_smp (smp, ostest, nxplayer, telnetd)
- Tested with sabre-6quad:smp with QEMU (smp, ostest)
- Tested with maix-bit:smp with QEMU (smp, ostest)
- Tested with esp32-core:smp with QEMU (smp, ostest)
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-10-08 07:39:22 +02:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
# include "irq/irq.h"
|
|
|
|
#endif
|
|
|
|
|
2019-04-29 22:52:05 +02:00
|
|
|
#include "signal/signal.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2008-01-30 19:49:31 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
* Name: nxtask_exit
|
2008-01-30 19:49:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-04-19 00:15:20 +02:00
|
|
|
* This is a part of the logic used to implement _exit(). The full
|
|
|
|
* implementation of _exit() is architecture-dependent. The _exit()
|
|
|
|
* function also implements the bottom half of exit() and pthread_exit().
|
|
|
|
*
|
2008-01-30 19:49:31 +01:00
|
|
|
* This function causes the currently running task (i.e., the task at the
|
2013-04-19 00:15:20 +02:00
|
|
|
* head of the ready-to-run list) to cease to exist. This function should
|
|
|
|
* never be called from normal user code, but only from the architecture-
|
|
|
|
* specific implementation of exit.
|
|
|
|
*
|
2020-03-08 13:51:34 +01:00
|
|
|
* Threads/tasks could also be terminated via pthread_cancel,
|
|
|
|
* task_delete(), and task_restart(). In the last two cases, the
|
|
|
|
* task will be terminated as though exit() were called.
|
2008-01-30 19:49:31 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2008-01-30 19:49:31 +01:00
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2008-01-30 19:49:31 +01:00
|
|
|
* OK on success; or ERROR on failure
|
|
|
|
*
|
2018-11-08 14:03:30 +01:00
|
|
|
* Assumptions:
|
2017-02-24 17:07:23 +01:00
|
|
|
* Executing within a critical section established by the caller.
|
2013-01-12 20:58:45 +01:00
|
|
|
*
|
2008-01-30 19:49:31 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
int nxtask_exit(void)
|
2008-01-30 19:49:31 +01:00
|
|
|
{
|
2018-02-07 02:06:33 +01:00
|
|
|
FAR struct tcb_s *dtcb;
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *rtcb;
|
2014-02-18 18:50:32 +01:00
|
|
|
int ret;
|
2018-02-07 02:06:33 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
/* Get the current CPU. By assumption, we are within a critical section
|
|
|
|
* and, hence, the CPU index will remain stable.
|
|
|
|
*
|
|
|
|
* Avoid using this_task() because it may assume a state that is not
|
|
|
|
* appropriate for an exiting task.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cpu = this_cpu();
|
|
|
|
dtcb = current_task(cpu);
|
|
|
|
#else
|
|
|
|
dtcb = this_task();
|
|
|
|
#endif
|
2008-01-30 19:49:31 +01:00
|
|
|
|
2024-01-14 09:43:11 +01:00
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
sinfo("%s pid=%d,TCB=%p\n", dtcb->name,
|
|
|
|
#else
|
|
|
|
sinfo("pid=%d,TCB=%p\n",
|
|
|
|
#endif
|
|
|
|
dtcb->pid, dtcb);
|
|
|
|
|
2020-06-04 07:59:09 +02:00
|
|
|
/* Update scheduler parameters */
|
|
|
|
|
|
|
|
nxsched_suspend_scheduler(dtcb);
|
|
|
|
|
2020-03-08 13:51:34 +01:00
|
|
|
/* 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.
|
2008-01-30 19:49:31 +01:00
|
|
|
*
|
2020-05-09 20:40:14 +02:00
|
|
|
* nxsched_remove_readytorun will mark the task at the head of the
|
2020-03-08 13:51:34 +01:00
|
|
|
* ready-to-run with state == TSTATE_TASK_RUNNING
|
2008-01-30 19:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2022-11-13 13:39:14 +01:00
|
|
|
nxsched_remove_readytorun(dtcb, true);
|
2020-11-09 08:31:57 +01:00
|
|
|
|
2018-02-07 02:06:33 +01:00
|
|
|
/* Get the new task at the head of the ready to run list */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
rtcb = current_task(cpu);
|
|
|
|
#else
|
2016-02-07 00:44:41 +01:00
|
|
|
rtcb = this_task();
|
2018-02-07 02:06:33 +01:00
|
|
|
#endif
|
2008-01-30 19:49:31 +01:00
|
|
|
|
2020-12-22 00:49:19 +01:00
|
|
|
/* NOTE: nxsched_resume_scheduler() was moved to up_exit()
|
|
|
|
* because the global IRQ control for SMP should be deferred until
|
|
|
|
* context switching, otherwise, the context switching would be done
|
|
|
|
* without a critical section
|
2018-01-31 03:23:22 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-12 20:58:45 +01:00
|
|
|
/* We are now in a bad state -- the head of the ready to run task list
|
2008-01-30 19:49:31 +01:00
|
|
|
* 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
|
2020-05-03 14:02:48 +02:00
|
|
|
* running.
|
2013-01-13 01:35:47 +01:00
|
|
|
*
|
|
|
|
* We disable pre-emption here by directly incrementing the lockcount
|
|
|
|
* (vs. calling sched_lock()).
|
2008-01-30 19:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-13 01:35:47 +01:00
|
|
|
rtcb->lockcount++;
|
2016-02-12 21:55:31 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* Make sure that the system knows about the locked state */
|
|
|
|
|
2016-02-17 20:20:01 +01:00
|
|
|
spin_setbit(&g_cpu_lockset, this_cpu(), &g_cpu_locksetlock,
|
|
|
|
&g_cpu_schedlock);
|
2016-02-12 21:55:31 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
rtcb->task_state = TSTATE_TASK_READYTORUN;
|
|
|
|
|
2013-04-24 00:41:43 +02:00
|
|
|
/* Move the TCB to the specified blocked task list and delete it. Calling
|
2020-03-08 13:51:34 +01:00
|
|
|
* nxtask_terminate with non-blocking true will suppress atexit() and
|
|
|
|
* on-exit() calls and will cause buffered I/O to fail to be flushed. The
|
|
|
|
* former is required _exit() behavior; the latter is optional _exit()
|
|
|
|
* behavior.
|
2013-04-24 00:41:43 +02:00
|
|
|
*/
|
2008-01-30 19:49:31 +01:00
|
|
|
|
2020-05-09 20:40:14 +02:00
|
|
|
nxsched_add_blocked(dtcb, TSTATE_TASK_INACTIVE);
|
sched: task: Fix nxtask_exit() for SMP
Summary:
- During Wi-Fi audio streaming test, I found a deadlock in nxtask_exit()
- Actually, nxtask_exit() was called and tried to enter critical section
- In enter_critical_section(), there is a deadlock avoidance logic
- However, if switched to a new rtcb with irqcount=0, the logic did not work
- Because the 2nd critical section was treated as if it were the 1st one
- Actually, it tried to run the deadlock avoidance logic
- But nxtask_exit() was called with critical section (i.e. IRQ already disabled)
- So the logic did not work as expected because up_irq_restore() did not enable the IRQ.
- This commit fixes this issue by incrementing irqcount before calling nxtask_terminate()
- Also it adjusts g_cpu_irqlock and g_cpu_lockset
Impact:
- Affects SMP only
Testing:
- Tested with spresense:wifi_smp (smp, ostest, nxplayer, telnetd)
- Tested with sabre-6quad:smp with QEMU (smp, ostest)
- Tested with maix-bit:smp with QEMU (smp, ostest)
- Tested with esp32-core:smp with QEMU (smp, ostest)
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-10-08 07:39:22 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* NOTE:
|
|
|
|
* During nxtask_terminate(), enter_critical_section() will be called
|
2021-02-25 13:48:46 +01:00
|
|
|
* to deallocate tcb. However, this would acquire g_cpu_irqlock if
|
sched: task: Fix nxtask_exit() for SMP
Summary:
- During Wi-Fi audio streaming test, I found a deadlock in nxtask_exit()
- Actually, nxtask_exit() was called and tried to enter critical section
- In enter_critical_section(), there is a deadlock avoidance logic
- However, if switched to a new rtcb with irqcount=0, the logic did not work
- Because the 2nd critical section was treated as if it were the 1st one
- Actually, it tried to run the deadlock avoidance logic
- But nxtask_exit() was called with critical section (i.e. IRQ already disabled)
- So the logic did not work as expected because up_irq_restore() did not enable the IRQ.
- This commit fixes this issue by incrementing irqcount before calling nxtask_terminate()
- Also it adjusts g_cpu_irqlock and g_cpu_lockset
Impact:
- Affects SMP only
Testing:
- Tested with spresense:wifi_smp (smp, ostest, nxplayer, telnetd)
- Tested with sabre-6quad:smp with QEMU (smp, ostest)
- Tested with maix-bit:smp with QEMU (smp, ostest)
- Tested with esp32-core:smp with QEMU (smp, ostest)
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-10-08 07:39:22 +02:00
|
|
|
* rtcb->irqcount = 0, event though we are in critical section.
|
2021-02-25 13:48:46 +01:00
|
|
|
* To prevent from acquiring, increment rtcb->irqcount here.
|
sched: task: Fix nxtask_exit() for SMP
Summary:
- During Wi-Fi audio streaming test, I found a deadlock in nxtask_exit()
- Actually, nxtask_exit() was called and tried to enter critical section
- In enter_critical_section(), there is a deadlock avoidance logic
- However, if switched to a new rtcb with irqcount=0, the logic did not work
- Because the 2nd critical section was treated as if it were the 1st one
- Actually, it tried to run the deadlock avoidance logic
- But nxtask_exit() was called with critical section (i.e. IRQ already disabled)
- So the logic did not work as expected because up_irq_restore() did not enable the IRQ.
- This commit fixes this issue by incrementing irqcount before calling nxtask_terminate()
- Also it adjusts g_cpu_irqlock and g_cpu_lockset
Impact:
- Affects SMP only
Testing:
- Tested with spresense:wifi_smp (smp, ostest, nxplayer, telnetd)
- Tested with sabre-6quad:smp with QEMU (smp, ostest)
- Tested with maix-bit:smp with QEMU (smp, ostest)
- Tested with esp32-core:smp with QEMU (smp, ostest)
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-10-08 07:39:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
rtcb->irqcount++;
|
|
|
|
#endif
|
|
|
|
|
2022-12-14 08:37:22 +01:00
|
|
|
ret = nxtask_terminate(dtcb->pid);
|
sched: task: Fix nxtask_exit() for SMP
Summary:
- During Wi-Fi audio streaming test, I found a deadlock in nxtask_exit()
- Actually, nxtask_exit() was called and tried to enter critical section
- In enter_critical_section(), there is a deadlock avoidance logic
- However, if switched to a new rtcb with irqcount=0, the logic did not work
- Because the 2nd critical section was treated as if it were the 1st one
- Actually, it tried to run the deadlock avoidance logic
- But nxtask_exit() was called with critical section (i.e. IRQ already disabled)
- So the logic did not work as expected because up_irq_restore() did not enable the IRQ.
- This commit fixes this issue by incrementing irqcount before calling nxtask_terminate()
- Also it adjusts g_cpu_irqlock and g_cpu_lockset
Impact:
- Affects SMP only
Testing:
- Tested with spresense:wifi_smp (smp, ostest, nxplayer, telnetd)
- Tested with sabre-6quad:smp with QEMU (smp, ostest)
- Tested with maix-bit:smp with QEMU (smp, ostest)
- Tested with esp32-core:smp with QEMU (smp, ostest)
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-10-08 07:39:22 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
rtcb->irqcount--;
|
|
|
|
#endif
|
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
rtcb->task_state = TSTATE_TASK_RUNNING;
|
|
|
|
|
2016-03-23 01:19:57 +01:00
|
|
|
/* Decrement the lockcount on rctb. */
|
2008-01-30 19:49:31 +01:00
|
|
|
|
2013-01-12 20:58:45 +01:00
|
|
|
rtcb->lockcount--;
|
2016-02-12 21:55:31 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
if (rtcb->lockcount == 0)
|
|
|
|
{
|
|
|
|
/* Make sure that the system knows about the unlocked state */
|
|
|
|
|
2016-02-17 20:20:01 +01:00
|
|
|
spin_clrbit(&g_cpu_lockset, this_cpu(), &g_cpu_locksetlock,
|
|
|
|
&g_cpu_schedlock);
|
2016-02-12 21:55:31 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-18 18:50:32 +01:00
|
|
|
return ret;
|
2008-01-30 19:49:31 +01:00
|
|
|
}
|