struct Atomic(T)
Overview
A value that may be updated atomically.
If T is a non-union primitive integer type or enum type, all operations are
supported. If T is a reference type, or a union type containing only
reference types or Nil, then only #compare_and_set, #swap, #set,
#lazy_set, #get, and #lazy_get are available.
Defined in:
atomic.crConstructors
- 
        .new(value : T)
        
          
Creates an Atomic with the given initial value.
 
Macro Summary
- 
        fence(ordering = :sequentially_consistent)
        
          
Adds an explicit memory barrier with the specified memory order guarantee.
 
Instance Method Summary
- 
        #add(value : T, ordering : Ordering = :sequentially_consistent) : T
        
          
Performs
atomic_value &+= value. - 
        #and(value : T, ordering : Ordering = :sequentially_consistent) : T
        
          
Performs
atomic_value &= value. - 
        #compare_and_set(cmp : T, new : T, success_ordering : Ordering, failure_ordering : Ordering) : Tuple(T, Bool)
        
          
Compares this atomic's value with cmp using explicit memory orderings:
 - 
        #compare_and_set(cmp : T, new : T) : Tuple(T, Bool)
        
          
Compares this atomic's value with cmp:
 - 
        #get(ordering : Ordering = :sequentially_consistent) : T
        
          
Atomically returns this atomic's value.
 - 
        #lazy_get
        
          
Non-atomically returns this atomic's value.
 - 
        #lazy_set(value : T) : T
        
          
Non-atomically sets this atomic's value to value.
 - 
        #max(value : T, ordering : Ordering = :sequentially_consistent)
        
          
Performs
atomic_value = {atomic_value, value}.max. - 
        #min(value : T, ordering : Ordering = :sequentially_consistent)
        
          
Performs
atomic_value = {atomic_value, value}.min. - 
        #nand(value : T, ordering : Ordering = :sequentially_consistent) : T
        
          
Performs
atomic_value = ~(atomic_value & value). - 
        #or(value : T, ordering : Ordering = :sequentially_consistent) : T
        
          
Performs
atomic_value |= value. - 
        #set(value : T, ordering : Ordering = :sequentially_consistent) : T
        
          
Atomically sets this atomic's value to value.
 - 
        #sub(value : T, ordering : Ordering = :sequentially_consistent) : T
        
          
Performs
atomic_value &-= value. - 
        #swap(value : T, ordering : Ordering = :sequentially_consistent)
        
          
Atomically sets this atomic's value to value.
 - 
        #xor(value : T, ordering : Ordering = :sequentially_consistent) : T
        
          
Performs
atomic_value ^= value. 
Instance methods inherited from struct Struct
  
  
    
      ==(other) : Bool
    ==, 
    
  
    
      hash(hasher)
    hash, 
    
  
    
      inspect(io : IO) : Nil
    inspect, 
    
  
    
      pretty_print(pp) : Nil
    pretty_print, 
    
  
    
      to_s(io : IO) : Nil
    to_s
    
  
    
    
    
  
    
  Instance methods inherited from struct Value
  
  
    
      ==(other : JSON::Any)==(other : YAML::Any)
==(other) ==, dup dup
Instance methods inherited from class Object
  
  
    
      ! : Bool
    !, 
    
  
    
      !=(other)
    !=, 
    
  
    
      !~(other)
    !~, 
    
  
    
      ==(other)
    ==, 
    
  
    
      ===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash, in?(collection : Object) : Bool
in?(*values : Object) : Bool in?, inspect(io : IO) : Nil
inspect : String inspect, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String to_json, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil to_pretty_json, to_s(io : IO) : Nil
to_s : String to_s, to_yaml(io : IO) : Nil
to_yaml : String to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as
Class methods inherited from class Object
  
  
    
      from_json(string_or_io, root : String)from_json(string_or_io) from_json, from_yaml(string_or_io : String | IO) from_yaml
