Sha256: 2b5d2a5a99e4241e8d8d9fa16191d555b625d9e948a7868edf8ef12c90479c1a

Contents?: true

Size: 1.31 KB

Versions: 20

Compression:

Stored size: 1.31 KB

Contents

require "set"
require "thread"
require "monitor"

module Bunny
  module Concurrent
    # Minimalistic implementation of a synchronized fixnum value,
    # designed after (but not implementing the entire API of!)
    #
    # @note Designed to be intentionally minimalistic and only cover Bunny's needs.
    #
    # @api public
    class AtomicFixnum
      def initialize(n = 0)
        @n     = n
        @mutex = Monitor.new
      end

      def get
        @mutex.synchronize do
          @n
        end
      end

      def set(n)
        @mutex.synchronize do
          @n = n
        end
      end

      def increment
        @mutex.synchronize do
          @n = @n + 1
        end
      end
      alias inc increment
      alias increment_and_get increment

      def get_and_add(i)
        @mutex.synchronize do
          v = @n
          @n = @n + i

          v
        end
      end

      def get_and_increment
        @mutex.synchronize do
          v = @n
          @n = @n + 1

          v
        end
      end

      def decrement
        @mutex.synchronize do
          @n = @n - 1
        end
      end
      alias dec decrement
      alias decrement_and_get decrement

      def ==(m)
        @mutex.synchronize { @n == m }
      end

      def ===(v)
        @mutex.synchronize { @n === v }
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
bunny-1.0.3 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.2 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.1 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0.rc3 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0.rc2 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.8 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0.rc1 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.7 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.6 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0.pre6 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.5 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0.pre5 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.4 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.3 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0.pre4 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.2 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.1 lib/bunny/concurrent/atomic_fixnum.rb
bunny-0.10.0 lib/bunny/concurrent/atomic_fixnum.rb
bunny-1.0.0.pre3 lib/bunny/concurrent/atomic_fixnum.rb