Sha256: ea9077e6cc14499cc42ca7fe235ca5b1925e8c00fd235c20a4f9fdd8dc17c856

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

require 'mutex_lock_timeout'
require 'mutex_lock_release_failure'

class DistributedMutex < Mutex

  DEFAULT_TIMEOUT = 1
  DEFAULT_EXCEPTION_ON_TIMEOUT = false

  attr_reader :key, :timeout, :exception_on_timeout
  alias excluse_unlock unlock

  def initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT)
    @key = key
    @timeout = timeout
    @locked = false
    @exception_on_timeout = exception_on_timeout
  end

  def lock
    @locked = get_lock
    if true == @locked
      true
    else
      if @exception_on_timeout
        raise MutexLockTimeout.new
      else
        false
      end
    end
  end

  def locked?
    @locked
  end

  def synchronize(&block)
    if self.lock
      begin
        yield
      ensure
        self.unlock
      end
      true
    else
      false
    end
  end

  def try_lock
    was_locked = nil
    begin
      self.lock
      was_locked = locked?
    ensure
      self.unlock
    end
    was_locked
  end

  def unlock
    if locked?
      if release_lock
        @locked = false
        true
      else
        raise MutexLockReleaseFailure
      end
    else
      false
    end
  end

  def self.synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, &block)
    mutex = new(key, timeout, exception_on_timeout)
    mutex.synchronize(&block)
  end

  private

  # Return true if and only if a lock is obtained
  def get_lock
    raise 'Method not implemented'
  end

  # Return true if and only if a lock is released
  def release_lock
    raise 'Method not implemented'
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
distributed_mutex-1.1.7 lib/distributed_mutex.rb
distributed_mutex-1.1.6 lib/distributed_mutex.rb
distributed_mutex-1.1.5 lib/distributed_mutex.rb
distributed_mutex-1.1.4 lib/distributed_mutex.rb
distributed_mutex-1.1.3 lib/distributed_mutex.rb
distributed_mutex-1.1.2 lib/distributed_mutex.rb
distributed_mutex-1.1.1 lib/distributed_mutex.rb
distributed_mutex-1.1.0 lib/distributed_mutex.rb