File: native_extension_loader.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (79 lines) | stat: -rw-r--r-- 1,927 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'concurrent/utility/engine'

module Concurrent

  module Utility

    # @!visibility private
    module NativeExtensionLoader

      def allow_c_extensions?
        Concurrent.on_cruby?
      end

      def c_extensions_loaded?
        defined?(@c_extensions_loaded) && @c_extensions_loaded
      end

      def java_extensions_loaded?
        defined?(@java_extensions_loaded) && @java_extensions_loaded
      end

      def load_native_extensions
        unless defined? Synchronization::AbstractObject
          raise 'native_extension_loader loaded before Synchronization::AbstractObject'
        end

        if Concurrent.on_cruby? && !c_extensions_loaded?
          ['concurrent/concurrent_ruby_ext',
           "concurrent/#{RUBY_VERSION[0..2]}/concurrent_ruby_ext"
          ].each { |p| try_load_c_extension p }
        end

        if Concurrent.on_jruby? && !java_extensions_loaded?
          begin
            require 'concurrent/concurrent_ruby.jar'
            set_java_extensions_loaded
          rescue LoadError => e
            raise e, "Java extensions are required for JRuby.\n" + e.message, e.backtrace
          end
        end
      end

      private

      def load_error_path(error)
        if error.respond_to? :path
          error.path
        else
          error.message.split(' -- ').last
        end
      end

      def set_c_extensions_loaded
        @c_extensions_loaded = true
      end

      def set_java_extensions_loaded
        @java_extensions_loaded = true
      end

      def try_load_c_extension(path)
        require path
        set_c_extensions_loaded
      rescue LoadError => e
        if load_error_path(e) == path
          # move on with pure-Ruby implementations
          # TODO (pitr-ch 12-Jul-2018): warning on verbose?
        else
          raise e
        end
      end

    end
  end

  # @!visibility private
  extend Utility::NativeExtensionLoader
end