lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.18 vs lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.19

- old
+ new

@@ -408,16 +408,29 @@ def trigger_job(params, &block) if params[:blocking] block.call elsif m = params[:mutex] - m = (@mutexes[m.to_s] ||= Mutex.new) unless m.is_a?(Mutex) - Thread.new { m.synchronize { block.call } } + Thread.new { synchronize_with_mutex(m, &block) } else Thread.new { block.call } end end + + def synchronize_with_mutex(mutex, &block) + case mutex + when Mutex + mutex.synchronize { block.call } + when Array + mutex.reduce(block) do |memo, m| + m = (@mutexes[m.to_s] ||= Mutex.new) unless m.is_a?(Mutex) + lambda { m.synchronize { memo.call } } + end.call + else + (@mutexes[mutex.to_s] ||= Mutex.new).synchronize { block.call } + end + end end #-- # SCHEDULER 'IMPLEMENTATIONS' #++ @@ -547,11 +560,10 @@ # (if I read the doc carefully...) if params[:blocking] EM.next_tick { block.call } elsif m = params[:mutex] - m = (@mutexes[m.to_s] ||= Mutex.new) unless m.is_a?(Mutex) - EM.defer { m.synchronize { block.call } } + EM.defer { synchronize_with_mutex(m, &block) } else EM.defer { block.call } end end end