Class: Fontisan::Utils::Future
- Inherits:
-
Object
- Object
- Fontisan::Utils::Future
- Defined in:
- lib/fontisan/utils/thread_pool.rb
Overview
Future object for async result retrieval
Represents a computation that will complete in the future. Provides blocking access to the eventual result or error.
Instance Method Summary collapse
-
#completed? ⇒ Boolean
Check if computation is complete.
-
#initialize ⇒ Future
constructor
A new instance of Future.
-
#set_error(error) ⇒ Object
Set an error.
-
#set_value(value) ⇒ Object
Set the computed value.
-
#value ⇒ Object
Get the value, blocking until available.
Constructor Details
#initialize ⇒ Future
Returns a new instance of Future.
82 83 84 85 86 87 88 |
# File 'lib/fontisan/utils/thread_pool.rb', line 82 def initialize @mutex = Mutex.new @condition = ConditionVariable.new @completed = false @value = nil @error = nil end |
Instance Method Details
#completed? ⇒ Boolean
Check if computation is complete
129 130 131 |
# File 'lib/fontisan/utils/thread_pool.rb', line 129 def completed? @mutex.synchronize { @completed } end |
#set_error(error) ⇒ Object
Set an error
104 105 106 107 108 109 110 |
# File 'lib/fontisan/utils/thread_pool.rb', line 104 def set_error(error) @mutex.synchronize do @error = error @completed = true @condition.signal end end |
#set_value(value) ⇒ Object
Set the computed value
93 94 95 96 97 98 99 |
# File 'lib/fontisan/utils/thread_pool.rb', line 93 def set_value(value) @mutex.synchronize do @value = value @completed = true @condition.signal end end |
#value ⇒ Object
Get the value, blocking until available
116 117 118 119 120 121 122 123 124 |
# File 'lib/fontisan/utils/thread_pool.rb', line 116 def value @mutex.synchronize do @condition.wait(@mutex) until @completed raise @error if @error @value end end |