lib/i18n/tasks/concurrent/cached_value.rb in i18n-tasks-0.9.25 vs lib/i18n/tasks/concurrent/cached_value.rb in i18n-tasks-0.9.26
- old
+ new
@@ -16,12 +16,44 @@
@result = NULL
end
# @return [Object] Result of the computation.
def get
- return @result unless @result == NULL
+ return get_result_volatile unless get_result_volatile == NULL
@mutex.synchronize do
- @result = @computation.call
+ next unless get_result_volatile == NULL
+ set_result_volatile @computation.call
+ @computation = nil
end
+ get_result_volatile
end
+
+ private
+
+ # Ruby instance variable volatility is currently unspecified:
+ # https://bugs.ruby-lang.org/issues/11539
+ #
+ # Below are the implementations for major ruby engines, based on concurrent-ruby.
+ # rubocop:disable Lint/DuplicateMethods,Naming/AccessorMethodName
+ case RUBY_ENGINE
+ when 'rbx'
+ def get_result_volatile
+ Rubinius.memory_barrier
+ @result
+ end
+
+ def set_result_volatile(value)
+ @result = value
+ Rubinius.memory_barrier
+ end
+ else
+ def get_result_volatile
+ @result
+ end
+
+ def set_result_volatile(value)
+ @result = value
+ end
+ end
+ # rubocop:enable Lint/DuplicateMethods,Naming/AccessorMethodName
end
end