Sha256: cd7c024b3b86b0578b8bbdcaf2047892957c50b460582e5e6050f35a72f02e2a

Contents?: true

Size: 1.32 KB

Versions: 28

Compression:

Stored size: 1.32 KB

Contents

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

module GorgonBunny
  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 GorgonBunny'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

28 entries across 28 versions & 1 rubygems

Version Path
gorgon-0.11.1 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.11.0 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.10.5 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.10.4 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.10.3 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.10.2 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.10.1 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.10.0 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.9.0 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.8.4 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.8.3 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.8.2 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.8.1 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.8.0 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.7.1 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.7.0 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.7.0.rc4 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.7.0.rc3 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.7.0.rc2 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb
gorgon-0.7.0.rc1 lib/gorgon_bunny/lib/gorgon_bunny/concurrent/atomic_fixnum.rb