2021-10-16 03:27:53 +02:00
|
|
|
/* See the end of this file for copyright and license terms. */
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <gay/types.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read an atom's current value.
|
|
|
|
* You usually shouldn't need this function because all the other atomic
|
|
|
|
* primitives return the value before the operation, and we are only really
|
|
|
|
* interested in how values *compare* between operations.
|
|
|
|
* Don't use `atom_read()` followed by another atomic operation, it defeats the
|
|
|
|
* whole purpose of using atomics in the first place.
|
|
|
|
*
|
|
|
|
* @param atom Atom to read the value of
|
|
|
|
* @return The atom's "current" value (at the time of reading it)
|
|
|
|
*/
|
|
|
|
inline int atom_read(const atom_t *atom)
|
|
|
|
{
|
|
|
|
return atom->_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write a new value to an atom.
|
|
|
|
*
|
|
|
|
* @param atom Atom to write to
|
|
|
|
* @param val New value
|
|
|
|
* @return The value of `atom` *before* the operation
|
|
|
|
*/
|
|
|
|
inline int atom_write(atom_t *atom, int val)
|
|
|
|
{
|
|
|
|
int eax;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
2021-10-16 18:39:03 +02:00
|
|
|
" mov (%2), %0 \n" /* eax = atom->_value */
|
2021-10-16 03:27:53 +02:00
|
|
|
"1: lock \n"
|
2021-10-16 18:39:03 +02:00
|
|
|
" cmpxchgl %1, (%2) \n" /* if (atom->_value == eax) atom->_value = val */
|
2021-10-21 22:12:37 +02:00
|
|
|
" pause \n" /* intel says you're supposed to do this in spin loops */
|
2021-10-16 18:39:03 +02:00
|
|
|
" jne 1b \n" /* else goto 1 (eax updated to new atom->_value) */
|
2021-10-16 03:27:53 +02:00
|
|
|
: "=a"(eax)
|
|
|
|
: "r"(val), "r"(&atom->_value)
|
|
|
|
: "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
return eax;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
inline int atom_add(atom_t *atom, int val)
|
|
|
|
{
|
|
|
|
__asm__ volatile(
|
|
|
|
" lock \n"
|
|
|
|
" xaddl %0, (%1) \n"
|
|
|
|
: "+r"(val)
|
|
|
|
: "r"(&atom->_value)
|
|
|
|
: "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Perform an atomic load/subtract/store.
|
|
|
|
*
|
|
|
|
* @param atom Atom to subtract from
|
|
|
|
* @param val Value to subtract
|
|
|
|
* @return The value of `atom` *before* the operation
|
|
|
|
*/
|
|
|
|
inline int atom_sub(atom_t *atom, int val)
|
|
|
|
{
|
|
|
|
return atom_add(atom, -val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Increment an atom by one.
|
|
|
|
*
|
|
|
|
* @param atom Atom to increment
|
|
|
|
* @return `true` if the vale *after* the operation is nonzero
|
|
|
|
*/
|
|
|
|
inline bool atom_inc(atom_t *atom)
|
|
|
|
{
|
|
|
|
bool nonzero = false;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
" lock \n"
|
|
|
|
" incl (%1) \n"
|
|
|
|
" setne %0 \n"
|
|
|
|
: "+r"(nonzero) /* read+write to ensure the initial value isn't optimized out */
|
|
|
|
: "r"(&atom->_value)
|
|
|
|
: "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
return nonzero;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Decrement an atom by one.
|
|
|
|
*
|
|
|
|
* @param atom Atom to decrement
|
|
|
|
* @return `true` if the value *after* the operation is nonzero
|
|
|
|
*/
|
|
|
|
inline bool atom_dec(atom_t *atom)
|
|
|
|
{
|
|
|
|
bool nonzero = false;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
2021-10-16 18:39:03 +02:00
|
|
|
" lock \n"
|
|
|
|
" decl (%1) \n"
|
|
|
|
" setne %0 \n"
|
2021-10-21 22:12:37 +02:00
|
|
|
: "+r"(nonzero) /* read+write to ensure the initializer isn't optimized out */
|
2021-10-16 03:27:53 +02:00
|
|
|
: "r"(&atom->_value)
|
|
|
|
: "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
return nonzero;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Perform an atomic bitwise AND and write the result back to the atom.
|
|
|
|
*
|
|
|
|
* @param atom Atom to perform the AND on
|
|
|
|
* @param val Value to AND with
|
|
|
|
* @return The value of `atom` *before* the operation
|
|
|
|
*/
|
|
|
|
inline int atom_and(atom_t *atom, int val)
|
|
|
|
{
|
|
|
|
int eax;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
" movl (%2), %0 \n" /* eax = atom->_value */
|
|
|
|
"1: andl %0, %1 \n" /* val &= eax */
|
|
|
|
" lock \n"
|
|
|
|
" cmpxchgl %1, (%2) \n" /* if (atom->_value == eax) atom->_value = val */
|
2021-10-21 22:12:37 +02:00
|
|
|
" pause \n" /* intel says you're supposed to do this in spin loops */
|
2021-10-16 18:39:03 +02:00
|
|
|
" jne 1b \n" /* else goto 1 (eax updated to new atom->_value) */
|
2021-10-16 03:27:53 +02:00
|
|
|
: "=a"(eax), "+r"(val)
|
|
|
|
: "r"(&atom->_value)
|
|
|
|
: "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
return eax;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Perform an atomic bitwise OR and write the result back to the atom.
|
|
|
|
*
|
|
|
|
* @param atom Atom to perform the OR on
|
|
|
|
* @param val Value to OR with
|
|
|
|
* @return The value of `atom` *before* the operation
|
|
|
|
*/
|
|
|
|
inline int atom_or(atom_t *atom, int val)
|
|
|
|
{
|
|
|
|
int eax;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
" movl (%2), %0 \n" /* eax = atom->_value */
|
2021-10-16 21:30:13 +02:00
|
|
|
"1: orl %0, %1 \n" /* val |= eax */
|
2021-10-16 03:27:53 +02:00
|
|
|
" lock \n"
|
|
|
|
" cmpxchgl %1, (%2) \n" /* if (atom->_value == eax) atom->_value = eax */
|
2021-10-21 22:12:37 +02:00
|
|
|
" pause \n" /* intel says you're supposed to do this in spin loops */
|
2021-10-16 18:39:03 +02:00
|
|
|
" jne 1b \n" /* else goto 1 (eax updated to new atom->_value) */
|
2021-10-16 03:27:53 +02:00
|
|
|
: "=a"(eax), "+r"(val)
|
|
|
|
: "r"(&atom->_value)
|
|
|
|
: "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
return eax;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Perform an atomic bitwise XOR and write the result back to the atom.
|
|
|
|
*
|
|
|
|
* @param atom Atom to perform the XOR on
|
|
|
|
* @param val Value to XOR with
|
|
|
|
* @return The value of `atom` *before* the operation
|
|
|
|
*/
|
|
|
|
inline int atom_xor(atom_t *atom, int val)
|
|
|
|
{
|
|
|
|
int eax;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
" movl (%2), %0 \n" /* eax = atom->_value */
|
2021-10-16 18:39:03 +02:00
|
|
|
"1: xorl %0, %1 \n" /* val ^= eax */
|
2021-10-16 03:27:53 +02:00
|
|
|
" lock \n"
|
|
|
|
" cmpxchgl %1, (%2) \n" /* if (atom->_value == eax) atom->_value = eax */
|
2021-10-21 22:12:37 +02:00
|
|
|
" pause \n" /* intel says you're supposed to do this in spin loops */
|
2021-10-16 18:39:03 +02:00
|
|
|
" jne 1b \n" /* else goto 1 (eax updated to new atom->_value) */
|
2021-10-16 03:27:53 +02:00
|
|
|
: "=a"(eax), "+r"(val)
|
|
|
|
: "r"(&atom->_value)
|
|
|
|
: "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
return eax;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Atomically set a bit.
|
|
|
|
*
|
|
|
|
* @param atom Atom to set a bit of
|
|
|
|
* @param pos Bit position (starting from 0 for the LSB)
|
|
|
|
* @return `true` the bit was clear *before* the operation
|
|
|
|
*/
|
2021-10-16 18:39:03 +02:00
|
|
|
inline bool atom_set_bit(atom_t *atom, int pos)
|
2021-10-16 03:27:53 +02:00
|
|
|
{
|
|
|
|
int mask = 1 << pos;
|
|
|
|
int oldval = atom_or(atom, mask);
|
|
|
|
return (oldval & mask) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Atomically clear a bit.
|
|
|
|
*
|
|
|
|
* @param atom Atom to clear a bit of
|
|
|
|
* @param pos Bit position (starting from 0 for the LSB)
|
|
|
|
* @return `true` if the bit was set *before* the operation
|
|
|
|
*/
|
2021-10-16 18:39:03 +02:00
|
|
|
inline bool atom_clr_bit(atom_t *atom, int pos)
|
2021-10-16 03:27:53 +02:00
|
|
|
{
|
|
|
|
int mask = 1 << pos;
|
|
|
|
int oldval = atom_and(atom, ~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.
|
|
|
|
*/
|