Class: ZK::Locker::SharedLocker

Inherits:
LockerBase show all
Includes:
Exceptions
Defined in:
lib/zk/locker/shared_locker.rb

Instance Attribute Summary

Attributes inherited from LockerBase

#lock_path

Instance Method Summary collapse

Methods inherited from LockerBase

#initialize, #lock!, #lock_basename, #unlock, #unlock!, #with_lock

Methods included from ZK::Logging

#logger, set_default

Constructor Details

This class inherits a constructor from ZK::Locker::LockerBase

Instance Method Details

#acquirable?Boolean

Note:

It should be obvious, but there is no way to guarantee that between the time this method checks the server and taking any action to acquire the lock, another client may grab the lock before us (or converseley, another client may release the lock). This is simply meant as an advisory, and may be useful in some cases.

  • If this instance holds the lock is true we return true (as we have already succeeded in acquiring the lock)
  • If this instance doesn't hold the lock, we'll do a check on the server to see if there are any participants who hold the lock and would prevent us from acquiring the lock.
    • If this instance could acquire the lock we will return true.
    • If another client would prevent us from acquiring the lock, we return false.

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/zk/locker/shared_locker.rb', line 44

def acquirable?
  return true if locked?
  !lock_children.any? { |n| n.start_with?(EXCLUSIVE_LOCK_PREFIX) }
rescue Exceptions::NoNode
  true
end

#assert!Object

This is for users who wish to check that the assumption is correct that they actually still hold the lock. (check for session interruption, perhaps a lock is obtained in one method and handed to another)

This, unlike #locked? will actually go and check the conditions that constitute "holding the lock" with the server.

checks that we:

  • we have obtained the lock (i.e. #locked? is true)
  • have a lock path
  • our lock path still exists
  • there are no exclusive locks with lower numbers than ours

Examples:


def process_jobs
  @lock.with_lock do
    @jobs.each do |j| 
      @lock.assert!
      perform_job(j)
    end
  end
end

def perform_job(j)
  puts "hah! he thinks we're workin!"
  sleep(60)
end

Raises:



34
35
36
# File 'lib/zk/locker/shared_locker.rb', line 34

def assert!
  super
end

#lock(blocking = false) ⇒ true, ...

obtain a shared lock.

Parameters:

  • blocking (true, false) (defaults to: false)

    if true we block the caller until we can obtain a lock on the resource

Returns:

  • (true)

    if we're already obtained a shared lock, or if we were able to obtain the lock in non-blocking mode.

  • (false)

    if we did not obtain the lock in non-blocking mode

  • (void)

    if we obtained the lock in blocking mode.

Raises:

  • (InterruptedSession)

    raised when blocked waiting for a lock and the underlying client's session is interrupted.

See Also:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zk/locker/shared_locker.rb', line 9

def lock(blocking=false)
  return true if @locked
  create_lock_path!(SHARED_LOCK_PREFIX)

  if got_read_lock?      
    @locked = true
  elsif blocking
    block_until_read_lock!
  else
    # we didn't get the lock, and we're not gonna wait around for it, so
    # clean up after ourselves
    cleanup_lock_path!
    false
  end
end

#locked?true, false

returns our current idea of whether or not we hold the lock, which does not actually check the state on the server.

The reason for the equivocation around thinking we hold the lock is to contrast our current state and the actual state on the server. If you want to make double-triple certain of the state of the lock, use #assert!

Returns:

  • (true)

    if we hold the lock

  • (false)

    if we don't hold the lock



39
40
41
# File 'lib/zk/locker/shared_locker.rb', line 39

def locked?
  false|@locked
end