Sha256: 0e83e7e3c04c755fac9e15b4eec5a59a6e021bad8569474f98aa4f7bb7663026

Contents?: true

Size: 833 Bytes

Versions: 17

Compression:

Stored size: 833 Bytes

Contents

require 'thread'
require 'set'

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

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

  # コンストラクタ
  def initialize( point, options )
    @id = options[:id] || :default
    @mutex = mutex( @id )
  end

  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

  # IDに対応するmutexを取得する
  def mutex( id )
    @@pool_mutex.synchronize {
       @@pool[id] ||= Mutex.new
    }
  end

end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
unageanu-jiji-0.1.0 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.0.0 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.0.1 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.0.2 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.0.3 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.1.0 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.1.1 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.1.2 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.1.3 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.1.4 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.2.1 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.2.3 lib/jiji/util/synchronize_interceptor.rb
unageanu-jiji-1.2.4 lib/jiji/util/synchronize_interceptor.rb
jiji-1.2.8 lib/jiji/util/synchronize_interceptor.rb
jiji-1.2.7 lib/jiji/util/synchronize_interceptor.rb
jiji-1.2.6 lib/jiji/util/synchronize_interceptor.rb
jiji-1.2.5 lib/jiji/util/synchronize_interceptor.rb