You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.3 KiB
C

/* See the end of this file for copyright and license terms. */
#pragma once
#ifndef _ARCH_ATOM_H_
#error "This file is not meant to be included directly, use <arch/atom.h>"
#endif
#include <gay/cdefs.h>
#include <gay/types.h>
#ifndef __LP64__
#error "__LP64__ must be defined on amd64"
#endif
static inline long latom_read(const latom_t *latom)
{
return latom->_value;
}
static inline long latom_write(latom_t *latom, long val)
{
long rax;
__asm__ volatile(
" movq (%2), %0 \n" /* rax = atom->_value */
"1: lock \n"
" cmpxchgq %1, (%2) \n" /* if (latom->_value == rax) latom->_value = val */
" pause \n" /* intel says you're supposed to do this in spin loops */
" jne 1b \n" /* else goto 1 (rax updated to new latom->_value) */
: "=a"(rax)
: "r"(val), "r"(&latom->_value)
: "cc", "memory"
);
return rax;
}
static inline long latom_cmp_xchg(latom_t *latom, long compare, long val)
{
long rax = compare;
__asm__ volatile(
" lock \n"
" cmpxchgq %1, (%2) \n" /* if ((rax = latom->_value) == compare) latom->_value = val */
: "+a"(rax)
: "r"(val), "r"(&latom->_value)
: "cc", "memory"
);
return rax;
}
/**
* @brief Perform an atomic load/add/store.
*
* @param atom Atom to add to
* @param val Value to add
* @return The value of `atom` *before* the operation
*/
static inline long latom_add(latom_t *latom, long val)
{
__asm__ volatile(
" lock \n"
" xaddq %0, (%1) \n"
: "+r"(val)
: "r"(&latom->_value)
: "cc", "memory"
);
return val;
}
static inline long latom_sub(latom_t *latom, long val)
{
return latom_add(latom, -val);
}
static inline bool latom_inc(latom_t *latom)
{
bool nonzero = false;
__asm__ volatile(
" lock \n"
" incq (%1) \n"
" setne %0 \n"
: "+r"(nonzero) /* read+write to ensure the initial value isn't optimized out */
: "r"(&latom->_value)
: "cc", "memory"
);
return nonzero;
}
static inline bool latom_dec(latom_t *latom)
{
bool nonzero = false;
__asm__ volatile(
" lock \n"
" decq (%1) \n"
" setne %0 \n"
: "+r"(nonzero) /* read+write to ensure the initializer isn't optimized out */
: "r"(&latom->_value)
: "cc", "memory"
);
return nonzero;
}
static inline long latom_and(latom_t *latom, long val)
{
long rax;
__asm__ volatile(
" movq (%2), %0 \n" /* rax = latom->_value */
"1: andq %0, %1 \n" /* val &= rax */
" lock \n"
" cmpxchgq %1, (%2) \n" /* if (latom->_value == rax) latom->_value = val */
" pause \n" /* intel says you're supposed to do this in spin loops */
" jne 1b \n" /* else goto 1 (rax updated to new latom->_value) */
: "=a"(rax), "+r"(val)
: "r"(&latom->_value)
: "cc", "memory"
);
return rax;
}
static inline long latom_or(latom_t *latom, long val)
{
long rax;
__asm__ volatile(
" movq (%2), %0 \n" /* rax = latom->_value */
"1: orq %0, %1 \n" /* val |= rax */
" lock \n"
" cmpxchgq %1, (%2) \n" /* if (latom->_value == rax) latom->_value = val */
" pause \n" /* intel says you're supposed to do this in spin loops */
" jne 1b \n" /* else goto 1 (rax updated to new latom->_value) */
: "=a"(rax), "+r"(val)
: "r"(&latom->_value)
: "cc", "memory"
);
return rax;
}
static inline long latom_xor(latom_t *latom, long val)
{
long rax;
__asm__ volatile(
" movq (%2), %0 \n" /* rax = latom->_value */
"1: xorq %0, %1 \n" /* val ^= rax */
" lock \n"
" cmpxchgq %1, (%2) \n" /* if (latom->_value == rax) latom->_value = val */
" pause \n" /* intel says you're supposed to do this in spin loops */
" jne 1b \n" /* else goto 1 (rax updated to new latom->_value) */
: "=a"(rax), "+r"(val)
: "r"(&latom->_value)
: "cc", "memory"
);
return rax;
}
static inline bool latom_set_bit(latom_t *latom, int pos)
{
int mask = 1 << pos;
long oldval = latom_or(latom, mask);
return (oldval & mask) == 0;
}
static inline bool latom_clr_bit(latom_t *latom, int pos)
{
int mask = 1 << pos;
long oldval = latom_and(latom, ~mask);
return (oldval & mask) != 0;
}
/*
* This file is part of GayBSD.
* Copyright (c) 2021 fef <owo@fef.moe>.
*
* GayBSD is nonviolent software: you may only use, redistribute, and/or
* modify it under the terms of the Cooperative Nonviolent Public License
* (CNPL) as found in the LICENSE file in the source code root directory
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
* of the license, or (at your option) any later version.
*
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPL for details.
*/