Class: Farce::Local::Flag

Inherits:
Object
  • Object
show all
Includes:
Abstract::Flag, Shareable::Tracked
Defined in:
lib/farce/local/flag.rb

Overview

A shareable atomic boolean with an independent value in each scope. Only true and false are accepted. Read #value to test its state.

Instance Attribute Summary

Attributes included from Scoped

#scope

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods included from Internal::ValueSerialization

#as_extended_json, #as_json, #to_bson, #to_bson_normalized_value, #to_cbor, #to_json, #to_msgpack

Methods included from Abstract::Value

#blank?, #unwrap

Methods included from Internal::Copyable

#duplicable?

Constructor Details

#initialize(value = false, scope: :ractor) ⇒ BasicObject

Valid scopes are:

  • :ractor - The value is shared by all threads within the same Ractor.
  • :thread_group - The value is shared by all threads within the same thread group.
  • :thread - The value is shared by all fibers within the same thread.
  • :fiber_storage - The value is shared by all fibers using the same storage. Unless explicitly specified, storage is inherited from the parent fiber for blocking fibers, but not for non-blocking fibers (such as those created by Scheduler#schedule).
  • :fiber - Each fiber has its own independent value.

Parameters:

  • value (Boolean) (defaults to: false) —

    the initial value

  • scope (Symbol) (defaults to: :ractor) —

    the scope of the flag

Raises:

  • (ArgumentError) —

    if the value is not true or false



# File 'lib/farce/local/flag.rb', line 14

Instance Method Details

#compare_and_set(expected, replacement) ⇒ Boolean

Replace the value atomically if it matches the expected boolean.

Parameters:

  • expected (Boolean) —

    the expected value

  • replacement (Boolean) —

    the replacement value

Returns:

  • (Boolean) —

    whether the value changed



49
# File 'lib/farce/local/flag.rb', line 49

def compare_and_set(expected, replacement) = scoped_value.compare_and_set(expected, replacement)

#set ⇒ true

Set the flag to true.

Returns:

  • (true)


27
# File 'lib/farce/local/flag.rb', line 27

def set = scoped_value.set

#store(value) ⇒ Boolean Also known as: value=

Store a boolean value.

Parameters:

  • value (Boolean) —

    the new value

Returns:

  • (Boolean) —

    the new value



36
# File 'lib/farce/local/flag.rb', line 36

def store(value) = scoped_value.store(value)

#swap(value) ⇒ Boolean

Replace the value and return its previous state.

Parameters:

  • value (Boolean) —

    the new value

Returns:

  • (Boolean) —

    the previous value



43
# File 'lib/farce/local/flag.rb', line 43

def swap(value) = scoped_value.swap(value)

#toggle ⇒ Boolean

Atomically invert the value.

Returns:

  • (Boolean) —

    the new value



53
# File 'lib/farce/local/flag.rb', line 53

def toggle = scoped_value.toggle

#unset ⇒ false

Set the flag to false.

Returns:

  • (false)


31
# File 'lib/farce/local/flag.rb', line 31

def unset = scoped_value.unset

#value ⇒ Boolean Also known as: get

Returns the current value.

Returns:

  • (Boolean) —

    the current value



21
# File 'lib/farce/local/flag.rb', line 21

def value = scoped_value.value