Class: Fontisan::Utils::Future

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

future = Future.new
Thread.new { future.set_value(42) }
result = future.value  # Blocks until value is set

Instance Method Summary collapse

Constructor Details

#initializeFuture

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

Returns:

  • (Boolean)

    True if 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

Parameters:

  • error (Exception)

    Error that occurred



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

Parameters:

  • value (Object)

    Result 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

#valueObject

Get the value, blocking until available

Returns:

  • (Object)

    Computed value

Raises:

  • (Exception)

    If computation failed



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