Class: DangoMutex

Inherits:
Mutex show all
Defined in:
lib/dango/dango_mutex.rb

Overview

Mutexクラスの名前情報機能追加

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mutex_name = nil) ⇒ DangoMutex

Returns a new instance of DangoMutex.



12
13
14
15
16
# File 'lib/dango/dango_mutex.rb', line 12

def initialize(mutex_name = nil)
  @mutex_name = mutex_name.to_s
  @lock_name = ""
  super()
end

Instance Attribute Details

#lock_nameObject (readonly)

Returns the value of attribute lock_name.



18
19
20
# File 'lib/dango/dango_mutex.rb', line 18

def lock_name
  @lock_name
end

#mutex_nameObject (readonly)

Returns the value of attribute mutex_name.



18
19
20
# File 'lib/dango/dango_mutex.rb', line 18

def mutex_name
  @mutex_name
end

Instance Method Details

#timeout_sync(sec = 10, lock_name = "") ⇒ Object

タイムアウトする排他処理



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dango/dango_mutex.rb', line 21

def timeout_sync(sec = 10, lock_name = "")
  ret = nil
  begin
    timeout(sec, DangoFrameworkMutexTimeoutException) do
      self.synchronize do
        @lock_name = lock_name
        ret = yield
      end
    end
    
  rescue DangoFrameworkMutexTimeoutException, Timeout::Error, TimeoutError
    
    # なぜかDangoFrameworkMutexTimeoutException以外の例外クラスが飛ぶことがあるので
    if $!.class == DangoFrameworkMutexTimeoutException
      error_msg = ""
    else
      error_msg = "#{$!.class}:"
    end
    
    locked_msg = ""
    ObjectSpace.each_object(DangoMutex) do |object|
      if object.locked?
        locked_msg += "#{object.mutex_name.to_s}(#{object.lock_name.to_s}) "
      end
    end
    
    error_msg += "DangoFrameworkMutexTimeoutException " + 
                 "self=#{self.mutex_name}(#{lock_name})\n" + 
                 "locked_mutex=#{locked_msg}\n" + 
                 "#{$!.message}\n" + 
                 "#{$!.backtrace.pretty_inspect}"
    
    raise(DangoFrameworkMutexTimeoutException, error_msg)
    
  rescue Exception
#      puts "#{$!.class} #{$!.message}\n#{$!.backtrace.pretty_inspect}"
    
    raise
  end
  
  ret
end