From cc00a7a1ea5579d9f4b4cffb96d5c35bf3cda0fc Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Tue, 8 Dec 2020 12:55:23 +0100 Subject: [PATCH] atom: Replace spinlocks with atoms --- arch/at91sam3x8e/Makefile | 2 +- arch/at91sam3x8e/{spinlock.c => atom.c} | 46 ++++++------- arch/at91sam3x8e/atomic.c | 10 +-- arch/at91sam3x8e/sched.c | 5 -- include/arch/at91sam3x8e/spinlock.h | 67 ------------------ include/arch/at91sam3x8e/spinlock_type.h | 33 --------- include/arch/spinlock_type.h | 32 --------- include/{arch/spinlock.h => ardix/atom.h} | 10 ++- include/ardix/sched.h | 6 +- include/ardix/spinlock.h | 83 ----------------------- include/ardix/types.h | 5 +- init/main.c | 1 - lib/malloc.c | 1 + 13 files changed, 42 insertions(+), 259 deletions(-) rename arch/at91sam3x8e/{spinlock.c => atom.c} (72%) delete mode 100644 include/arch/at91sam3x8e/spinlock.h delete mode 100644 include/arch/at91sam3x8e/spinlock_type.h delete mode 100644 include/arch/spinlock_type.h rename include/{arch/spinlock.h => ardix/atom.h} (90%) delete mode 100644 include/ardix/spinlock.h diff --git a/arch/at91sam3x8e/Makefile b/arch/at91sam3x8e/Makefile index 1e2e78f..01afd9d 100644 --- a/arch/at91sam3x8e/Makefile +++ b/arch/at91sam3x8e/Makefile @@ -26,11 +26,11 @@ ARDIX_ARCH_PWD = $(PWD)/arch/at91sam3x8e ARDIX_SOURCES += \ + $(ARDIX_ARCH_PWD)/atom.c \ $(ARDIX_ARCH_PWD)/atomic.c \ $(ARDIX_ARCH_PWD)/interrupt.c \ $(ARDIX_ARCH_PWD)/sched.c \ $(ARDIX_ARCH_PWD)/serial.c \ - $(ARDIX_ARCH_PWD)/spinlock.c \ $(ARDIX_ARCH_PWD)/startup.c \ $(ARDIX_ARCH_PWD)/sys.c \ $(ARDIX_ARCH_PWD)/watchdog.c diff --git a/arch/at91sam3x8e/spinlock.c b/arch/at91sam3x8e/atom.c similarity index 72% rename from arch/at91sam3x8e/spinlock.c rename to arch/at91sam3x8e/atom.c index a3fca47..8f8926b 100644 --- a/arch/at91sam3x8e/spinlock.c +++ b/arch/at91sam3x8e/atom.c @@ -1,58 +1,56 @@ /* SPDX-License-Identifier: BSD-3-Clause */ /* See the end of this file for copyright, licensing, and warranty information. */ -#include +#include -/* This code is basically stolen from arch/arm/include/asm/spinlock.h in Linux 5.9 */ - -void arch_spinlock_init(spinlock_t *lock) +void atom_init(atom_t *atom) { - lock->lock = 0; + atom->count = 0; } -int arch_spin_lock(spinlock_t *lock) +int atom_get(atom_t *atom) { int tmp; int newval; - spinlock_t lockval; + atom_t atom_val; __asm__ volatile( -"1: ldrex %0, [%3] \n" /* lockval = *lock */ -" add %1, %0, #1 \n" /* newval = lockval.lock + 1 */ -" strex %2, %1, [%3] \n" /* *lock = newval */ +"1: ldrex %0, [%3] \n" /* atom_val = *atom */ +" add %1, %0, #1 \n" /* newval = atom_val.count + 1 */ +" strex %2, %1, [%3] \n" /* *atom = newval */ " teq %2, #0 \n" /* store successful? */ " bne 1b \n" /* -> goto 1 if not */ " dmb " /* memory barrier */ - : "=&r" (lockval), "=&r" (newval), "=&r" (tmp) - : "r" (lock) + : "=&r" (atom_val), "=&r" (newval), "=&r" (tmp) + : "r" (atom) : "cc"); return newval; } -int arch_spin_unlock(spinlock_t *lock) +int atom_put(atom_t *atom) { int tmp; int newval; - spinlock_t lockval; + atom_t atom_val; __asm__ volatile( -"1: ldrex %0, [%3] \n" -" sub %1, %0, #1 \n" -" strex %2, %1, [%3] \n" -" teq %2, #0 \n" -" bne 1b \n" -" dmb " - : "=&r" (lockval), "=&r" (newval), "=&r" (tmp) - : "r" (lock) +"1: ldrex %0, [%3] \n" /* atom_val = *atom */ +" sub %1, %0, #1 \n" /* newval = atom_val.count - 1 */ +" strex %2, %1, [%3] \n" /* *atom = newval */ +" teq %2, #0 \n" /* store successful? */ +" bne 1b \n" /* -> goto 1 if not */ +" dmb " /* memory barrier */ + : "=&r" (atom_val), "=&r" (newval), "=&r" (tmp) + : "r" (atom) : "cc"); return newval; } -int arch_spinlock_count(spinlock_t *lock) +int atom_count(atom_t *atom) { - return lock->lock; + return atom->count; } /* diff --git a/arch/at91sam3x8e/atomic.c b/arch/at91sam3x8e/atomic.c index 50163d8..8376e4b 100644 --- a/arch/at91sam3x8e/atomic.c +++ b/arch/at91sam3x8e/atomic.c @@ -1,24 +1,24 @@ /* SPDX-License-Identifier: BSD-3-Clause */ /* See the end of this file for copyright, licensing, and warranty information. */ -#include #include +#include -static SPINLOCK_DEFINE(atomic_context); +static ATOM_DEFINE(atomic_context); void atomic_enter(void) { - arch_spin_lock(&atomic_context); + atom_get(&atomic_context); } void atomic_leave(void) { - arch_spin_unlock(&atomic_context); + atom_put(&atomic_context); } int is_atomic_context(void) { - return arch_spinlock_count(&atomic_context); + return atom_count(&atomic_context); } /* diff --git a/arch/at91sam3x8e/sched.c b/arch/at91sam3x8e/sched.c index e4060f8..bc415f3 100644 --- a/arch/at91sam3x8e/sched.c +++ b/arch/at91sam3x8e/sched.c @@ -9,11 +9,6 @@ #include #include -#include -#include - -#include - void irq_sys_tick(void) { /* diff --git a/include/arch/at91sam3x8e/spinlock.h b/include/arch/at91sam3x8e/spinlock.h deleted file mode 100644 index 2a37dcf..0000000 --- a/include/arch/at91sam3x8e/spinlock.h +++ /dev/null @@ -1,67 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ -/* See the end of this file for copyright, licensing, and warranty information. */ - -#pragma once - -#include - -#include - -/* This code is basically stolen from arch/arm/include/asm/spinlock.h in Linux 5.9 */ - -#define SPINLOCK_DEFINE(name) spinlock_t name = { .lock = 0 } - -/** - * Initialize a spinlock. - * - * @param lock: Pointer to the spinlock. - */ -void arch_spinlock_init(spinlock_t *lock); - -/** - * Increment the lock count on a spinlock. - * - * @param lock: Pointer to the spinlock. - * @returns The new lock count. - */ -int arch_spin_lock(spinlock_t *lock); - -/** - * Decrement the lock count on a spinlock. - * - * @param lock: Pointer to the spinlock. - * @returns The new lock count. - */ -int arch_spin_unlock(spinlock_t *lock); - -/** - * Get the lock count on a spinlock. - * - * @param lock: Pointer to the spinlock. - */ -int arch_spinlock_count(spinlock_t *lock); - -/* - * Copyright (c) 2020 Felix Kopp - * - * 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 of the copyright holder 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 HOLDER 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. - */ diff --git a/include/arch/at91sam3x8e/spinlock_type.h b/include/arch/at91sam3x8e/spinlock_type.h deleted file mode 100644 index b1276e1..0000000 --- a/include/arch/at91sam3x8e/spinlock_type.h +++ /dev/null @@ -1,33 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ -/* See the end of this file for copyright, licensing, and warranty information. */ - -#pragma once - -typedef struct spinlock { - int lock; -} spinlock_t; - -/* - * Copyright (c) 2020 Felix Kopp - * - * 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 of the copyright holder 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 HOLDER 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. - */ diff --git a/include/arch/spinlock_type.h b/include/arch/spinlock_type.h deleted file mode 100644 index f2c6789..0000000 --- a/include/arch/spinlock_type.h +++ /dev/null @@ -1,32 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ -/* See the end of this file for copyright, licensing, and warranty information. */ - -#pragma once - -#include -#include ARCH_INCLUDE(spinlock_type.h) - -/* - * Copyright (c) 2020 Felix Kopp - * - * 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 of the copyright holder 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 HOLDER 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. - */ diff --git a/include/arch/spinlock.h b/include/ardix/atom.h similarity index 90% rename from include/arch/spinlock.h rename to include/ardix/atom.h index ac9989f..23432c0 100644 --- a/include/arch/spinlock.h +++ b/include/ardix/atom.h @@ -3,9 +3,15 @@ #pragma once -#include +#include -#include ARCH_INCLUDE(spinlock.h) +#define ATOM_DEFINE(name) atom_t name = { .count = 0, } + +int atom_get(atom_t *atom); + +int atom_put(atom_t *atom); + +int atom_count(atom_t *atom); /* * Copyright (c) 2020 Felix Kopp diff --git a/include/ardix/sched.h b/include/ardix/sched.h index a5bf8f2..e94ef1c 100644 --- a/include/ardix/sched.h +++ b/include/ardix/sched.h @@ -3,12 +3,10 @@ #pragma once -#include -#include -#include #include -#include +#include +#include #ifndef CONFIG_SCHED_MAXPROC /** The maximum number of processes. */ diff --git a/include/ardix/spinlock.h b/include/ardix/spinlock.h deleted file mode 100644 index 234ac04..0000000 --- a/include/ardix/spinlock.h +++ /dev/null @@ -1,83 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ -/* See the end of this file for copyright, licensing, and warranty information. */ - -#pragma once - -/* - * Spinlocks in Ardix work pretty much the same as they do on Linux - * (this is basically just a ripoff). See The Linux Kernel documentation - * for details. - */ - -#include -#include - -/** - * Initialize a spinlock. - * - * @param lock: Pointer to the spinlock. - */ -__always_inline void spinlock_init(spinlock_t *lock) -{ - arch_spinlock_init(lock); -} - -/** - * Increment the lock count on a spinlock. - * If required, block until we have exclusive access to the memory. - * - * @param lock: Pointer to the spinlock. - * @returns The new lock count. - */ -__always_inline int spin_lock(spinlock_t *lock) -{ - return arch_spin_lock(lock); -} - -/** - * Decrement the lock count on a spinlock. - * If required, block until we have exclusive access to the memory. - * - * @param lock: Pointer to the spinlock. - * @returns The new lock count. - */ -__always_inline int spin_unlock(spinlock_t *lock) -{ - return arch_spin_unlock(lock); -} - -/** - * Get the lock count of a spinlock. - * - * @param lock: Pointer to the spinlock. - * @returns The current lock count. - */ -__always_inline int spinlock_count(spinlock_t *lock) -{ - return arch_spinlock_count(lock); -} - -/* - * Copyright (c) 2020 Felix Kopp - * - * 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 of the copyright holder 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 HOLDER 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. - */ diff --git a/include/ardix/types.h b/include/ardix/types.h index ff19b19..5e2fab4 100644 --- a/include/ardix/types.h +++ b/include/ardix/types.h @@ -30,9 +30,10 @@ typedef _PID_TYPE_ pid_t; #define __SIG_ATOMIC_TYPE__ int #endif /* __SIG_ATOMIC_TYPE__ */ +/** Simple atomic reference counter */ typedef struct { - _Atomic __SIG_ATOMIC_TYPE__ lock; -} atomic_t; + int count; +} atom_t; /* * Copyright (c) 2020 Felix Kopp diff --git a/init/main.c b/init/main.c index a18e95b..da9d4b3 100644 --- a/init/main.c +++ b/init/main.c @@ -3,7 +3,6 @@ #include -#include #include #include #include diff --git a/lib/malloc.c b/lib/malloc.c index 9cdecd9..91fd6dd 100644 --- a/lib/malloc.c +++ b/lib/malloc.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: BSD-3-Clause */ /* See the end of this file for copyright, licensing, and warranty information. */ +#include #include #include #include