2016-02-13 17:25:36 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/irq/irq_csection.c
|
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-05-09 21:25:30 +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
|
2016-02-13 17:25:36 +01:00
|
|
|
*
|
2020-05-09 21:25:30 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-02-13 17:25:36 +01:00
|
|
|
*
|
2020-05-09 21:25:30 +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.
|
2016-02-13 17:25:36 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2016-02-19 22:57:07 +01:00
|
|
|
#include <sys/types.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2016-02-19 22:57:07 +01:00
|
|
|
|
2016-05-18 16:21:28 +02:00
|
|
|
#include <nuttx/init.h>
|
2016-02-13 17:25:36 +01:00
|
|
|
#include <nuttx/spinlock.h>
|
2016-03-17 16:49:43 +01:00
|
|
|
#include <nuttx/sched_note.h>
|
2016-02-13 17:25:36 +01:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#include "sched/sched.h"
|
|
|
|
#include "irq/irq.h"
|
|
|
|
|
2018-11-25 18:50:15 +01:00
|
|
|
#ifdef CONFIG_IRQCOUNT
|
2023-12-12 04:53:18 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
# define cpu_irqlock_set(cpu) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
g_cpu_irqset |= (1 << cpu); \
|
|
|
|
} \
|
|
|
|
while (0)
|
|
|
|
#endif
|
2016-02-13 17:25:36 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
2016-03-22 00:08:07 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
2016-02-13 17:25:36 +01:00
|
|
|
/* This is the spinlock that enforces critical sections when interrupts are
|
|
|
|
* disabled.
|
|
|
|
*/
|
|
|
|
|
2021-04-21 03:27:47 +02:00
|
|
|
volatile spinlock_t g_cpu_irqlock = SP_UNLOCKED;
|
2016-02-13 17:25:36 +01:00
|
|
|
|
2016-02-17 20:20:01 +01:00
|
|
|
/* Used to keep track of which CPU(s) hold the IRQ lock. */
|
2016-02-15 15:06:17 +01:00
|
|
|
|
2021-04-21 03:27:47 +02:00
|
|
|
volatile cpu_set_t g_cpu_irqset;
|
2016-11-17 13:37:24 +01:00
|
|
|
|
2016-11-17 05:45:08 +01:00
|
|
|
/* Handles nested calls to enter_critical section from interrupt handlers */
|
|
|
|
|
2017-01-13 13:48:10 +01:00
|
|
|
volatile uint8_t g_cpu_nestcount[CONFIG_SMP_NCPUS];
|
2016-03-22 00:08:07 +01:00
|
|
|
#endif
|
2016-02-15 15:06:17 +01:00
|
|
|
|
2016-11-22 18:34:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-13 17:25:36 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: enter_critical_section
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take the CPU IRQ lock and disable interrupts on all CPUs. A thread-
|
2021-09-16 16:52:46 +02:00
|
|
|
* specific counter is incremented to indicate that the thread has IRQs
|
2016-02-13 17:25:36 +01:00
|
|
|
* disabled and to support nested calls to enter_critical_section().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-22 00:08:07 +01:00
|
|
|
#ifdef CONFIG_SMP
|
2016-02-13 17:25:36 +01:00
|
|
|
irqstate_t enter_critical_section(void)
|
|
|
|
{
|
2024-09-16 05:35:18 +02:00
|
|
|
FAR struct tcb_s *rtcb;
|
|
|
|
irqstate_t ret;
|
2016-11-17 13:37:24 +01:00
|
|
|
int cpu;
|
2016-11-15 14:54:18 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Disable interrupts.
|
|
|
|
*
|
|
|
|
* NOTE 1: Ideally this should disable interrupts on all CPUs, but most
|
|
|
|
* architectures only support disabling interrupts on the local CPU.
|
|
|
|
* NOTE 2: Interrupts may already be disabled, but we call up_irq_save()
|
|
|
|
* unconditionally because we need to return valid interrupt status in any
|
|
|
|
* event.
|
|
|
|
* NOTE 3: We disable local interrupts BEFORE taking the spinlock in order
|
|
|
|
* to prevent possible waits on the spinlock from interrupt handling on
|
|
|
|
* the local CPU.
|
2016-11-15 14:54:18 +01:00
|
|
|
*/
|
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
ret = up_irq_save();
|
2016-02-15 15:06:17 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
/* If called from an interrupt handler, then just take the spinlock.
|
|
|
|
* If we are already in a critical section, this will lock the CPU
|
|
|
|
* in the interrupt handler. Sounds worse than it is.
|
2016-05-18 16:21:28 +02:00
|
|
|
*/
|
2016-02-15 15:06:17 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
if (up_interrupt_context())
|
2016-02-15 15:06:17 +01:00
|
|
|
{
|
2024-05-08 13:56:22 +02:00
|
|
|
/* We are in an interrupt handler. How can this happen?
|
|
|
|
*
|
|
|
|
* 1. We were not in a critical section when the interrupt
|
|
|
|
* occurred. In this case, the interrupt was entered with:
|
|
|
|
*
|
|
|
|
* g_cpu_irqlock = SP_UNLOCKED.
|
|
|
|
* g_cpu_nestcount = 0
|
|
|
|
* All CPU bits in g_cpu_irqset should be zero
|
|
|
|
*
|
|
|
|
* 2. We were in a critical section and interrupts on this
|
|
|
|
* this CPU were disabled -- this is an impossible case.
|
|
|
|
*
|
|
|
|
* 3. We were in critical section, but up_irq_save() only
|
|
|
|
* disabled local interrupts on a different CPU;
|
|
|
|
* Interrupts could still be enabled on this CPU.
|
|
|
|
*
|
|
|
|
* g_cpu_irqlock = SP_LOCKED.
|
|
|
|
* g_cpu_nestcount = 0
|
|
|
|
* The bit in g_cpu_irqset for this CPU should be zero
|
|
|
|
*
|
|
|
|
* 4. An extension of 3 is that we may be re-entered numerous
|
|
|
|
* times from the same interrupt handler. In that case:
|
|
|
|
*
|
|
|
|
* g_cpu_irqlock = SP_LOCKED.
|
|
|
|
* g_cpu_nestcount > 0
|
|
|
|
* The bit in g_cpu_irqset for this CPU should be zero
|
|
|
|
*
|
|
|
|
* NOTE: However, the interrupt entry conditions can change due
|
|
|
|
* to previous processing by the interrupt handler that may
|
|
|
|
* instantiate a new thread that has irqcount > 0 and may then
|
|
|
|
* set the bit in g_cpu_irqset and g_cpu_irqlock = SP_LOCKED
|
2016-11-17 01:28:35 +01:00
|
|
|
*/
|
2016-03-22 00:08:07 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
/* Handle nested calls to enter_critical_section() from the same
|
|
|
|
* interrupt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cpu = this_cpu();
|
|
|
|
if (g_cpu_nestcount[cpu] > 0)
|
2016-05-18 16:21:28 +02:00
|
|
|
{
|
2024-05-08 13:56:22 +02:00
|
|
|
DEBUGASSERT(spin_is_locked(&g_cpu_irqlock) &&
|
|
|
|
g_cpu_nestcount[cpu] < UINT8_MAX);
|
|
|
|
g_cpu_nestcount[cpu]++;
|
|
|
|
}
|
2016-11-17 01:28:35 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
/* This is the first call to enter_critical_section from the
|
|
|
|
* interrupt handler.
|
|
|
|
*/
|
2016-02-13 17:25:36 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Make sure that the g_cpu_irqset was not already set
|
|
|
|
* by previous logic on this CPU that was executed by the
|
|
|
|
* interrupt handler. We know that the bit in g_cpu_irqset
|
|
|
|
* for this CPU was zero on entry into the interrupt handler,
|
|
|
|
* so if it is non-zero now then we know that was the case.
|
2016-11-18 14:38:16 +01:00
|
|
|
*/
|
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
if ((g_cpu_irqset & (1 << cpu)) == 0)
|
2016-11-17 05:45:08 +01:00
|
|
|
{
|
2024-05-08 13:56:22 +02:00
|
|
|
/* Wait until we can get the spinlock (meaning that we are
|
|
|
|
* no longer blocked by the critical section).
|
2016-11-17 05:45:08 +01:00
|
|
|
*/
|
|
|
|
|
2024-07-29 14:47:46 +02:00
|
|
|
spin_lock(&g_cpu_irqlock);
|
|
|
|
cpu_irqlock_set(cpu);
|
2016-11-17 05:45:08 +01:00
|
|
|
}
|
2024-05-08 13:56:22 +02:00
|
|
|
|
|
|
|
/* In any event, the nesting count is now one */
|
|
|
|
|
|
|
|
g_cpu_nestcount[cpu] = 1;
|
|
|
|
|
2023-12-12 04:53:18 +01:00
|
|
|
DEBUGASSERT(spin_is_locked(&g_cpu_irqlock) &&
|
|
|
|
(g_cpu_irqset & (1 << cpu)) != 0);
|
2016-05-18 16:21:28 +02:00
|
|
|
}
|
2024-05-08 13:56:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Normal tasking environment.
|
2024-05-08 13:56:22 +02:00
|
|
|
*
|
2024-09-16 05:35:18 +02:00
|
|
|
* Get the TCB of the currently executing task on this CPU (avoid
|
|
|
|
* using this_task() which can recurse.
|
2024-05-08 13:56:22 +02:00
|
|
|
*/
|
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
cpu = this_cpu();
|
|
|
|
rtcb = current_task(cpu);
|
|
|
|
DEBUGASSERT(rtcb != NULL);
|
2024-05-08 13:56:22 +02:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Do we already have interrupts disabled? */
|
2024-05-08 13:56:22 +02:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
if (rtcb->irqcount > 0)
|
2016-05-18 16:21:28 +02:00
|
|
|
{
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Yes... make sure that the spinlock is set and increment the
|
|
|
|
* IRQ lock count.
|
|
|
|
*
|
|
|
|
* NOTE: If irqcount > 0 then (1) we are in a critical section,
|
|
|
|
* and (2) this CPU should hold the lock.
|
2018-02-06 16:17:28 +01:00
|
|
|
*/
|
2016-11-17 01:28:35 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
DEBUGASSERT(spin_is_locked(&g_cpu_irqlock) &&
|
|
|
|
(g_cpu_irqset & (1 << this_cpu())) != 0 &&
|
|
|
|
rtcb->irqcount < INT16_MAX);
|
|
|
|
rtcb->irqcount++;
|
2024-05-08 13:56:22 +02:00
|
|
|
}
|
2024-09-16 05:35:18 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If we get here with irqcount == 0, then we know that the
|
|
|
|
* current task running on this CPU is not in a critical
|
|
|
|
* section. However other tasks on other CPUs may be in a
|
|
|
|
* critical section. If so, we must wait until they release
|
|
|
|
* the spinlock.
|
|
|
|
*/
|
2016-11-17 01:28:35 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
DEBUGASSERT((g_cpu_irqset & (1 << cpu)) == 0);
|
|
|
|
|
2024-07-29 14:47:46 +02:00
|
|
|
spin_lock(&g_cpu_irqlock);
|
2016-11-17 01:28:35 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Then set the lock count to 1.
|
|
|
|
*
|
|
|
|
* Interrupts disables must follow a stacked order. We
|
|
|
|
* cannot other context switches to re-order the enabling
|
|
|
|
* disabling of interrupts.
|
|
|
|
*
|
|
|
|
* The scheduler accomplishes this by treating the irqcount
|
|
|
|
* like lockcount: Both will disable pre-emption.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cpu_irqlock_set(cpu);
|
|
|
|
rtcb->irqcount = 1;
|
2016-02-16 22:21:45 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Note that we have entered the critical section */
|
2016-02-16 22:21:45 +01:00
|
|
|
|
2024-01-26 08:50:18 +01:00
|
|
|
#if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0
|
2024-05-30 05:05:24 +02:00
|
|
|
nxsched_critmon_csection(rtcb, true, return_address(0));
|
2018-11-24 17:32:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SCHED_INSTRUMENTATION_CSECTION
|
2024-09-16 05:35:18 +02:00
|
|
|
sched_note_csection(rtcb, true);
|
2016-02-16 22:21:45 +01:00
|
|
|
#endif
|
2024-09-16 05:35:18 +02:00
|
|
|
}
|
2016-11-15 22:44:43 +01:00
|
|
|
}
|
2016-02-13 17:25:36 +01:00
|
|
|
|
2016-11-15 14:54:18 +01:00
|
|
|
/* Return interrupt status */
|
2016-02-13 17:25:36 +01:00
|
|
|
|
2016-11-15 14:54:18 +01:00
|
|
|
return ret;
|
2016-02-13 17:25:36 +01:00
|
|
|
}
|
2018-11-25 18:50:15 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-03-22 00:08:07 +01:00
|
|
|
irqstate_t enter_critical_section(void)
|
|
|
|
{
|
2016-11-18 20:57:30 +01:00
|
|
|
irqstate_t ret;
|
|
|
|
|
|
|
|
/* Disable interrupts */
|
|
|
|
|
|
|
|
ret = up_irq_save();
|
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Check if we were called from an interrupt handler */
|
2016-03-22 00:08:07 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
if (!up_interrupt_context())
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
DEBUGASSERT(rtcb != NULL);
|
2016-03-22 00:08:07 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Have we just entered the critical section? Or is this a nested
|
|
|
|
* call to enter_critical_section.
|
|
|
|
*/
|
2018-11-24 22:07:12 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
DEBUGASSERT(rtcb->irqcount >= 0 && rtcb->irqcount < INT16_MAX);
|
|
|
|
if (++rtcb->irqcount == 1)
|
|
|
|
{
|
|
|
|
/* Note that we have entered the critical section */
|
2016-03-22 00:08:07 +01:00
|
|
|
|
2024-01-26 08:50:18 +01:00
|
|
|
#if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0
|
2024-05-30 05:05:24 +02:00
|
|
|
nxsched_critmon_csection(rtcb, true, return_address(0));
|
2018-11-24 17:32:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SCHED_INSTRUMENTATION_CSECTION
|
2024-09-16 05:35:18 +02:00
|
|
|
sched_note_csection(rtcb, true);
|
2018-11-24 17:32:45 +01:00
|
|
|
#endif
|
2024-09-16 05:35:18 +02:00
|
|
|
}
|
2016-03-22 00:08:07 +01:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:57:30 +01:00
|
|
|
/* Return interrupt status */
|
2016-03-22 00:08:07 +01:00
|
|
|
|
2016-11-18 20:57:30 +01:00
|
|
|
return ret;
|
2016-03-22 00:08:07 +01:00
|
|
|
}
|
|
|
|
#endif
|
2016-02-13 17:25:36 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: leave_critical_section
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Decrement the IRQ lock count and if it decrements to zero then release
|
|
|
|
* the spinlock.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-22 00:08:07 +01:00
|
|
|
#ifdef CONFIG_SMP
|
2016-02-13 17:25:36 +01:00
|
|
|
void leave_critical_section(irqstate_t flags)
|
|
|
|
{
|
2016-11-17 13:37:24 +01:00
|
|
|
int cpu;
|
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
/* If called from an interrupt handler, then just release the
|
|
|
|
* spinlock. The interrupt handling logic should already hold the
|
|
|
|
* spinlock if enter_critical_section() has been called. Unlocking
|
|
|
|
* the spinlock will allow interrupt handlers on other CPUs to execute
|
|
|
|
* again.
|
2016-05-18 16:21:28 +02:00
|
|
|
*/
|
2016-02-13 17:25:36 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
if (up_interrupt_context())
|
2016-02-13 17:25:36 +01:00
|
|
|
{
|
2024-05-08 13:56:22 +02:00
|
|
|
/* We are in an interrupt handler. Check if the last call to
|
|
|
|
* enter_critical_section() was nested.
|
2016-02-15 15:06:17 +01:00
|
|
|
*/
|
2016-02-13 17:25:36 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
cpu = this_cpu();
|
|
|
|
if (g_cpu_nestcount[cpu] > 1)
|
2016-02-15 15:06:17 +01:00
|
|
|
{
|
2024-05-08 13:56:22 +02:00
|
|
|
/* Yes.. then just decrement the nesting count */
|
2016-11-17 05:45:08 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
DEBUGASSERT(spin_is_locked(&g_cpu_irqlock));
|
|
|
|
g_cpu_nestcount[cpu]--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No, not nested. Restore the g_cpu_irqset for this CPU
|
|
|
|
* and release the spinlock (if necessary).
|
|
|
|
*/
|
2016-11-17 05:45:08 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
DEBUGASSERT(spin_is_locked(&g_cpu_irqlock) &&
|
|
|
|
g_cpu_nestcount[cpu] == 1);
|
2017-01-13 13:48:10 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
FAR struct tcb_s *rtcb = current_task(cpu);
|
2024-05-08 13:56:22 +02:00
|
|
|
DEBUGASSERT(rtcb != NULL);
|
2023-12-12 04:53:18 +01:00
|
|
|
DEBUGASSERT((g_cpu_irqset & (1 << cpu)) != 0);
|
2016-11-17 01:28:35 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
if (rtcb->irqcount <= 0)
|
|
|
|
{
|
2023-12-12 04:53:18 +01:00
|
|
|
cpu_irqlock_clear();
|
2016-11-17 05:45:08 +01:00
|
|
|
}
|
2024-05-08 13:56:22 +02:00
|
|
|
|
|
|
|
g_cpu_nestcount[cpu] = 0;
|
2016-02-15 15:06:17 +01:00
|
|
|
}
|
2024-05-08 13:56:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-16 05:35:18 +02:00
|
|
|
FAR struct tcb_s *rtcb;
|
2016-02-16 22:21:45 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Get the TCB of the currently executing task on this CPU (avoid
|
|
|
|
* using this_task() which can recurse.
|
|
|
|
*/
|
2016-02-15 17:15:07 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
cpu = this_cpu();
|
|
|
|
rtcb = current_task(cpu);
|
|
|
|
DEBUGASSERT(rtcb != NULL && rtcb->irqcount > 0);
|
2016-11-15 15:37:58 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Normal tasking context. We need to coordinate with other
|
|
|
|
* tasks.
|
|
|
|
*
|
|
|
|
* Will we still have interrupts disabled after decrementing the
|
|
|
|
* count?
|
|
|
|
*/
|
2024-03-14 03:42:30 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
if (rtcb->irqcount > 1)
|
|
|
|
{
|
|
|
|
/* Yes... the spinlock should remain set */
|
2024-03-14 03:42:30 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
DEBUGASSERT(spin_is_locked(&g_cpu_irqlock));
|
|
|
|
rtcb->irqcount--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Note that we have left the critical section */
|
2016-11-17 01:28:35 +01:00
|
|
|
|
2024-01-26 08:50:18 +01:00
|
|
|
#if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0
|
2024-05-30 05:05:24 +02:00
|
|
|
nxsched_critmon_csection(rtcb, false, return_address(0));
|
2018-11-24 17:32:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SCHED_INSTRUMENTATION_CSECTION
|
2024-09-16 05:35:18 +02:00
|
|
|
sched_note_csection(rtcb, false);
|
2016-11-17 01:28:35 +01:00
|
|
|
#endif
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Decrement our count on the lock. If all CPUs have
|
|
|
|
* released, then unlock the spinlock.
|
|
|
|
*/
|
2016-02-17 17:55:18 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
DEBUGASSERT(spin_is_locked(&g_cpu_irqlock) &&
|
|
|
|
(g_cpu_irqset & (1 << cpu)) != 0);
|
2016-11-17 13:37:24 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Now, possibly on return from a context switch, clear our
|
|
|
|
* count on the lock. If all CPUs have released the lock,
|
|
|
|
* then unlock the global IRQ spinlock.
|
|
|
|
*/
|
2016-12-28 20:58:24 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
rtcb->irqcount = 0;
|
|
|
|
cpu_irqlock_clear();
|
2016-12-28 20:58:24 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Have all CPUs released the lock? */
|
|
|
|
}
|
2016-05-18 16:21:28 +02:00
|
|
|
}
|
2016-02-15 15:06:17 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Restore the previous interrupt state which may still be interrupts
|
|
|
|
* disabled (but we don't have a mechanism to verify that now)
|
|
|
|
*/
|
|
|
|
|
2016-05-18 16:21:28 +02:00
|
|
|
up_irq_restore(flags);
|
2016-02-13 17:25:36 +01:00
|
|
|
}
|
2018-11-24 17:32:45 +01:00
|
|
|
|
2018-11-25 18:50:15 +01:00
|
|
|
#else
|
2018-11-24 17:32:45 +01:00
|
|
|
|
2016-03-22 00:08:07 +01:00
|
|
|
void leave_critical_section(irqstate_t flags)
|
|
|
|
{
|
2016-05-18 16:21:28 +02:00
|
|
|
/* Check if we were called from an interrupt handler and that the tasks
|
|
|
|
* lists have been initialized.
|
|
|
|
*/
|
2016-03-22 00:08:07 +01:00
|
|
|
|
2024-05-08 13:56:22 +02:00
|
|
|
if (!up_interrupt_context())
|
2016-03-22 00:08:07 +01:00
|
|
|
{
|
2024-09-16 05:35:18 +02:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
DEBUGASSERT(rtcb != NULL);
|
2024-03-14 03:42:30 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Have we left entered the critical section? Or are we still
|
|
|
|
* nested.
|
|
|
|
*/
|
2024-03-14 03:42:30 +01:00
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
DEBUGASSERT(rtcb->irqcount > 0);
|
|
|
|
if (--rtcb->irqcount <= 0)
|
|
|
|
{
|
|
|
|
/* Note that we have left the critical section */
|
2016-03-22 00:08:07 +01:00
|
|
|
|
2024-01-26 08:50:18 +01:00
|
|
|
#if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0
|
2024-05-30 05:05:24 +02:00
|
|
|
nxsched_critmon_csection(rtcb, false, return_address(0));
|
2018-11-24 17:32:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SCHED_INSTRUMENTATION_CSECTION
|
2024-09-16 05:35:18 +02:00
|
|
|
sched_note_csection(rtcb, false);
|
2018-11-24 17:32:45 +01:00
|
|
|
#endif
|
2024-09-16 05:35:18 +02:00
|
|
|
}
|
2016-03-22 00:08:07 +01:00
|
|
|
}
|
|
|
|
|
2024-09-16 05:35:18 +02:00
|
|
|
/* Restore the previous interrupt state. */
|
|
|
|
|
2016-03-22 00:08:07 +01:00
|
|
|
up_irq_restore(flags);
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-25 18:50:15 +01:00
|
|
|
#endif /* CONFIG_IRQCOUNT */
|