Class: Garcon::MutexAtomicBoolean

Inherits:
Object
  • Object
show all
Defined in:
lib/garcon/task/atomic_boolean.rb

Overview

A boolean value that can be updated atomically. Reads and writes to an atomic boolean and thread-safe and guaranteed to succeed. Reads and writes may block briefly but no explicit locking is required.

Direct Known Subclasses

AtomicBoolean

Instance Method Summary collapse

Constructor Details

#initialize(initial = false) ⇒ MutexAtomicBoolean

Creates a new ‘AtomicBoolean` with the given initial value.

Parameters:

  • initial (Boolean) (defaults to: false)

    the initial value



34
35
36
37
# File 'lib/garcon/task/atomic_boolean.rb', line 34

def initialize(initial = false)
  @value = !!initial
  @mutex = Mutex.new
end

Instance Method Details

#false?Boolean

Is the current value ‘false`

Returns:

  • (Boolean)

    True if the current value is ‘false`, else false



88
89
90
91
92
93
# File 'lib/garcon/task/atomic_boolean.rb', line 88

def false?
  @mutex.lock
  !@value
ensure
  @mutex.unlock
end

#make_falseBoolean

Explicitly sets the value to false.

Returns:

  • (Boolean)

    True is value has changed, otherwise false



116
117
118
119
120
121
122
123
# File 'lib/garcon/task/atomic_boolean.rb', line 116

def make_false
  @mutex.lock
  old = @value
  @value = false
  old
ensure
  @mutex.unlock
end

#make_trueBoolean

Explicitly sets the value to true.

Returns:

  • (Boolean)

    True is value has changed, otherwise false



101
102
103
104
105
106
107
108
# File 'lib/garcon/task/atomic_boolean.rb', line 101

def make_true
  @mutex.lock
  old = @value
  @value = true
  !old
ensure
  @mutex.unlock
end

#true?Boolean

Is the current value ‘true`

Returns:

  • (Boolean)

    True if the current value is ‘true`, else false



75
76
77
78
79
80
# File 'lib/garcon/task/atomic_boolean.rb', line 75

def true?
  @mutex.lock
  @value
ensure
  @mutex.unlock
end

#valueBoolean

Retrieves the current ‘Boolean` value.

Returns:



45
46
47
48
49
50
# File 'lib/garcon/task/atomic_boolean.rb', line 45

def value
  @mutex.lock
  @value
ensure
  @mutex.unlock
end

#value=(value) ⇒ Boolean

Explicitly sets the value.

Parameters:

  • value (Boolean)

    the new value to be set

Returns:



61
62
63
64
65
66
67
# File 'lib/garcon/task/atomic_boolean.rb', line 61

def value=(value)
  @mutex.lock
  @value = !!value
  @value
ensure
  @mutex.unlock
end