2008-01-31 02:49:31 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 06:44:08 +08:00
|
|
|
* sched/task/task_exit.c
|
2008-01-31 02:49:31 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-05-17 00:05:46 +08: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-31 02:49:31 +08:00
|
|
|
*
|
2020-05-17 00:05:46 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-01-31 02:49:31 +08:00
|
|
|
*
|
2020-05-17 00:05:46 +08: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-31 02:49:31 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-12-14 22:22:07 +08:00
|
|
|
#include <nuttx/config.h>
|
2008-01-31 02:49:31 +08:00
|
|
|
|
2023-12-14 22:22:07 +08:00
|
|
|
#include <sched.h>
|
2024-01-14 16:43:11 +08:00
|
|
|
#include <debug.h>
|
2014-08-09 06:44:08 +08:00
|
|
|
|
2024-10-23 21:13:22 +08:00
|
|
|
#include <nuttx/sched_note.h>
|
|
|
|
|
2023-12-14 22:22:07 +08:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 06:44:08 +08: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 13:39:22 +08:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
# include "irq/irq.h"
|
|
|
|
#endif
|
|
|
|
|
2019-04-30 04:52:05 +08:00
|
|
|
#include "signal/signal.h"
|
2014-08-09 06:44:08 +08:00
|
|
|
#include "task/task.h"
|
2008-01-31 02:49:31 +08: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-05 03:42:51 +08:00
|
|
|
* Name: nxtask_exit
|
2008-01-31 02:49:31 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2013-04-19 06:15:20 +08: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-31 02:49:31 +08:00
|
|
|
* This function causes the currently running task (i.e., the task at the
|
2013-04-19 06:15:20 +08: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 20:51:34 +08: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-31 02:49:31 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Input Parameters:
|
2008-01-31 02:49:31 +08:00
|
|
|
* None
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2008-01-31 02:49:31 +08:00
|
|
|
* OK on success; or ERROR on failure
|
|
|
|
*
|
2018-11-08 21:03:30 +08:00
|
|
|
* Assumptions:
|
2017-02-25 00:07:23 +08:00
|
|
|
* Executing within a critical section established by the caller.
|
2013-01-13 03:58:45 +08:00
|
|
|
*
|
2008-01-31 02:49:31 +08: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-05 03:42:51 +08:00
|
|
|
int nxtask_exit(void)
|
2008-01-31 02:49:31 +08:00
|
|
|
{
|
2018-02-07 09:06:33 +08:00
|
|
|
FAR struct tcb_s *dtcb;
|
2013-02-05 02:46:28 +08:00
|
|
|
FAR struct tcb_s *rtcb;
|
2014-02-19 01:50:32 +08:00
|
|
|
int ret;
|
2018-02-07 09:06:33 +08:00
|
|
|
#ifdef CONFIG_SMP
|
2024-10-22 21:06:38 +08:00
|
|
|
/* Avoid using this_task() because it may assume a state that is not
|
2018-02-07 09:06:33 +08:00
|
|
|
* appropriate for an exiting task.
|
|
|
|
*/
|
|
|
|
|
2024-10-22 21:06:38 +08:00
|
|
|
dtcb = current_task(this_cpu());
|
2018-02-07 09:06:33 +08:00
|
|
|
#else
|
|
|
|
dtcb = this_task();
|
|
|
|
#endif
|
2008-01-31 02:49:31 +08:00
|
|
|
|
2024-08-28 16:28:46 +08:00
|
|
|
sinfo("%s pid=%d,TCB=%p\n", get_task_name(dtcb),
|
2024-01-14 16:43:11 +08:00
|
|
|
dtcb->pid, dtcb);
|
|
|
|
|
2020-06-04 13:59:09 +08:00
|
|
|
/* Update scheduler parameters */
|
|
|
|
|
|
|
|
nxsched_suspend_scheduler(dtcb);
|
|
|
|
|
2020-03-08 20:51:34 +08: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-31 02:49:31 +08:00
|
|
|
*
|
2020-05-10 02:40:14 +08:00
|
|
|
* nxsched_remove_readytorun will mark the task at the head of the
|
2020-03-08 20:51:34 +08:00
|
|
|
* ready-to-run with state == TSTATE_TASK_RUNNING
|
2008-01-31 02:49:31 +08:00
|
|
|
*/
|
|
|
|
|
2024-01-19 20:57:04 +08:00
|
|
|
nxsched_remove_self(dtcb);
|
2020-11-09 15:31:57 +08:00
|
|
|
|
2018-02-07 09:06:33 +08:00
|
|
|
/* Get the new task at the head of the ready to run list */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
2024-10-22 21:06:38 +08:00
|
|
|
rtcb = current_task(this_cpu());
|
2018-02-07 09:06:33 +08:00
|
|
|
#else
|
2016-02-07 07:44:41 +08:00
|
|
|
rtcb = this_task();
|
2018-02-07 09:06:33 +08:00
|
|
|
#endif
|
2008-01-31 02:49:31 +08:00
|
|
|
|
2020-12-22 07:49:19 +08: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 10:23:22 +08:00
|
|
|
*/
|
|
|
|
|
2013-01-13 03:58:45 +08:00
|
|
|
/* We are now in a bad state -- the head of the ready to run task list
|
2008-01-31 02:49:31 +08: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 20:02:48 +08:00
|
|
|
* running.
|
2013-01-13 08:35:47 +08:00
|
|
|
*
|
|
|
|
* We disable pre-emption here by directly incrementing the lockcount
|
|
|
|
* (vs. calling sched_lock()).
|
2008-01-31 02:49:31 +08:00
|
|
|
*/
|
|
|
|
|
2013-01-13 08:35:47 +08:00
|
|
|
rtcb->lockcount++;
|
2016-02-13 04:55:31 +08:00
|
|
|
|
2008-01-31 02:49:31 +08:00
|
|
|
rtcb->task_state = TSTATE_TASK_READYTORUN;
|
|
|
|
|
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 13:39:22 +08:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* NOTE:
|
|
|
|
* During nxtask_terminate(), enter_critical_section() will be called
|
2021-02-25 20:48:46 +08: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 13:39:22 +08:00
|
|
|
* rtcb->irqcount = 0, event though we are in critical section.
|
2021-02-25 20:48:46 +08: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 13:39:22 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
rtcb->irqcount++;
|
|
|
|
#endif
|
|
|
|
|
2024-09-29 19:52:57 +08:00
|
|
|
dtcb->task_state = TSTATE_TASK_INACTIVE;
|
2024-10-23 21:13:22 +08:00
|
|
|
sched_note_stop(dtcb);
|
2024-09-29 19:52:57 +08:00
|
|
|
ret = nxsched_release_tcb(dtcb, dtcb->flags & TCB_FLAG_TTYPE_MASK);
|
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 13:39:22 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
rtcb->irqcount--;
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 02:49:31 +08:00
|
|
|
rtcb->task_state = TSTATE_TASK_RUNNING;
|
|
|
|
|
2016-03-23 08:19:57 +08:00
|
|
|
/* Decrement the lockcount on rctb. */
|
2008-01-31 02:49:31 +08:00
|
|
|
|
2013-01-13 03:58:45 +08:00
|
|
|
rtcb->lockcount--;
|
2016-02-13 04:55:31 +08:00
|
|
|
|
2014-02-19 01:50:32 +08:00
|
|
|
return ret;
|
2008-01-31 02:49:31 +08:00
|
|
|
}
|