Macros inherited from class Object
  
  
    
      class_getter(*names, &block)
    class_getter, 
    
  
    
      class_getter!(*names)
    class_getter!, 
    
  
    
      class_getter?(*names, &block)
    class_getter?, 
    
  
    
      class_property(*names, &block)
    class_property, 
    
  
    
      class_property!(*names)
    class_property!, 
    
  
    
      class_property?(*names, &block)
    class_property?, 
    
  
    
      class_setter(*names)
    class_setter, 
    
  
    
      def_clone
    def_clone, 
    
  
    
      def_equals(*fields)
    def_equals, 
    
  
    
      def_equals_and_hash(*fields)
    def_equals_and_hash, 
    
  
    
      def_hash(*fields)
    def_hash, 
    
  
    
      delegate(*methods, to object)
    delegate, 
    
  
    
      forward_missing_to(delegate)
    forward_missing_to, 
    
  
    
      getter(*names, &block)
    getter, 
    
  
    
      getter!(*names)
    getter!, 
    
  
    
      getter?(*names, &block)
    getter?, 
    
  
    
      property(*names, &block)
    property, 
    
  
    
      property!(*names)
    property!, 
    
  
    
      property?(*names, &block)
    property?, 
    
  
    
      setter(*names)
    setter
    
  
  
Constructor Detail
Macro Detail
Adds an explicit memory barrier with the specified memory order guarantee.
Atomics on weakly-ordered CPUs (e.g. ARM32) may not guarantee memory order of other memory accesses, and an explicit memory barrier is thus required.
Notes:
- X86 is strongly-ordered and trying to add a fence should be a NOOP;
 - AArch64 guarantees memory order and doesn't need explicit fences in addition to the atomics (but may need barriers in other cases).
 
Instance Method Detail
Performs atomic_value &+= value. Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(1)
atomic.add(2) # => 1
atomic.get    # => 3
        Performs atomic_value &= value. Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(5)
atomic.and(3) # => 5
atomic.get    # => 1
        Compares this atomic's value with cmp using explicit memory orderings:
- if they are equal, sets the value to new, and returns 
{old_value, true} - if they are not equal the value remains the same, and returns 
{old_value, false} 
Reference types are compared by #same?, not #==.
atomic = Atomic.new(0_u32)
value = atomic.get(:acquire)
loop do
  value, success = atomic.compare_and_set(value, value &+ 1, :acquire_release, :acquire)
  break if success
end
        Compares this atomic's value with cmp:
- if they are equal, sets the value to new, and returns 
{old_value, true} - if they are not equal the value remains the same, and returns 
{old_value, false} 
Reference types are compared by #same?, not #==.
atomic = Atomic.new(1)
atomic.compare_and_set(2, 3) # => {1, false}
atomic.get                   # => 1
atomic.compare_and_set(1, 3) # => {1, true}
atomic.get                   # => 3
        Atomically returns this atomic's value.
Non-atomically returns this atomic's value.
NOTE use with caution, this may break atomic guarantees.
Non-atomically sets this atomic's value to value. Returns the new value.
atomic = Atomic.new(5)
atomic.lazy_set(10) # => 10
atomic.get          # => 10
NOTE use with caution, this may break atomic guarantees.
Performs atomic_value = {atomic_value, value}.max. Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(5)
atomic.max(3) # => 5
atomic.get    # => 5
atomic.max(10) # => 5
atomic.get     # => 10
        Performs atomic_value = {atomic_value, value}.min. Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(5)
atomic.min(10) # => 5
atomic.get     # => 5
atomic.min(3) # => 5
atomic.get    # => 3
        Performs atomic_value = ~(atomic_value & value). Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(5)
atomic.nand(3) # => 5
atomic.get     # => -2
        Performs atomic_value |= value. Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(5)
atomic.or(2) # => 5
atomic.get   # => 7
        Atomically sets this atomic's value to value. Returns the new value.
atomic = Atomic.new(5)
atomic.set(10) # => 10
atomic.get     # => 10
        Performs atomic_value &-= value. Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(9)
atomic.sub(2) # => 9
atomic.get    # => 7
        Atomically sets this atomic's value to value. Returns the old value.
atomic = Atomic.new(5)
atomic.swap(10) # => 5
atomic.get      # => 10
        Performs atomic_value ^= value. Returns the old value.
T cannot contain any reference types.
atomic = Atomic.new(5)
atomic.xor(3) # => 5
atomic.get    # => 6