Class: Farce::Flag

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

Overview

A Ractor-shareable atomic boolean for coordinating threads and Ractors. Only true and false are accepted. Read Abstract::Flag#value when testing the flag's state. Use Abstract::Flag#compare_and_set to check and change the state in a single atomic operation.

On CRuby, subclasses must not add Ruby instance variables.

Examples:

Allowing one caller to claim work

claimed = Farce::Flag.new
workers = 4.times.map do
  Thread.new { claimed.compare_and_set(false, true) }
end
workers.count { |worker| worker.value } # => 1
claimed.value # => true

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods included from Abstract::Flag

#compare_and_set, #set, #store, #swap, #toggle, #unset, #value, #value=

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, #value

Methods included from Internal::Copyable

#duplicable?

Constructor Details

#initialize(value = false) ⇒ BasicObject

Parameters:

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

    the initial value

Raises:

  • (ArgumentError) —

    if the value is not true or false



23
24
25
26
# File 'lib/farce/flag.rb', line 23

class Flag < Internal::Flag
  include Abstract::Flag
  include Shareable::Native
end