From ad1a2e0cd5970e5d1618782a6ac2d5328811370d Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Thu, 20 Apr 2023 11:02:45 +0200 Subject: Ensure some relevant constants are available in Ractor --- lib/ffi/dynamic_library.rb | 2 +- lib/ffi/library.rb | 2 +- lib/ffi/platform.rb | 28 +++++++++++++++------------- spec/ffi/library_spec.rb | 13 +++++++++++++ spec/ffi/platform_spec.rb | 13 +++++++++++++ 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/lib/ffi/dynamic_library.rb b/lib/ffi/dynamic_library.rb index a5469c4..b415033 100644 --- a/lib/ffi/dynamic_library.rb +++ b/lib/ffi/dynamic_library.rb @@ -35,7 +35,7 @@ module FFI SEARCH_PATH << '/opt/homebrew/lib' end - SEARCH_PATH_MESSAGE = "Searched in , #{SEARCH_PATH.join(', ')}" + SEARCH_PATH_MESSAGE = "Searched in , #{SEARCH_PATH.join(', ')}".freeze def self.load_library(name, flags) if name == FFI::CURRENT_PROCESS diff --git a/lib/ffi/library.rb b/lib/ffi/library.rb index 92d2143..21dce60 100644 --- a/lib/ffi/library.rb +++ b/lib/ffi/library.rb @@ -31,7 +31,7 @@ require 'ffi/dynamic_library' module FFI - CURRENT_PROCESS = USE_THIS_PROCESS_AS_LIBRARY = Object.new + CURRENT_PROCESS = USE_THIS_PROCESS_AS_LIBRARY = FFI.make_shareable(Object.new) # @param [#to_s] lib library name # @return [String] library name formatted for current platform diff --git a/lib/ffi/platform.rb b/lib/ffi/platform.rb index bf01a27..5ac4dd7 100644 --- a/lib/ffi/platform.rb +++ b/lib/ffi/platform.rb @@ -29,13 +29,15 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.# require 'rbconfig' +require_relative 'compat' + module FFI class PlatformError < LoadError; end # This module defines different constants and class methods to play with # various platforms. module Platform - OS = case RbConfig::CONFIG['host_os'].downcase + OS = FFI.make_shareable(case RbConfig::CONFIG['host_os'].downcase when /linux/ "linux" when /darwin/ @@ -54,13 +56,13 @@ module FFI "windows" else RbConfig::CONFIG['host_os'].downcase - end + end) OSVERSION = RbConfig::CONFIG['host_os'].gsub(/[^\d]/, '').to_i - CPU = RbConfig::CONFIG['host_cpu'] + CPU = FFI.make_shareable(RbConfig::CONFIG['host_cpu']) - ARCH = case CPU.downcase + ARCH = FFI.make_shareable(case CPU.downcase when /amd64|x86_64|x64/ "x86_64" when /i\d86|x86|i86pc/ @@ -81,7 +83,7 @@ module FFI end else RbConfig::CONFIG['host_cpu'] - end + end) private # @param [String) os @@ -105,21 +107,21 @@ module FFI # Add the version for known ABI breaks name_version = "12" if IS_FREEBSD && OSVERSION >= 12 # 64-bit inodes - NAME = "#{ARCH}-#{OS}#{name_version}" - CONF_DIR = File.join(File.dirname(__FILE__), 'platform', NAME) + NAME = FFI.make_shareable("#{ARCH}-#{OS}#{name_version}") + CONF_DIR = FFI.make_shareable(File.join(File.dirname(__FILE__), 'platform', NAME)) public - LIBPREFIX = case OS + LIBPREFIX = FFI.make_shareable(case OS when /windows|msys/ '' when /cygwin/ 'cyg' else 'lib' - end + end) - LIBSUFFIX = case OS + LIBSUFFIX = FFI.make_shareable(case OS when /darwin/ 'dylib' when /linux|bsd|solaris/ @@ -129,9 +131,9 @@ module FFI else # Punt and just assume a sane unix (i.e. anything but AIX) 'so' - end + end) - LIBC = if IS_WINDOWS + LIBC = FFI.make_shareable(if IS_WINDOWS crtname = RbConfig::CONFIG["RUBY_SO_NAME"][/msvc\w+/] || 'ucrtbase' "#{crtname}.dll" elsif IS_GNU @@ -143,7 +145,7 @@ module FFI "msys-2.0.dll" else "#{LIBPREFIX}c.#{LIBSUFFIX}" - end + end) LITTLE_ENDIAN = 1234 unless defined?(LITTLE_ENDIAN) BIG_ENDIAN = 4321 unless defined?(BIG_ENDIAN) diff --git a/spec/ffi/library_spec.rb b/spec/ffi/library_spec.rb index 5b806bb..f223d26 100644 --- a/spec/ffi/library_spec.rb +++ b/spec/ffi/library_spec.rb @@ -322,4 +322,17 @@ describe "Library" do expect(val[:data]).to eq(i) end end + + it "should have shareable constants for Ractor", :ractor do + res = Ractor.new do + [ + FFI::Library::LIBC, + FFI::Library::CURRENT_PROCESS, + FFI::CURRENT_PROCESS, + FFI::USE_THIS_PROCESS_AS_LIBRARY, + ] + end.take + + expect( res.size ).to be > 0 + end end diff --git a/spec/ffi/platform_spec.rb b/spec/ffi/platform_spec.rb index ad23621..8890b07 100644 --- a/spec/ffi/platform_spec.rb +++ b/spec/ffi/platform_spec.rb @@ -134,4 +134,17 @@ describe "FFI::Platform.unix?" do expect(FFI::Platform::BYTE_ORDER).to eq(order) end end + + it "should have shareable constants for Ractor", :ractor do + res = Ractor.new do + [ + FFI::Platform::OS, + FFI::Platform::CPU, + FFI::Platform::ARCH, + FFI::Platform::OS, + ] + end.take + + expect( res.size ).to be > 0 + end end -- cgit v1.2.1