Class: Berater::Lock
- Inherits:
-
Object
- Object
- Berater::Lock
- Defined in:
- lib/berater/lock.rb
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
-
#contention ⇒ Object
readonly
Returns the value of attribute contention.
Instance Method Summary collapse
-
#initialize(capacity, contention, release_fn = nil) ⇒ Lock
constructor
A new instance of Lock.
- #locked? ⇒ Boolean
- #release ⇒ Object
Constructor Details
#initialize(capacity, contention, release_fn = nil) ⇒ Lock
Returns a new instance of Lock.
6 7 8 9 10 11 12 |
# File 'lib/berater/lock.rb', line 6 def initialize(capacity, contention, release_fn = nil) @capacity = capacity @contention = contention @locked_at = Time.now @release_fn = release_fn @released_at = nil end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
4 5 6 |
# File 'lib/berater/lock.rb', line 4 def capacity @capacity end |
#contention ⇒ Object (readonly)
Returns the value of attribute contention.
4 5 6 |
# File 'lib/berater/lock.rb', line 4 def contention @contention end |
Instance Method Details
#locked? ⇒ Boolean
14 15 16 |
# File 'lib/berater/lock.rb', line 14 def locked? @released_at.nil? end |
#release ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/berater/lock.rb', line 18 def release raise 'lock already released' unless locked? @released_at = Time.now @release_fn&.call true end |