Class: SynchronizeInterceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/util/synchronize_interceptor.rb

Overview

同時実行抑制インターセプタ

Constant Summary collapse

@@pool =
{}
@@pool_mutex =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(point, options) ⇒ SynchronizeInterceptor

コンストラクタ



12
13
14
15
# File 'lib/jiji/util/synchronize_interceptor.rb', line 12

def initialize( point, options )
  @id = options[:id] || :default
  @mutex = mutex( @id )
end

Instance Method Details

#mutex(id) ⇒ Object

IDに対応するmutexを取得する



35
36
37
38
39
# File 'lib/jiji/util/synchronize_interceptor.rb', line 35

def mutex( id )
  @@pool_mutex.synchronize {
     @@pool[id] ||= Mutex.new
  }
end

#process(chain, context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jiji/util/synchronize_interceptor.rb', line 17

def process( chain, context )
  # 2重ロックの回避
  set = Thread.current[:synchronize_interceptor_locked] ||= Set.new
  if set.include? @id
    chain.process_next( context )
  else
    set.add( @id )
    begin
      @mutex.synchronize {
        chain.process_next( context )
      }
    ensure
      set.delete( @id )
    end
  end
end