Sha256: 026329ec17becc3b39858ce800f2937a72c3a370a1e3e5589752ebdad01c1b6e

Contents?: true

Size: 645 Bytes

Versions: 15

Compression:

Stored size: 645 Bytes

Contents

require 'thread' # Mutex

# A [LazySingleton] wraps the result of the provided block,
# which is guaranteed to be called at-most-once, even if the
# block's return value is nil.
class ::LogStash::Util::LazySingleton

  def initialize(&block)
    @mutex = Mutex.new
    @block = block
    @instantiated = false
  end

  def instance
    unless @instantiated
      @mutex.synchronize do
        unless @instantiated
          @instance = @block.call
          @instantiated = true
        end
      end
    end

    return @instance
  end

  def reset!
    @mutex.synchronize do
      @instantiated = false
      @instance = nil
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
logstash-core-6.8.23-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.22-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.21-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.20-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.19-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.18-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.17-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.16-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.15-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.14-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.13-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.12-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.11-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.10-java lib/logstash/util/lazy_singleton.rb
logstash-core-6.8.9-java lib/logstash/util/lazy_singleton.rb