Sha256: 653c79fda5770cc32e5c7008a156ba8149785e882f37086ef87495e89bf79ae8

Contents?: true

Size: 480 Bytes

Versions: 5

Compression:

Stored size: 480 Bytes

Contents

module Agent
  class Once
    def initialize
      @mutex     = Mutex.new
      @performed = false
    end

    def perform
      # optimium path
      return nil, error if @performed

      # slow path
      @mutex.synchronize do
        return nil, error if @performed
        @performed = true
      end

      return yield, nil
    end

    def performed?
      @performed
    end

  protected

    def error
      @error ||= Error.new("already performed")
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
agent-0.12.0 lib/agent/once.rb
agent-0.11.0 lib/agent/once.rb
agent-0.10.0 lib/agent/once.rb
agent-0.9.1 lib/agent/once.rb
agent-0.9.0 lib/agent/once.rb