Sha256: 01b042663e896fa2a14a9e54a228c9dddd944880ad112853d366a094abe1747a

Contents?: true

Size: 752 Bytes

Versions: 1

Compression:

Stored size: 752 Bytes

Contents

# frozen_string_literal: true

require "concurrent"

module Speculation
  # @private
  module Pmap
    refine Array do
      if RUBY_PLATFORM == "java"
        def pmap(&block)
          Pmap.pmap_jruby(self, &block)
        end
      else
        alias_method :pmap, :map
      end
    end

    def self.pmap_jruby(array, &block)
      thread_count = [1, Concurrent.processor_count - 1].max
      pool = Concurrent::FixedThreadPool.new(thread_count, :auto_terminate  => true,
                                                           :fallback_policy => :abort)

      array.
        map { |x| Concurrent::Future.execute(:executor => pool) { block.call(x) } }.
        map { |f| f.value || f.reason }
    ensure
      pool.shutdown
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
speculation-0.1.0 lib/speculation/pmap.rb