Class: Shoryuken::Helpers::AtomicBoolean

Inherits:
AtomicCounter show all
Defined in:
lib/shoryuken/helpers/atomic_boolean.rb

Overview

A thread-safe boolean implementation using AtomicCounter as base. Drop-in replacement for Concurrent::AtomicBoolean without external dependencies. Uses 1 for true and 0 for false internally.

Instance Method Summary collapse

Methods inherited from AtomicCounter

#decrement, #increment

Constructor Details

#initialize(initial_value = false) ⇒ AtomicBoolean

Initializes a new AtomicBoolean

Parameters:

  • initial_value (Boolean) (defaults to: false)

    the initial value



16
17
18
# File 'lib/shoryuken/helpers/atomic_boolean.rb', line 16

def initialize(initial_value = false)
  super(initial_value ? 1 : 0)
end

Instance Method Details

#false?Boolean

Checks if the value is false

Returns:

  • (Boolean)

    true if the value is false



53
54
55
# File 'lib/shoryuken/helpers/atomic_boolean.rb', line 53

def false?
  @mutex.synchronize { @value == 0 }
end

#make_falseBoolean

Sets the value to false

Returns:

  • (Boolean)

    false



38
39
40
41
# File 'lib/shoryuken/helpers/atomic_boolean.rb', line 38

def make_false
  @mutex.synchronize { @value = 0 }
  false
end

#make_trueBoolean

Sets the value to true

Returns:

  • (Boolean)

    true



30
31
32
33
# File 'lib/shoryuken/helpers/atomic_boolean.rb', line 30

def make_true
  @mutex.synchronize { @value = 1 }
  true
end

#true?Boolean

Checks if the value is true

Returns:

  • (Boolean)

    true if the value is true



46
47
48
# File 'lib/shoryuken/helpers/atomic_boolean.rb', line 46

def true?
  @mutex.synchronize { @value != 0 }
end

#valueBoolean

Gets the current value as a boolean

Returns:

  • (Boolean)

    the current value



23
24
25
# File 'lib/shoryuken/helpers/atomic_boolean.rb', line 23

def value
  super != 0
end