Class: Bunny::Concurrent::AtomicFixnum
- Inherits:
-
Object
- Object
- Bunny::Concurrent::AtomicFixnum
- 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
- #==(m) ⇒ Object
- #===(v) ⇒ Object
- #decrement ⇒ Object (also: #dec, #decrement_and_get)
- #get ⇒ Object (also: #to_i)
- #get_and_add(i) ⇒ Object
- #get_and_increment ⇒ Object
- #increment ⇒ Object (also: #inc, #increment_and_get)
-
#initialize(n = 0) ⇒ AtomicFixnum
constructor
A new instance of AtomicFixnum.
- #set(n) ⇒ Object
Constructor Details
#initialize(n = 0) ⇒ AtomicFixnum
Returns a new instance of AtomicFixnum.
14 15 16 17 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 14 def initialize(n = 0) @n = n @mutex = Monitor.new end |
Instance Method Details
#==(m) ⇒ Object
66 67 68 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 66 def ==(m) @mutex.synchronize { @n == m } end |
#===(v) ⇒ Object
70 71 72 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 70 def ===(v) @mutex.synchronize { @n === v } end |
#decrement ⇒ Object Also known as: dec, decrement_and_get
58 59 60 61 62 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 58 def decrement @mutex.synchronize do @n = @n - 1 end end |
#get ⇒ Object Also known as: to_i
19 20 21 22 23 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 19 def get @mutex.synchronize do @n end end |
#get_and_add(i) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 40 def get_and_add(i) @mutex.synchronize do v = @n @n = @n + i v end end |
#get_and_increment ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 49 def get_and_increment @mutex.synchronize do v = @n @n = @n + 1 v end end |
#increment ⇒ Object Also known as: inc, increment_and_get
32 33 34 35 36 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 32 def increment @mutex.synchronize do @n = @n + 1 end end |
#set(n) ⇒ Object
26 27 28 29 30 |
# File 'lib/bunny/concurrent/atomic_fixnum.rb', line 26 def set(n) @mutex.synchronize do @n = n end end |