sched: implement spinlocks

This commit is contained in:
Felix Kopp 2020-11-30 22:19:30 +01:00
parent 0026db4583
commit e45f75f6bc
No known key found for this signature in database
GPG key ID: C478BA0A85F75728
6 changed files with 325 additions and 3 deletions

View file

@ -5,6 +5,7 @@
#include <ardix/sched.h>
#include <arch/at91sam3x8e/interrupt.h>
#include <arch/at91sam3x8e/spinlock.h>
#include <stdbool.h>
#include <toolchain.h>
@ -12,13 +13,13 @@
/** Enter atomic context, i.e. disable preemption */
__always_inline void sched_atomic_enter(void)
{
_is_atomic_context = true;
arch_spin_lock(&_in_atomic_context);
}
/** Leave atomic context, i.e. re-enable preemption */
__always_inline void sched_atomic_leave(void)
{
_is_atomic_context = false;
arch_spin_unlock(&_in_atomic_context);
}
/*

View file

@ -0,0 +1,110 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#pragma once
#include <stdint.h>
#include <toolchain.h>
typedef struct spinlock {
int lock;
} spinlock_t;
/* This code is basically stolen from arch/arm/include/asm/spinlock.h in Linux 5.9 */
/**
* Initialize a spinlock.
*
* @param lock: Pointer to the spinlock.
*/
inline void arch_spinlock_init(spinlock_t *lock)
{
lock->lock = 0;
}
/**
* Increment the lock count on a spinlock.
*
* @param lock: Pointer to the spinlock.
* @returns The new lock count.
*/
inline int arch_spin_lock(spinlock_t *lock)
{
int tmp;
int newval;
spinlock_t lockval;
__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 */
" teq %2, #0 \n" /* store successful? */
" bne 1b \n" /* -> goto 1 if not */
" dmb " /* memory barrier */
: "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
: "r" (lock)
: "cc");
return newval;
}
/**
* Decrement the lock count on a spinlock.
*
* @param lock: Pointer to the spinlock.
* @returns The new lock count.
*/
inline int arch_spin_unlock(spinlock_t *lock)
{
int tmp;
int newval;
spinlock_t lockval;
__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)
: "cc");
return newval;
}
/**
* Get the lock count on a spinlock.
*
* @param lock: Pointer to the spinlock.
*/
__always_inline int arch_spinlock_count(spinlock_t *lock)
{
return lock->lock;
}
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
*/

33
include/arch/spinlock.h Normal file
View file

@ -0,0 +1,33 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#pragma once
#include <arch/arch_include.h>
#include ARCH_INCLUDE(spinlock.h)
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
*/

96
include/ardix/atom.h Normal file
View file

@ -0,0 +1,96 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
/*
* An over-simplified approach of implementing locks on a system that
* doesn't even support SMP. Describing the realization as "unfortunate"
* is pretty much an understatement, it is straight up horrible because it
* can lead to two threads failing to acquire a lock at the same time
* (but it is platform-agnostic thanks to C11, yay!).
*/
#pragma once
#include <errno.h>
#include <stdatomic.h>
#include <toolchain.h>
struct atom {
atomic_int lock;
};
/** Initialize an atom to be used as a lock. */
__always_inline void atom_init(struct atom *atom)
{
atom->lock = 0;
}
/**
* Destroy this atom or fail if it is currently locked.
* If successful, this will make any subsequent locking attempts fail.
*
* @param atom: The atom to be destroyed.
* @returns 0 on success, and `-EAGAIN` if the atom is currently locked.
*/
__always_inline int atom_destroy(struct atom *atom)
{
if (atom->lock != 0)
return -EAGAIN;
atom->lock = -1;
return 0;
}
/**
* Attempt to aquire a lock on an atom.
*
* @param atom: The atom to get the the lock on.
* @returns 0 on success, and `-EAGAIN` if the atom was already locked by
* another process.
*/
__always_inline int atom_lock(struct atom *atom)
{
atom->lock++;
if (atom->lock != 1) {
atom->lock--;
return -EAGAIN;
} else {
return 0;
}
}
/**
* Release the lock on an atom.
* Even though it is possible with the current implementation, releasing a
* lock that isn't yours is a bad idea for obvious reasons.
*
* @param atom: The atom the release the lock from.
*/
__always_inline void atom_unlock(struct atom *atom)
{
atom->lock--;
}
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
*/

View file

@ -60,7 +60,7 @@ struct process {
/** The currently executing process. */
extern struct process *_current_process;
extern bool _is_atomic_context;
extern spinlock_t _in_atomic_context;
/**
* Initialize the scheduler subsystem.

82
include/ardix/spinlock.h Normal file
View file

@ -0,0 +1,82 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#pragma once
#include <arch/spinlock.h>
/*
* 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.
*/
/**
* 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 <sandtler@sandtler.club>
*
* 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.
*/