Sha256: bbb61855dd5bdf8d73660e5747dd2cde95afa8cee3d4fc2bc6da324de66e9c10

Contents?: true

Size: 830 Bytes

Versions: 3

Compression:

Stored size: 830 Bytes

Contents

module Spectator
  # Thread safe number operations
  class AtomicNumber
    def initialize(init)
      @value = init
      @lock = Mutex.new
    end

    def set(value)
      @lock.synchronize { @value = value }
    end

    def get
      @lock.synchronize { @value }
    end

    def get_and_set(value)
      @lock.synchronize do
        tmp = @value
        @value = value
        tmp
      end
    end

    def get_and_add(amount)
      @lock.synchronize do
        tmp = @value
        @value += amount
        tmp
      end
    end

    def add_and_get(amount)
      @lock.synchronize { @value += amount }
    end

    def max(value)
      v = value.to_f
      @lock.synchronize do
        @value = v if v > @value || @value.nan?
      end
      @value
    end

    def to_s
      "AtomicNumber{#{@value}}"
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
netflix-spectator-rb-0.1.3 lib/spectator/atomic_number.rb
netflix-spectator-rb-0.1.1 lib/spectator/atomic_number.rb
spectator-rb-0.1.0 lib/spectator/atomic_number.rb