Class: LockManager

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

Instance Method Summary collapse

Constructor Details

#initializeLockManager

Returns a new instance of LockManager.



5
6
7
# File 'lib/lock_manager.rb', line 5

def initialize
  @locks = {}
end

Instance Method Details

#acquire(lock_id) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/lock_manager.rb', line 17

def acquire( lock_id )
  if @locks[lock_id].try_lock
    lock_id
  else
    nil
  end
end

#create(params) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/lock_manager.rb', line 9

def create( params )
  systype = params[:systype].to_s
  locktype = params[:locktype].to_s
  lock_id = "#{systype}:#{locktype}"
  @locks[lock_id] = Mutex.new unless @locks.has_key?( lock_id )
  lock_id
end

#critical_section_insist(lock_id) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/lock_manager.rb', line 31

def critical_section_insist( lock_id )
  if block_given?
    if @locks.has_key?( lock_id )
      @locks[lock_id].synchronize do
        yield
      end
    end
  end
end

#critical_section_try(lock_id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/lock_manager.rb', line 41

def critical_section_try( lock_id )
  if block_given?
    if @locks.has_key?( lock_id )
      if acquire( lock_id )
        yield
        release( lock_id )
      end
    end
  end
end

#release(lock_id) ⇒ Object



25
26
27
28
29
# File 'lib/lock_manager.rb', line 25

def release( lock_id )
  if @locks.has_key?( lock_id )
    @locks[lock_id].unlock
  end
end