File: native_extension_loader.rb

package info (click to toggle)
ruby-concurrent 1.0.5-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,200 kB
  • sloc: ruby: 27,502; java: 6,085; ansic: 282; sh: 82; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 1,739 bytes parent folder | download | duplicates (2)
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
require 'concurrent/utility/engine'

module Concurrent

  module Utility

    # @!visibility private
    module NativeExtensionLoader

      def allow_c_extensions?
        Concurrent.on_cruby?
      end

      def c_extensions_loaded?
        @c_extensions_loaded ||= false
      end

      def java_extensions_loaded?
        @java_extensions_loaded ||= false
      end

      def set_c_extensions_loaded
        @c_extensions_loaded = true
      end

      def set_java_extensions_loaded
        @java_extensions_loaded = true
      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?
          tries = [
            lambda do
              require 'concurrent/extension'
              set_c_extensions_loaded
            end,
            lambda do
              # may be a Windows cross-compiled native gem
              require "concurrent/#{RUBY_VERSION[0..2]}/extension"
              set_c_extensions_loaded
            end]

          tries.each do |try|
            begin
              try.call
              break
            rescue LoadError
              next
            end
          end
        end

        if Concurrent.on_jruby? && !java_extensions_loaded?
          begin
            require 'concurrent_ruby_ext'
            set_java_extensions_loaded
          rescue LoadError
            # move on with pure-Ruby implementations
            raise 'On JRuby but Java extensions failed to load.'
          end
        end
      end
    end
  end

  # @!visibility private
  extend Utility::NativeExtensionLoader
end