Class: Bunny::Concurrent::AtomicFixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/bunny/concurrent/atomic_fixnum.rb

Overview

Note:

Designed to be intentionally minimalistic and only cover Bunny’s needs.

Minimalistic implementation of a synchronized fixnum value, designed after (but not implementing the entire API of!)

Instance Method Summary collapse

Constructor Details

#initialize(n = 0) ⇒ AtomicFixnum

Returns a new instance of AtomicFixnum.



16
17
18
19
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 16

def initialize(n = 0)
  @n     = n
  @mutex = Monitor.new
end

Instance Method Details

#==(m) ⇒ Object



68
69
70
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 68

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

#===(v) ⇒ Object



72
73
74
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 72

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

#decrementObject Also known as: dec, decrement_and_get



60
61
62
63
64
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 60

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

#getObject Also known as: to_i



21
22
23
24
25
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 21

def get
  @mutex.synchronize do
    @n
  end
end

#get_and_add(i) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 42

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

    v
  end
end

#get_and_incrementObject



51
52
53
54
55
56
57
58
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 51

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

    v
  end
end

#incrementObject Also known as: inc, increment_and_get



34
35
36
37
38
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 34

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

#set(n) ⇒ Object



28
29
30
31
32
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 28

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