Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)
|
Atomic operations. More...
#include "ruby/internal/config.h"
#include "ruby/assert.h"
#include "ruby/backward/2/limits.h"
#include "ruby/internal/attr/artificial.h"
#include "ruby/internal/attr/noalias.h"
#include "ruby/internal/attr/nonnull.h"
#include "ruby/internal/compiler_since.h"
#include "ruby/internal/cast.h"
#include "ruby/internal/value.h"
#include "ruby/internal/static_assert.h"
#include "ruby/internal/stdbool.h"
Go to the source code of this file.
Macros | |
#define | RUBY_ATOMIC_GENERIC_MACRO 1 |
#define | RUBY_ATOMIC_FETCH_ADD(var, val) |
Atomically replaces the value pointed by var with the result of addition of val to the old value of var. | |
#define | RUBY_ATOMIC_FETCH_SUB(var, val) |
Atomically replaces the value pointed by var with the result of subtraction of val to the old value of var. | |
#define | RUBY_ATOMIC_OR(var, val) |
Atomically replaces the value pointed by var with the result of bitwise OR between val and the old value of var. | |
#define | RUBY_ATOMIC_EXCHANGE(var, val) |
Atomically replaces the value pointed by var with val. | |
#define | RUBY_ATOMIC_CAS(var, oldval, newval) |
Atomic compare-and-swap. | |
#define | RUBY_ATOMIC_LOAD(var) |
Atomic load. | |
#define | RUBY_ATOMIC_SET(var, val) |
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type. | |
#define | RUBY_ATOMIC_ADD(var, val) |
Identical to RUBY_ATOMIC_FETCH_ADD, except for the return type. | |
#define | RUBY_ATOMIC_SUB(var, val) |
Identical to RUBY_ATOMIC_FETCH_SUB, except for the return type. | |
#define | RUBY_ATOMIC_INC(var) |
Atomically increments the value pointed by var. | |
#define | RUBY_ATOMIC_DEC(var) |
Atomically decrements the value pointed by var. | |
#define | RUBY_ATOMIC_SIZE_INC(var) |
Identical to RUBY_ATOMIC_INC, except it expects its argument is size_t. | |
#define | RUBY_ATOMIC_SIZE_DEC(var) |
Identical to RUBY_ATOMIC_DEC, except it expects its argument is size_t. | |
#define | RUBY_ATOMIC_SIZE_EXCHANGE(var, val) |
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are size_t. | |
#define | RUBY_ATOMIC_SIZE_CAS(var, oldval, newval) |
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are size_t. | |
#define | RUBY_ATOMIC_SIZE_ADD(var, val) |
Identical to RUBY_ATOMIC_ADD, except it expects its arguments are size_t. | |
#define | RUBY_ATOMIC_SIZE_SUB(var, val) |
Identical to RUBY_ATOMIC_SUB, except it expects its arguments are size_t. | |
#define | RUBY_ATOMIC_PTR_EXCHANGE(var, val) |
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are void*. | |
#define | RUBY_ATOMIC_PTR_LOAD(var) |
Identical to RUBY_ATOMIC_LOAD, except it expects its arguments are void*. | |
#define | RUBY_ATOMIC_PTR_CAS(var, oldval, newval) |
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are void*. | |
#define | RUBY_ATOMIC_VALUE_EXCHANGE(var, val) |
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are VALUE. | |
#define | RUBY_ATOMIC_VALUE_CAS(var, oldval, newval) |
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE. |
Typedefs | |
using | rb_atomic_t = std::atomic<unsigned> |
Type that is eligible for atomic operations. |
Atomic operations.
Basically, if we could assume either C11 or C++11, these macros are just redundant. Sadly we cannot. We have to do them ourselves.
Definition in file atomic.h.
#define RUBY_ATOMIC_ADD | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_FETCH_ADD, except for the return type.
var | A variable of rb_atomic_t. |
val | Value to add. |
#define RUBY_ATOMIC_CAS | ( | var, | |
oldval, | |||
newval ) |
Atomic compare-and-swap.
This stores val to var if and only if the assignment changes the value of var from oldval to newval. You can detect whether the assignment happened or not using the return value.
var | A variable of rb_atomic_t. |
oldval | Expected value of var before the assignment. |
newval | What you want to store at var. |
oldval | Successful assignment (var is now newval). |
otherwise | Something else is at var; not updated. |
#define RUBY_ATOMIC_DEC | ( | var | ) |
Atomically decrements the value pointed by var.
var | A variable of rb_atomic_t. |
#define RUBY_ATOMIC_EXCHANGE | ( | var, | |
val ) |
Atomically replaces the value pointed by var with val.
This is just an assignment, but you can additionally know the previous value.
var | A variable of rb_atomic_t. |
val | Value to set. |
#define RUBY_ATOMIC_FETCH_ADD | ( | var, | |
val ) |
Atomically replaces the value pointed by var with the result of addition of val to the old value of var.
var | A variable of rb_atomic_t. |
val | Value to add. |
#define RUBY_ATOMIC_FETCH_SUB | ( | var, | |
val ) |
Atomically replaces the value pointed by var with the result of subtraction of val to the old value of var.
var | A variable of rb_atomic_t. |
val | Value to subtract. |
#define RUBY_ATOMIC_INC | ( | var | ) |
Atomically increments the value pointed by var.
var | A variable of rb_atomic_t. |
#define RUBY_ATOMIC_LOAD | ( | var | ) |
Atomic load.
This loads var with an atomic intrinsic and returns its value.
var | A variable of rb_atomic_t |
#define RUBY_ATOMIC_OR | ( | var, | |
val ) |
Atomically replaces the value pointed by var with the result of bitwise OR between val and the old value of var.
var | A variable of rb_atomic_t. |
val | Value to mix. |
Definition at line 116 of file atomic.h.
Referenced by rb_postponed_job_trigger().
#define RUBY_ATOMIC_PTR_CAS | ( | var, | |
oldval, | |||
newval ) |
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are void*.
There are cases where rb_atomic_t is 32bit while void* is 64bit. This should be used for size related operations to support such platforms.
var | A variable of void*. |
oldval | Expected value of var before the assignment. |
newval | What you want to store at var. |
oldval | Successful assignment (var is now newval). |
otherwise | Something else is at var; not updated. |
Definition at line 315 of file atomic.h.
Referenced by rb_postponed_job_preregister().
#define RUBY_ATOMIC_PTR_EXCHANGE | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are void*.
There are cases where rb_atomic_t is 32bit while void* is 64bit. This should be used for pointer related operations to support such platforms.
var | A variable of void *. |
val | Value to set. |
Definition at line 290 of file atomic.h.
Referenced by rb_postponed_job_preregister().
#define RUBY_ATOMIC_PTR_LOAD | ( | var | ) |
Identical to RUBY_ATOMIC_LOAD, except it expects its arguments are void*.
There are cases where rb_atomic_t is 32bit while void* is 64bit. This should be used for size related operations to support such platforms.
var | A variable of void* |
#define RUBY_ATOMIC_SET | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type.
var | A variable of rb_atomic_t. |
val | Value to set. |
#define RUBY_ATOMIC_SIZE_ADD | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_ADD, except it expects its arguments are size_t.
There are cases where rb_atomic_t is 32bit while size_t is 64bit. This should be used for size related operations to support such platforms.
var | A variable of size_t. |
val | Value to add. |
#define RUBY_ATOMIC_SIZE_CAS | ( | var, | |
oldval, | |||
newval ) |
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are size_t.
There are cases where rb_atomic_t is 32bit while size_t is 64bit. This should be used for size related operations to support such platforms.
var | A variable of size_t. |
oldval | Expected value of var before the assignment. |
newval | What you want to store at var. |
oldval | Successful assignment (var is now newval). |
otherwise | Something else is at var; not updated. |
#define RUBY_ATOMIC_SIZE_DEC | ( | var | ) |
Identical to RUBY_ATOMIC_DEC, except it expects its argument is size_t.
There are cases where rb_atomic_t is 32bit while size_t is 64bit. This should be used for size related operations to support such platforms.
var | A variable of size_t. |
#define RUBY_ATOMIC_SIZE_EXCHANGE | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are size_t.
There are cases where rb_atomic_t is 32bit while size_t is 64bit. This should be used for size related operations to support such platforms.
var | A variable of size_t. |
val | Value to set. |
#define RUBY_ATOMIC_SIZE_INC | ( | var | ) |
Identical to RUBY_ATOMIC_INC, except it expects its argument is size_t.
There are cases where rb_atomic_t is 32bit while size_t is 64bit. This should be used for size related operations to support such platforms.
var | A variable of size_t. |
#define RUBY_ATOMIC_SIZE_SUB | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_SUB, except it expects its arguments are size_t.
There are cases where rb_atomic_t is 32bit while size_t is 64bit. This should be used for size related operations to support such platforms.
var | A variable of size_t. |
val | Value to subtract. |
#define RUBY_ATOMIC_SUB | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_FETCH_SUB, except for the return type.
var | A variable of rb_atomic_t. |
val | Value to subtract. |
#define RUBY_ATOMIC_VALUE_CAS | ( | var, | |
oldval, | |||
newval ) |
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
There are cases where rb_atomic_t is 32bit while VALUE is 64bit. This should be used for size related operations to support such platforms.
var | A variable of void*. |
oldval | Expected value of var before the assignment. |
newval | What you want to store at var. |
oldval | Successful assignment (var is now newval). |
otherwise | Something else is at var; not updated. |
#define RUBY_ATOMIC_VALUE_EXCHANGE | ( | var, | |
val ) |
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are VALUE.
There are cases where rb_atomic_t is 32bit while VALUE is 64bit. This should be used for pointer related operations to support such platforms.
var | A variable of VALUE. |
val | Value to set. |
using rb_atomic_t = std::atomic<unsigned> |