fix compiler errors/include conflicts
What a hell of a nightmare this codebase has become over the past few weeks fascinates even myselfpull/1/head
parent
63f78d7b2b
commit
99a51a5dd2
@ -1,25 +1,24 @@
|
||||
/* SPDX-License-Identifier: BSD-3-Clause */
|
||||
/* See the end of this file for copyright, licensing, and warranty information. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ardix/sched.h>
|
||||
#include <arch/at91sam3x8e/interrupt.h>
|
||||
#include <arch/at91sam3x8e/spinlock.h>
|
||||
#include <ardix/atomic.h>
|
||||
|
||||
static SPINLOCK_DEFINE(atomic_context);
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <toolchain.h>
|
||||
void atomic_enter(void)
|
||||
{
|
||||
arch_spin_lock(&atomic_context);
|
||||
}
|
||||
|
||||
/** Enter atomic context, i.e. disable preemption */
|
||||
__always_inline void sched_atomic_enter(void)
|
||||
void atomic_leave(void)
|
||||
{
|
||||
arch_spin_lock(&_in_atomic_context);
|
||||
arch_spin_unlock(&atomic_context);
|
||||
}
|
||||
|
||||
/** Leave atomic context, i.e. re-enable preemption */
|
||||
__always_inline void sched_atomic_leave(void)
|
||||
int is_atomic_context(void)
|
||||
{
|
||||
arch_spin_unlock(&_in_atomic_context);
|
||||
return arch_spinlock_count(&atomic_context);
|
||||
}
|
||||
|
||||
/*
|
@ -1,73 +1,58 @@
|
||||
/* 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>
|
||||
#include <arch/at91sam3x8e/spinlock.h>
|
||||
|
||||
struct atom {
|
||||
atomic_int lock;
|
||||
};
|
||||
/* This code is basically stolen from arch/arm/include/asm/spinlock.h in Linux 5.9 */
|
||||
|
||||
/** Initialize an atom to be used as a lock. */
|
||||
__always_inline void atom_init(struct atom *atom)
|
||||
void arch_spinlock_init(spinlock_t *lock)
|
||||
{
|
||||
atom->lock = 0;
|
||||
lock->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)
|
||||
int arch_spin_lock(spinlock_t *lock)
|
||||
{
|
||||
if (atom->lock != 0)
|
||||
return -EAGAIN;
|
||||
atom->lock = -1;
|
||||
return 0;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
int arch_spin_unlock(spinlock_t *lock)
|
||||
{
|
||||
atom->lock++;
|
||||
if (atom->lock != 1) {
|
||||
atom->lock--;
|
||||
return -EAGAIN;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
int arch_spinlock_count(spinlock_t *lock)
|
||||
{
|
||||
atom->lock--;
|
||||
return lock->lock;
|
||||
}
|
||||
|
||||
/*
|
@ -0,0 +1,33 @@
|
||||
/* 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 <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.
|
||||
*/
|
@ -0,0 +1,32 @@
|
||||
/* 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_type.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.
|
||||
*/
|
@ -0,0 +1,38 @@
|
||||
/* SPDX-License-Identifier: BSD-3-Clause */
|
||||
/* See the end of this file for copyright, licensing, and warranty information. */
|
||||
|
||||
#pragma once
|
||||
|
||||
/** Enter atomic context. */
|
||||
void atomic_enter(void);
|
||||
|
||||
/** Leave atomic context. */
|
||||
void atomic_leave(void);
|
||||
|
||||
/** Return a nonzero value if the current process is in atomic context. */
|
||||
int is_atomic_context(void);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
Loading…
Reference in New Issue