Skip to content
Kward Search API index

Class: Kward::Workers::WriteLock

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/workers/write_lock.rb

Overview

Cooperative ownership guard for workspace-mutating worker tools.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriteLock

Returns a new instance of WriteLock.



7
8
9
10
# File 'lib/kward/workers/write_lock.rb', line 7

def initialize
  @owner_id = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#owner_idObject (readonly)

Returns the value of attribute owner_id.



12
13
14
# File 'lib/kward/workers/write_lock.rb', line 12

def owner_id
  @owner_id
end

Instance Method Details

#acquire(owner_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kward/workers/write_lock.rb', line 14

def acquire(owner_id)
  owner = owner_id.to_s
  return false if owner.empty?

  @mutex.synchronize do
    return true if @owner_id == owner
    return false if @owner_id

    @owner_id = owner
    true
  end
end

#owned_by?(owner_id) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/kward/workers/write_lock.rb', line 27

def owned_by?(owner_id)
  @mutex.synchronize { @owner_id == owner_id.to_s }
end

#release(owner_id) ⇒ Object



31
32
33
34
35
# File 'lib/kward/workers/write_lock.rb', line 31

def release(owner_id)
  @mutex.synchronize do
    @owner_id = nil if @owner_id == owner_id.to_s
  end
end