2017-12-07 20:39:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/irq/irq_spinlock.c
|
|
|
|
*
|
2020-07-28 05:16:17 +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
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
2020-07-28 05:16:17 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
2020-07-28 05:16:17 +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.
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/spinlock.h>
|
|
|
|
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2017-12-07 20:39:04 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#include "sched/sched.h"
|
|
|
|
|
2024-03-22 09:17:00 +01:00
|
|
|
#if defined(CONFIG_SPINLOCK)
|
2018-02-04 22:41:22 +01:00
|
|
|
|
2017-12-07 20:39:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Used for access control */
|
|
|
|
|
2021-04-21 03:27:47 +02:00
|
|
|
static volatile spinlock_t g_irq_spin = SP_UNLOCKED;
|
2017-12-07 20:39:04 +01:00
|
|
|
|
|
|
|
/* Handles nested calls to spin_lock_irqsave and spin_unlock_irqrestore */
|
|
|
|
|
|
|
|
static volatile uint8_t g_irq_spin_count[CONFIG_SMP_NCPUS];
|
|
|
|
|
2023-10-15 17:13:11 +02:00
|
|
|
#ifdef CONFIG_RW_SPINLOCK
|
|
|
|
/* Used for access control */
|
|
|
|
|
|
|
|
static volatile rwlock_t g_irq_rwspin = RW_SP_UNLOCKED;
|
|
|
|
|
|
|
|
/* Handles nested calls to write_lock_irqsave and write_unlock_irqrestore */
|
|
|
|
|
|
|
|
static volatile uint8_t g_irq_rwspin_count[CONFIG_SMP_NCPUS];
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-12-07 20:39:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spin_lock_irqsave
|
|
|
|
*
|
|
|
|
* Description:
|
2021-02-06 01:31:18 +01:00
|
|
|
* If SMP is enabled:
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* If the argument lock is not specified (i.e. NULL),
|
|
|
|
* disable local interrupts and take the global spinlock (g_irq_spin)
|
2017-12-07 20:39:04 +01:00
|
|
|
* if the call counter (g_irq_spin_count[cpu]) equals to 0. Then the
|
2021-07-24 15:17:56 +02:00
|
|
|
* counter on the CPU is incremented to allow nested call and return
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* the interrupt state.
|
|
|
|
*
|
|
|
|
* If the argument lock is specified,
|
2021-07-24 15:17:56 +02:00
|
|
|
* disable local interrupts and take the given lock and return the
|
|
|
|
* interrupt state.
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
|
|
|
* NOTE: This API is very simple to protect data (e.g. H/W register
|
|
|
|
* or internal data structure) in SMP mode. But do not use this API
|
|
|
|
* with kernel APIs which suspend a caller thread. (e.g. nxsem_wait)
|
|
|
|
*
|
2021-02-06 01:31:18 +01:00
|
|
|
* If SMP is not enabled:
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* This function is equivalent to up_irq_save().
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* lock - Caller specific spinlock. If specified NULL, g_irq_spin is used
|
|
|
|
* and can be nested. Otherwise, nested call for the same lock
|
|
|
|
* would cause a deadlock
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* An opaque, architecture-specific value that represents the state of
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* the interrupts prior to the call to spin_lock_irqsave(lock);
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
irqstate_t spin_lock_irqsave(spinlock_t *lock)
|
2017-12-07 20:39:04 +01:00
|
|
|
{
|
|
|
|
irqstate_t ret;
|
|
|
|
ret = up_irq_save();
|
|
|
|
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
if (NULL == lock)
|
|
|
|
{
|
2024-03-20 03:26:36 +01:00
|
|
|
int me = up_cpu_index();
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
if (0 == g_irq_spin_count[me])
|
|
|
|
{
|
|
|
|
spin_lock(&g_irq_spin);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_irq_spin_count[me]++;
|
|
|
|
DEBUGASSERT(0 != g_irq_spin_count[me]);
|
|
|
|
}
|
|
|
|
else
|
2017-12-07 20:39:04 +01:00
|
|
|
{
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
spin_lock(lock);
|
2017-12-07 20:39:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-12-28 07:30:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: spin_lock_irqsave_wo_note
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
irqstate_t spin_lock_irqsave_wo_note(spinlock_t *lock)
|
|
|
|
{
|
|
|
|
irqstate_t ret;
|
|
|
|
ret = up_irq_save();
|
|
|
|
|
|
|
|
if (NULL == lock)
|
|
|
|
{
|
2024-03-20 03:26:36 +01:00
|
|
|
int me = up_cpu_index();
|
2022-12-28 07:30:38 +01:00
|
|
|
if (0 == g_irq_spin_count[me])
|
|
|
|
{
|
|
|
|
spin_lock_wo_note(&g_irq_spin);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_irq_spin_count[me]++;
|
|
|
|
DEBUGASSERT(0 != g_irq_spin_count[me]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spin_lock_wo_note(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-07 20:39:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: spin_unlock_irqrestore
|
|
|
|
*
|
|
|
|
* Description:
|
2021-02-06 01:31:18 +01:00
|
|
|
* If SMP is enabled:
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* If the argument lock is not specified (i.e. NULL),
|
|
|
|
* decrement the call counter (g_irq_spin_count[cpu]) and if it
|
2017-12-07 20:39:04 +01:00
|
|
|
* decrements to zero then release the spinlock (g_irq_spin) and
|
|
|
|
* restore the interrupt state as it was prior to the previous call to
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* spin_lock_irqsave(NULL).
|
|
|
|
*
|
2021-07-24 15:17:56 +02:00
|
|
|
* If the argument lock is specified, release the lock and restore
|
|
|
|
* the interrupt state as it was prior to the previous call to
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* spin_lock_irqsave(lock).
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
2021-02-06 01:31:18 +01:00
|
|
|
* If SMP is not enabled:
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* This function is equivalent to up_irq_restore().
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* lock - Caller specific spinlock. If specified NULL, g_irq_spin is used.
|
|
|
|
*
|
2017-12-07 20:39:04 +01:00
|
|
|
* flags - The architecture-specific value that represents the state of
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
* the interrupts prior to the call to spin_lock_irqsave(lock);
|
2017-12-07 20:39:04 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
void spin_unlock_irqrestore(spinlock_t *lock, irqstate_t flags)
|
2017-12-07 20:39:04 +01:00
|
|
|
{
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
if (NULL == lock)
|
|
|
|
{
|
2024-03-20 03:26:36 +01:00
|
|
|
int me = up_cpu_index();
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
DEBUGASSERT(0 < g_irq_spin_count[me]);
|
|
|
|
g_irq_spin_count[me]--;
|
2017-12-07 20:39:04 +01:00
|
|
|
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
if (0 == g_irq_spin_count[me])
|
|
|
|
{
|
|
|
|
spin_unlock(&g_irq_spin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-12-07 20:39:04 +01:00
|
|
|
{
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
spin_unlock(lock);
|
2017-12-07 20:39:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
up_irq_restore(flags);
|
|
|
|
}
|
2018-02-04 22:41:22 +01:00
|
|
|
|
2022-12-28 07:30:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: spin_lock_irqsave_wo_note
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void spin_unlock_irqrestore_wo_note(spinlock_t *lock, irqstate_t flags)
|
|
|
|
{
|
|
|
|
if (NULL == lock)
|
|
|
|
{
|
2024-03-20 03:26:36 +01:00
|
|
|
int me = up_cpu_index();
|
2022-12-28 07:30:38 +01:00
|
|
|
DEBUGASSERT(0 < g_irq_spin_count[me]);
|
|
|
|
g_irq_spin_count[me]--;
|
|
|
|
|
|
|
|
if (0 == g_irq_spin_count[me])
|
|
|
|
{
|
|
|
|
spin_unlock_wo_note(&g_irq_spin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spin_unlock_wo_note(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
up_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
2023-10-15 17:13:11 +02:00
|
|
|
#ifdef CONFIG_RW_SPINLOCK
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: read_lock_irqsave
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If SMP is enabled:
|
|
|
|
* If the 'lock' argument is not specified (i.e. NULL), disable local
|
|
|
|
* interrupts and take the global read write spinlock (g_irq_rwspin)
|
|
|
|
* and increase g_irq_rwspin.
|
|
|
|
*
|
|
|
|
* If the 'lock' argument is specified,
|
|
|
|
* disable local interrupts and take the lock spinlock and return
|
|
|
|
* the interrupt state.
|
|
|
|
*
|
|
|
|
* NOTE: This API is very simple to protect data (e.g. H/W register
|
|
|
|
* or internal data structure) in SMP mode. Do not use this API
|
|
|
|
* with kernel APIs which suspend a caller thread. (e.g. nxsem_wait)
|
|
|
|
*
|
|
|
|
* If SMP is not enabled:
|
|
|
|
* This function is equivalent to up_irq_save().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lock - Caller specific spinlock. If specified NULL, g_irq_spin is used
|
|
|
|
* and can be nested. Otherwise, nested call for the same lock
|
|
|
|
* would cause a deadlock
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* An opaque, architecture-specific value that represents the state of
|
|
|
|
* the interrupts prior to the call to write_lock_irqsave(lock);
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
irqstate_t read_lock_irqsave(FAR rwlock_t *lock)
|
|
|
|
{
|
|
|
|
irqstate_t ret;
|
|
|
|
ret = up_irq_save();
|
|
|
|
|
|
|
|
if (NULL == lock)
|
|
|
|
{
|
|
|
|
read_lock(&g_irq_rwspin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_lock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: read_unlock_irqrestore
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If SMP is enabled:
|
|
|
|
* If the argument lock is not specified (i.e. NULL),
|
|
|
|
* decrement the call counter (g_irq_rwspin) and restore the interrupt
|
|
|
|
* state as it was prior to the previous call to read_lock_irqsave(NULL).
|
|
|
|
*
|
|
|
|
* If the argument lock is specified, release the lock and
|
|
|
|
* restore the interrupt state as it was prior to the previous call to
|
|
|
|
* read_lock_irqsave(lock).
|
|
|
|
*
|
|
|
|
* If SMP is not enabled:
|
|
|
|
* This function is equivalent to up_irq_restore().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lock - Caller specific spinlock. If specified NULL, g_irq_spin is used.
|
|
|
|
*
|
|
|
|
* flags - The architecture-specific value that represents the state of
|
|
|
|
* the interrupts prior to the call to read_lock_irqsave(lock);
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void read_unlock_irqrestore(rwlock_t *lock, irqstate_t flags)
|
|
|
|
{
|
|
|
|
if (NULL == lock)
|
|
|
|
{
|
|
|
|
read_unlock(&g_irq_rwspin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_unlock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
up_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: write_lock_irqsave
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If SMP is enabled:
|
|
|
|
* If the argument lock is not specified (i.e. NULL),
|
|
|
|
* disable local interrupts and take the global spinlock (g_irq_rwspin)
|
|
|
|
* if the call counter (g_irq_rwspin_count[cpu]) equals to 0. Then
|
|
|
|
* the counter on the CPU is incremented to allow nested calls and return
|
|
|
|
* the interrupt state.
|
|
|
|
*
|
|
|
|
* If the argument lock is specified,
|
|
|
|
* disable local interrupts and take the lock spinlock and return
|
|
|
|
* the interrupt state.
|
|
|
|
*
|
|
|
|
* NOTE: This API is very simple to protect data (e.g. H/W register
|
|
|
|
* or internal data structure) in SMP mode. But do not use this API
|
|
|
|
* with kernel APIs which suspend a caller thread. (e.g. nxsem_wait)
|
|
|
|
*
|
|
|
|
* If SMP is not enabled:
|
|
|
|
* This function is equivalent to up_irq_save().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lock - Caller specific spinlock. If specified NULL, g_irq_spin is used
|
|
|
|
* and can be nested. Otherwise, nested call for the same lock
|
|
|
|
* would cause a deadlock
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* An opaque, architecture-specific value that represents the state of
|
|
|
|
* the interrupts prior to the call to write_lock_irqsave(lock);
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
irqstate_t write_lock_irqsave(rwlock_t *lock)
|
|
|
|
{
|
|
|
|
irqstate_t ret;
|
|
|
|
ret = up_irq_save();
|
|
|
|
|
|
|
|
if (NULL == lock)
|
|
|
|
{
|
2024-03-20 03:26:36 +01:00
|
|
|
int me = up_cpu_index();
|
2023-10-15 17:13:11 +02:00
|
|
|
if (0 == g_irq_rwspin_count[me])
|
|
|
|
{
|
|
|
|
write_lock(&g_irq_rwspin);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_irq_rwspin_count[me]++;
|
|
|
|
DEBUGASSERT(0 != g_irq_rwspin_count[me]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
write_lock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: write_unlock_irqrestore
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If SMP is enabled:
|
|
|
|
* If the argument lock is not specified (i.e. NULL),
|
|
|
|
* decrement the call counter (g_irq_rwspin_count[cpu]) and if it
|
|
|
|
* decrements to zero then release the spinlock (g_irq_rwspin) and
|
|
|
|
* restore the interrupt state as it was prior to the previous call to
|
|
|
|
* write_lock_irqsave(NULL).
|
|
|
|
*
|
|
|
|
* If the argument lock is specified, release the lock and
|
|
|
|
* restore the interrupt state as it was prior to the previous call to
|
|
|
|
* write_lock_irqsave(lock).
|
|
|
|
*
|
|
|
|
* If SMP is not enabled:
|
|
|
|
* This function is equivalent to up_irq_restore().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lock - Caller specific spinlock. If specified NULL, g_irq_spin is used.
|
|
|
|
*
|
|
|
|
* flags - The architecture-specific value that represents the state of
|
|
|
|
* the interrupts prior to the call to write_lock_irqsave(lock);
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void write_unlock_irqrestore(rwlock_t *lock, irqstate_t flags)
|
|
|
|
{
|
|
|
|
if (NULL == lock)
|
|
|
|
{
|
2024-03-20 03:26:36 +01:00
|
|
|
int me = up_cpu_index();
|
2023-10-15 17:13:11 +02:00
|
|
|
DEBUGASSERT(0 < g_irq_rwspin_count[me]);
|
|
|
|
g_irq_rwspin_count[me]--;
|
|
|
|
|
|
|
|
if (0 == g_irq_rwspin_count[me])
|
|
|
|
{
|
|
|
|
write_unlock(&g_irq_rwspin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
write_unlock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
up_irq_restore(flags);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_RW_SPINLOCK */
|
2024-03-22 09:17:00 +01:00
|
|
|
#endif /* CONFIG_SPINLOCK */
|