Class: Lazy::PromiseSafe

Inherits:
Promise show all
Defined in:
lib/core/facets/lazy.rb

Overview

PromiseSafe

Thread safe version of Promise class.

Direct Known Subclasses

Future

Constant Summary

Constant Summary

Constants inherited from Promise

Lazy::Promise::DIVERGES

Instance Method Summary (collapse)

Methods inherited from Promise

#__result__, #initialize, #inspect, #method_missing, #respond_to?

Constructor Details

This class inherits a constructor from Lazy::Promise

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Lazy::Promise

Instance Method Details

- (Object) __synchronize__

:nodoc:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/core/facets/lazy.rb', line 134

def __synchronize__ #:nodoc:
  current = Thread.current

  Thread.critical = true
  unless @computation
    # fast path for evaluated thunks
    Thread.critical = false
    yield
  else
    if @owner == current
      Thread.critical = false
      raise DivergenceError.new
    end
    while @owner # spinlock
      Thread.critical = false
      Thread.pass
      Thread.critical = true
    end
    @owner = current
    Thread.critical = false

    begin
      yield
    ensure
      @owner = nil
    end
  end
end