Class: Berater::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/berater/lock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#capacityObject (readonly)

Returns the value of attribute capacity.



4
5
6
# File 'lib/berater/lock.rb', line 4

def capacity
  @capacity
end

#contentionObject (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

Returns:

  • (Boolean)


14
15
16
# File 'lib/berater/lock.rb', line 14

def locked?
  @released_at.nil?
end

#releaseObject



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
  nil
end