diff options
Diffstat (limited to 'vendor/rsync_command/lib/rsync_command/thread_pool.rb')
-rw-r--r-- | vendor/rsync_command/lib/rsync_command/thread_pool.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/vendor/rsync_command/lib/rsync_command/thread_pool.rb b/vendor/rsync_command/lib/rsync_command/thread_pool.rb new file mode 100644 index 0000000..c788ee2 --- /dev/null +++ b/vendor/rsync_command/lib/rsync_command/thread_pool.rb @@ -0,0 +1,36 @@ +require 'thread' + +class RsyncCommand + class ThreadPool + class << self + attr_accessor :default_size + end + + def initialize(size=nil) + @size = size || ThreadPool.default_size || 10 + @jobs = Queue.new + @retvals = [] + @pool = Array.new(@size) do |i| + Thread.new do + Thread.current[:id] = i + catch(:exit) do + loop do + job, args = @jobs.pop + @retvals << job.call(*args) + end + end + end + end + end + def schedule(*args, &block) + @jobs << [block, args] + end + def shutdown + @size.times do + schedule { throw :exit } + end + @pool.map(&:join) + @retvals + end + end +end |