From 246fe3d81899e2b4a618eaa4ae0de9a6da04f1de Mon Sep 17 00:00:00 2001 From: Jun Aruga Date: Thu, 26 Jul 2018 17:55:43 +0200 Subject: [PATCH 1/2] replace thread_safe with concurrent-ruby thread_safe is deprecated and has been merged into concurrent-ruby --- CHANGELOG.adoc | 4 ++++ asciidoctor.gemspec | 2 +- lib/asciidoctor/converter/factory.rb | 14 +++++++------- lib/asciidoctor/converter/template.rb | 12 ++++++------ test/converter_test.rb | 4 ++-- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index ed9530d01f..c883007e82 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -39,6 +39,10 @@ Improvements:: * use shorthands %F and %T instead of %Y-%m-%d and %H:%M:%S to format time * read file in binary mode whenever contents are being normalized +Build / Infrastructure:: + + * replace thread_safe with concurrent-ruby (PR #2822) (*@junaruga*) + // tag::compact[] == 1.5.7.1 (2018-05-10) - @mojavelinux diff --git a/asciidoctor.gemspec b/asciidoctor.gemspec index ce66bc42e3..d433b6027e 100644 --- a/asciidoctor.gemspec +++ b/asciidoctor.gemspec @@ -51,7 +51,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'rspec-expectations', '~> 2.14.0' # slim is needed for testing custom templates s.add_development_dependency 'slim', '~> 3.0.0' - s.add_development_dependency 'thread_safe', '~> 0.3.0' + s.add_development_dependency 'concurrent-ruby', '~> 1.0.5' # tilt is needed for testing custom templates s.add_development_dependency 'tilt', '~> 2.0.0' s.add_development_dependency 'minitest', '~> 5.3.0' diff --git a/lib/asciidoctor/converter/factory.rb b/lib/asciidoctor/converter/factory.rb index 8fbf43f87a..3f365e07d9 100644 --- a/lib/asciidoctor/converter/factory.rb +++ b/lib/asciidoctor/converter/factory.rb @@ -23,7 +23,7 @@ module Converter # DocBook 5} and {DocBook45Converter DocBook 4.5}, as well as any custom # converters that have been discovered or explicitly registered. # - # If the {https://rubygems.org/gems/thread_safe thread_safe} gem is + # If the {https://rubygems.org/gems/concurrent-ruby concurrent-ruby} gem is # installed, access to the default factory is guaranteed to be thread safe. # Otherwise, a warning is issued to the user. class Factory @@ -32,8 +32,8 @@ class << self # Public: Retrieves a singleton instance of {Factory Converter::Factory}. # - # If the thread_safe gem is installed, the registry of converters is - # initialized as a ThreadSafe::Cache. Otherwise, a warning is issued and + # If the concurrent-ruby gem is installed, the registry of converters is + # initialized as a Concurrent::Hash. Otherwise, a warning is issued and # the registry of converters is initialized using a normal Hash. # # initialize_singleton - A Boolean to indicate whether the singleton should @@ -44,14 +44,14 @@ class << self # Returns the default [Factory] singleton instance def default initialize_singleton = true return @__default__ || new unless initialize_singleton - # FIXME this assignment is not thread_safe, may need to use a ::Threadsafe helper here + # FIXME this assignment is not concurrent-ruby, may need to use a ::Threadsafe helper here @__default__ ||= begin # NOTE .to_s hides require from Opal - require 'thread_safe'.to_s unless defined? ::ThreadSafe - new ::ThreadSafe::Cache.new + require 'concurrent'.to_s unless defined? ::Concurrent + new ::Concurrent::Hash.new rescue ::LoadError include Logging unless include? Logging - logger.warn 'gem \'thread_safe\' is not installed. This gem is recommended when registering custom converters.' + logger.warn 'gem \'concurrent-ruby\' is not installed. This gem is recommended when registering custom converters.' new end end diff --git a/lib/asciidoctor/converter/template.rb b/lib/asciidoctor/converter/template.rb index 8f6661e4c0..a3f805d22c 100644 --- a/lib/asciidoctor/converter/template.rb +++ b/lib/asciidoctor/converter/template.rb @@ -1,4 +1,4 @@ -autoload :ThreadSafe, 'thread_safe' +autoload :Concurrent, 'concurrent' # encoding: UTF-8 module Asciidoctor @@ -22,8 +22,8 @@ module Asciidoctor # backend format (e.g., "html5"). # # As an optimization, scan results and templates are cached for the lifetime - # of the Ruby process. If the {https://rubygems.org/gems/thread_safe - # thread_safe} gem is installed, these caches are guaranteed to be thread + # of the Ruby process. If the {https://rubygems.org/gems/concurrent-ruby + # concurrent-ruby} gem is installed, these caches are guaranteed to be thread # safe. If this gem is not present, there is no such guarantee and a warning # will be issued. class Converter::TemplateConverter < Converter::Base @@ -36,8 +36,8 @@ class Converter::TemplateConverter < Converter::Base } begin - # triggers autoload of thread_safe - @caches = { :scans => ::ThreadSafe::Cache.new, :templates => ::ThreadSafe::Cache.new } + # triggers autoload of concurrent + @caches = { :scans => ::Concurrent::Hash.new, :templates => ::Concurrent::Hash.new } rescue ::LoadError @caches = { :scans => {}, :templates => {} } end @@ -76,7 +76,7 @@ def initialize backend, template_dirs, opts = {} end case opts[:template_cache] when true - logger.warn 'gem \'thread_safe\' is not installed. This gem is recommended when using the built-in template cache.' unless defined? ::ThreadSafe::Cache + logger.warn 'gem \'concurrent-ruby\' is not installed. This gem is recommended when using the built-in template cache.' unless defined? ::Concurrent::Hash @caches = self.class.caches when ::Hash @caches = opts[:template_cache] diff --git a/test/converter_test.rb b/test/converter_test.rb index d498f7d963..d21d4ade32 100644 --- a/test/converter_test.rb +++ b/test/converter_test.rb @@ -171,8 +171,8 @@ doc = Asciidoctor::Document.new [], :template_dir => template_dir doc.converter caches = Asciidoctor::Converter::TemplateConverter.caches - if defined? ::ThreadSafe::Cache - assert_kind_of ::ThreadSafe::Cache, caches[:templates] + if defined? ::Concurrent::Hash + assert_kind_of ::Concurrent::Hash, caches[:templates] refute_empty caches[:templates] paragraph_template_before = caches[:templates].values.find {|t| File.basename(t.file) == 'block_paragraph.html.haml' } refute_nil paragraph_template_before From 1685c242c8581532931fa5043dc08ef20d26ce0f Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Tue, 31 Jul 2018 00:23:57 -0600 Subject: [PATCH 2/2] map Concurrent::Hash to ThreadSafe::Cache in Ruby 1.8.7 --- Gemfile | 31 +++++++++++-------- asciidoctor.gemspec | 3 +- lib/asciidoctor/converter/factory.rb | 8 +++-- lib/asciidoctor/converter/template.rb | 6 ++-- .../core_ext/1.8.7/concurrent/hash.rb | 5 +++ 5 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 lib/asciidoctor/core_ext/1.8.7/concurrent/hash.rb diff --git a/Gemfile b/Gemfile index ba08d414e9..3a850be059 100644 --- a/Gemfile +++ b/Gemfile @@ -4,25 +4,30 @@ source 'https://rubygems.org' gemspec group :development do - if (ruby_version = Gem::Version.new RUBY_VERSION) < (Gem::Version.new '2.1.0') - if ruby_version < (Gem::Version.new '2.0.0') - gem 'haml', '~> 4.0.0' - if ruby_version < (Gem::Version.new '1.9.3') - gem 'cucumber', '~> 1.3.0' - gem 'nokogiri', '~> 1.5.0' - gem 'slim', '~> 2.1.0' - gem 'tilt', '2.0.7' + ruby_version = Gem::Version.new RUBY_VERSION + gem 'concurrent-ruby', '~> 1.0.0' unless ruby_version < (Gem::Version.new '1.9.3') + if ruby_version < (Gem::Version.new '2.2.0') + if ruby_version < (Gem::Version.new '2.1.0') + if ruby_version < (Gem::Version.new '2.0.0') + gem 'haml', '~> 4.0.0' + if ruby_version < (Gem::Version.new '1.9.3') + gem 'cucumber', '~> 1.3.0' + gem 'nokogiri', '~> 1.5.0' + gem 'slim', '~> 2.1.0' + gem 'thread_safe', '0.3.6' + gem 'tilt', '2.0.7' + else + gem 'nokogiri', '~> 1.6.0' + gem 'slim', '<= 3.0.7' + end else gem 'nokogiri', '~> 1.6.0' - gem 'slim', '<= 3.0.7' end else - gem 'nokogiri', '~> 1.6.0' + gem 'nokogiri', '~> 1.7.0' if Gem::Platform.local =~ 'x86-mingw32' || Gem::Platform.local =~ 'x64-mingw32' + gem 'racc', '~> 1.4.0' if RUBY_VERSION == '2.1.0' && RUBY_ENGINE == 'rbx' end - elsif ruby_version < (Gem::Version.new '2.2.0') - gem 'nokogiri', '~> 1.7.0' if Gem::Platform.local =~ 'x86-mingw32' || Gem::Platform.local =~ 'x64-mingw32' end - gem 'racc', '~> 1.4.0' if RUBY_VERSION == '2.1.0' && RUBY_ENGINE == 'rbx' end group :doc do diff --git a/asciidoctor.gemspec b/asciidoctor.gemspec index d433b6027e..db2dac8b7e 100644 --- a/asciidoctor.gemspec +++ b/asciidoctor.gemspec @@ -51,7 +51,8 @@ Gem::Specification.new do |s| s.add_development_dependency 'rspec-expectations', '~> 2.14.0' # slim is needed for testing custom templates s.add_development_dependency 'slim', '~> 3.0.0' - s.add_development_dependency 'concurrent-ruby', '~> 1.0.5' + # concurrent-ruby is defined in Gemfile due to enforcement of minimum required Ruby version + #s.add_development_dependency 'concurrent-ruby', '~> 1.0.0' # tilt is needed for testing custom templates s.add_development_dependency 'tilt', '~> 2.0.0' s.add_development_dependency 'minitest', '~> 5.3.0' diff --git a/lib/asciidoctor/converter/factory.rb b/lib/asciidoctor/converter/factory.rb index 3f365e07d9..61352fbf89 100644 --- a/lib/asciidoctor/converter/factory.rb +++ b/lib/asciidoctor/converter/factory.rb @@ -44,10 +44,12 @@ class << self # Returns the default [Factory] singleton instance def default initialize_singleton = true return @__default__ || new unless initialize_singleton - # FIXME this assignment is not concurrent-ruby, may need to use a ::Threadsafe helper here + # FIXME this assignment itself may not be thread safe; may need to use a helper here @__default__ ||= begin - # NOTE .to_s hides require from Opal - require 'concurrent'.to_s unless defined? ::Concurrent + unless defined? ::Concurrent::Hash + # NOTE .to_s hides require from Opal + require ::RUBY_MIN_VERSION_1_9 ? 'concurrent/hash'.to_s : 'asciidoctor/core_ext/1.8.7/concurrent/hash'.to_s + end new ::Concurrent::Hash.new rescue ::LoadError include Logging unless include? Logging diff --git a/lib/asciidoctor/converter/template.rb b/lib/asciidoctor/converter/template.rb index a3f805d22c..36a220a39d 100644 --- a/lib/asciidoctor/converter/template.rb +++ b/lib/asciidoctor/converter/template.rb @@ -1,5 +1,3 @@ -autoload :Concurrent, 'concurrent' - # encoding: UTF-8 module Asciidoctor # A {Converter} implementation that uses templates composed in template @@ -36,7 +34,9 @@ class Converter::TemplateConverter < Converter::Base } begin - # triggers autoload of concurrent + unless defined? ::Concurrent::Hash + require ::RUBY_MIN_VERSION_1_9 ? 'concurrent/hash' : 'asciidoctor/core_ext/1.8.7/concurrent/hash' + end @caches = { :scans => ::Concurrent::Hash.new, :templates => ::Concurrent::Hash.new } rescue ::LoadError @caches = { :scans => {}, :templates => {} } diff --git a/lib/asciidoctor/core_ext/1.8.7/concurrent/hash.rb b/lib/asciidoctor/core_ext/1.8.7/concurrent/hash.rb new file mode 100644 index 0000000000..7c9b11a476 --- /dev/null +++ b/lib/asciidoctor/core_ext/1.8.7/concurrent/hash.rb @@ -0,0 +1,5 @@ +require 'thread_safe' + +module Concurrent + Hash = ::ThreadSafe::Cache +end