Compare commits
8 Commits
Author | SHA1 | Date |
---|---|---|
anna | e86ef2acbd | 2 years ago |
anna | 5d2539fc4a | 2 years ago |
anna | 3e35afcfa9 | 2 years ago |
anna | f293c6661e | 2 years ago |
anna | 4177931774 | 2 years ago |
anna | e80a6cb630 | 2 years ago |
anna | ad76275721 | 2 years ago |
anna | adccbef80d | 2 years ago |
@ -0,0 +1,114 @@
|
||||
/* See the end of this file for copyright, license, and warranty information. */
|
||||
|
||||
.include "asm.S"
|
||||
|
||||
.text
|
||||
|
||||
/* int _atom_add(volatile int *atom, int val) */
|
||||
func_begin _atom_add
|
||||
push {r4}
|
||||
1: ldrex r2, [r0] /* int old = __ldrex(atom) */
|
||||
add r3, r2, r1 /* int new = old + val */
|
||||
strex r4, r3, [r0] /* int err = __strex(atom, new) */
|
||||
teq r4, #0 /* if (err) */
|
||||
bne 1b /* goto 1 */
|
||||
dmb /* data memory barrier */
|
||||
mov r0, r2 /* return old */
|
||||
pop {r4}
|
||||
bx lr
|
||||
func_end _atom_add
|
||||
|
||||
/* these are the same as _atom_add except for the instruction
|
||||
* in the LDREX/STREX pair, so i'm not gonna annotate them */
|
||||
|
||||
func_begin _atom_sub
|
||||
push {r4}
|
||||
1: ldrex r2, [r0]
|
||||
sub r3, r2, r1
|
||||
strex r4, r3, [r0]
|
||||
teq r4, #0
|
||||
bne 1b
|
||||
dmb
|
||||
mov r0, r2
|
||||
pop {r4}
|
||||
bx lr
|
||||
func_end _atom_sub
|
||||
|
||||
func_begin _atom_and
|
||||
push {r4}
|
||||
1: ldrex r2, [r0]
|
||||
and r3, r2, r1
|
||||
strex r4, r3, [r0]
|
||||
teq r4, #0
|
||||
bne 1b
|
||||
dmb
|
||||
mov r0, r2
|
||||
pop {r4}
|
||||
bx lr
|
||||
func_end _atom_and
|
||||
|
||||
func_begin _atom_or
|
||||
push {r4}
|
||||
1: ldrex r2, [r0]
|
||||
orr r3, r2, r1
|
||||
strex r4, r3, [r0]
|
||||
teq r4, #0
|
||||
bne 1b
|
||||
dmb
|
||||
mov r0, r2
|
||||
pop {r4}
|
||||
bx lr
|
||||
func_end _atom_or
|
||||
|
||||
func_begin _atom_xor
|
||||
push {r4}
|
||||
1: ldrex r2, [r0]
|
||||
eor r3, r2, r1
|
||||
strex r4, r3, [r0]
|
||||
teq r4, #0
|
||||
bne 1b
|
||||
dmb
|
||||
mov r0, r2
|
||||
pop {r4}
|
||||
bx lr
|
||||
func_end _atom_xor
|
||||
|
||||
/* int _atom_xchg(volatile int *atom, int val) */
|
||||
func_begin _atom_xchg
|
||||
ldrex r2, [r0] /* int old = __ldrex(atom) */
|
||||
strex r3, r1, [r0] /* int err = __strex(atom, val) */
|
||||
teq r3, #0 /* if (err) */
|
||||
bne _atom_xchg /* goto _atom_xchg */
|
||||
dmb /* data memory barrier */
|
||||
mov r0, r2 /* return old */
|
||||
bx lr
|
||||
func_end _atom_xchg
|
||||
|
||||
/* int _atom_cmpxchg(volatile int *atom, int cmp, int val) */
|
||||
func_begin _atom_cmpxchg
|
||||
push {r4}
|
||||
1: mov r4, #0 /* int err = 0 */
|
||||
ldrex r3, [r0] /* int old = __ldrex(atom) */
|
||||
teq r3, r1 /* if (old == cmp) */
|
||||
it eq
|
||||
strexeq r4, r1, [r0] /* err = __strex(atom, val) */
|
||||
teq r4, #0 /* if (err) */
|
||||
bne 1b /* goto 1b */
|
||||
dmb /* data memory barrier */
|
||||
mov r0, r3 /* return old */
|
||||
pop {r4}
|
||||
bx lr
|
||||
func_end _atom_cmpxchg
|
||||
|
||||
/*
|
||||
* This file is part of Ardix.
|
||||
* Copyright (c) 2021 Felix Kopp <owo@fef.moe>.
|
||||
*
|
||||
* Ardix is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
@ -1,27 +0,0 @@
|
||||
/* See the end of this file for copyright, license, and warranty information. */
|
||||
|
||||
#include <ardix/atom.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void atom_init(atom_t *atom)
|
||||
{
|
||||
atom->count = 0;
|
||||
}
|
||||
|
||||
int atom_count(atom_t *atom)
|
||||
{
|
||||
return atom->count;
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of Ardix.
|
||||
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
||||
*
|
||||
* Ardix is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
@ -1,46 +0,0 @@
|
||||
/* See the end of this file for copyright, license, and warranty information. */
|
||||
|
||||
.include "asm.S"
|
||||
|
||||
.text
|
||||
|
||||
/* int _atom_get(int *count); */
|
||||
func_begin _atom_get
|
||||
|
||||
ldrex r1, [r0] /* int tmp = atom->count */
|
||||
add r2, r1, #1 /* int newval = tmp + 1 */
|
||||
strex r3, r2, [r0] /* atom->count = newval */
|
||||
teq r3, #0 /* store successful? */
|
||||
bne _atom_get /* -> goto _atom_get to try again if not */
|
||||
dmb /* data memory barrier */
|
||||
mov r0, r2 /* return newval */
|
||||
bx lr
|
||||
|
||||
func_end _atom_get
|
||||
|
||||
/* int _atom_put(int *count); */
|
||||
func_begin _atom_put
|
||||
|
||||
ldrex r1, [r0] /* int tmp = atom->count */
|
||||
sub r2, r1, #1 /* int newval = tmp - 1 */
|
||||
strex r3, r2, [r0] /* atom->count = newval */
|
||||
teq r3, #0 /* store successful? */
|
||||
bne _atom_put /* -> goto _atom_put to try again if not */
|
||||
dmb /* data memory barrier */
|
||||
mov r0, r2 /* return newval */
|
||||
bx lr
|
||||
|
||||
func_end _atom_put
|
||||
|
||||
/*
|
||||
* This file is part of Ardix.
|
||||
* Copyright (c) 2021 Felix Kopp <owo@fef.moe>.
|
||||
*
|
||||
* Ardix is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
@ -1,34 +0,0 @@
|
||||
/* See the end of this file for copyright, license, and warranty information. */
|
||||
|
||||
#include <ardix/atomic.h>
|
||||
#include <ardix/atom.h>
|
||||
|
||||
static ATOM(atomic_context);
|
||||
|
||||
void atomic_enter(void)
|
||||
{
|
||||
atom_get(&atomic_context);
|
||||
}
|
||||
|
||||
void atomic_leave(void)
|
||||
{
|
||||
atom_put(&atomic_context);
|
||||
}
|
||||
|
||||
int is_atomic(void)
|
||||
{
|
||||
return atom_count(&atomic_context);
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of Ardix.
|
||||
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
||||
*
|
||||
* Ardix is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <ardix/types.h>
|
||||
|
||||
#include <toolchain.h>
|
||||
|
||||
static __always_inline word_t _atomic_enter(void)
|
||||
{
|
||||
word_t primask;
|
||||
__asm__ volatile(
|
||||
" mrs %0, primask \n"
|
||||
" cpsid i \n"
|
||||
: "=r"(primask));
|
||||
return primask;
|
||||
}
|
||||
|
||||
static __always_inline void _atomic_restore(word_t context)
|
||||
{
|
||||
if (!(context & 1))
|
||||
__asm__ volatile("cpsie i");
|
||||
}
|
||||
|
||||
static inline int _is_atomic(void)
|
||||
{
|
||||
int primask;
|
||||
__asm__ volatile("mrs %0, primask" : "=r"(primask));
|
||||
return primask & 1;
|
||||
}
|
Loading…
Reference in New Issue