lib/asynchronous/concurrency.rb in asynchronous-3.0.1 vs lib/asynchronous/concurrency.rb in asynchronous-4.0.0.pre

- old
+ new

@@ -1,78 +1,44 @@ -module Asynchronous +# you can use simple :c also instead of :concurrency +# remember :concurrency is all about GIL case, so +# you can modify the objects in memory +# This is ideal for little operations in simultaneously or +# when you need to update objects in the memory +class Asynchronous::Concurrency - # you can use simple :c also instead of :concurrency - # remember :concurrency is all about GIL case, so - # you can modify the objects in memory - # This is ideal for little operations in simultaneously or - # when you need to update objects in the memory - class Concurrency < Asynchronous::CleanClass + def initialize(&block) - def initialize callable - begin - @value= nil - @try_count= 0 + @rescue_state= nil + @try_count = 0 + + begin + @value= nil + @thread ||= ::Thread.new { block.call } + @rescue_state= nil + rescue ThreadError + @rescue_state ||= true + @try_count += 1 + if 3 <= @try_count + @value= block.call @rescue_state= nil - @thread ||= ::Thread.new { callable.call } - @rescue_state= nil - rescue ThreadError - @rescue_state ||= true - @try_count += 1 - if 3 <= @try_count - @value= callable.call - @rescue_state= nil - else - sleep 5 - retry - end + else + sleep 5 + retry end end - def asynchronous_get_value + end - if @value.nil? - until @rescue_state.nil? - sleep 1 - end - @value= @thread.value + def join + if @value.nil? + until @rescue_state.nil? + sleep 1 end - - return @value - + @value= @thread.value end + end - def asynchronous_set_value(obj) - @value= obj - end - alias :asynchronous_set_value= :asynchronous_set_value - - def synchronize - asynchronous_get_value - end - alias :sync :synchronize - - def method_missing(method, *args) - - new_value= asynchronous_get_value - - begin - original_value= new_value.dup - rescue ::TypeError - original_value= new_value - end - - return_value= new_value.__send__(method,*args) - unless new_value == original_value - asynchronous_set_value new_value - end - - return return_value - - end - - #def respond_to_missing?(method, include_private = false) - # value.respond_to?(method, include_private) - #end - + def value + join; @value end end