Sha256: dd8b7cf1c97876b4ef12a3adfffb663579abee0f4e8fbcace1ed9a34c54dc38a
Contents?: true
Size: 1.5 KB
Versions: 8
Compression:
Stored size: 1.5 KB
Contents
# frozen_string_literal: true require 'i18n/tasks/concurrent/cached_value' module I18n::Tasks::Concurrent # A thread-safe memoized value. # The given computation is guaranteed to be invoked at most once. # @since 0.9.25 class CachedValue NULL = Object.new # @param [Proc] computation The computation that returns the value to cache. def initialize(&computation) @computation = computation @mutex = Mutex.new @result = NULL end # @return [Object] Result of the computation. def get return get_result_volatile unless get_result_volatile == NULL @mutex.synchronize do 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
Version data entries
8 entries across 8 versions & 1 rubygems