зеркало из https://github.com/github/ruby.git
Extract mutex_m as bundled gems
This commit is contained in:
Родитель
e59a730477
Коммит
d16f992e1b
|
@ -181,11 +181,6 @@ have commit right, others don't.
|
|||
* https://github.com/ruby/logger
|
||||
* https://rubygems.org/gems/logger
|
||||
|
||||
#### lib/mutex_m.rb
|
||||
* Keiju ISHITSUKA (keiju)
|
||||
* https://github.com/ruby/mutex_m
|
||||
* https://rubygems.org/gems/mutex_m
|
||||
|
||||
#### lib/net/http.rb, lib/net/https.rb
|
||||
* NARUSE, Yui (naruse)
|
||||
* https://github.com/ruby/net-http
|
||||
|
@ -487,6 +482,9 @@ have commit right, others don't.
|
|||
### racc
|
||||
* https://github.com/ruby/racc
|
||||
|
||||
#### mutex_m
|
||||
* https://github.com/ruby/mutex_m
|
||||
|
||||
|
||||
## Platform Maintainers
|
||||
### mswin64 (Microsoft Windows)
|
||||
|
|
|
@ -53,7 +53,6 @@ IPAddr:: Provides methods to manipulate IPv4 and IPv6 IP addresses
|
|||
IRB:: Interactive Ruby command-line tool for REPL (Read Eval Print Loop)
|
||||
OptionParser:: Ruby-oriented class for command-line option analysis
|
||||
Logger:: Provides a simple logging utility for outputting messages
|
||||
Mutex_m:: Mixin to extend objects to be handled like a Mutex
|
||||
Net::HTTP:: HTTP client api for Ruby
|
||||
Observable:: Provides a mechanism for publish/subscribe pattern in Ruby
|
||||
Open3:: Provides access to stdin, stdout and stderr when running other programs
|
||||
|
@ -130,3 +129,4 @@ RBS:: RBS is a language to describe the structure of Ruby programs
|
|||
TypeProf:: A type analysis tool for Ruby code based on abstract interpretation
|
||||
DEBUGGER__:: Debugging functionality for Ruby
|
||||
Racc:: A LALR(1) parser generator written in Ruby.
|
||||
Mutex_m:: Mixin to extend objects to be handled like a Mutex
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
begin
|
||||
require_relative "lib/mutex_m"
|
||||
rescue LoadError
|
||||
# for Ruby core repository
|
||||
require_relative "mutex_m"
|
||||
end
|
||||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "mutex_m"
|
||||
spec.version = Mutex_m::VERSION
|
||||
spec.authors = ["Keiju ISHITSUKA"]
|
||||
spec.email = ["keiju@ruby-lang.org"]
|
||||
|
||||
spec.summary = %q{Mixin to extend objects to be handled like a Mutex.}
|
||||
spec.description = %q{Mixin to extend objects to be handled like a Mutex.}
|
||||
spec.homepage = "https://github.com/ruby/mutex_m"
|
||||
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
||||
|
||||
spec.files = ["Gemfile", "LICENSE.txt", "README.md", "Rakefile", "lib/mutex_m.rb", "mutex_m.gemspec"]
|
||||
spec.bindir = "exe"
|
||||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
||||
spec.require_paths = ["lib"]
|
||||
spec.required_ruby_version = '>= 2.5'
|
||||
|
||||
spec.add_development_dependency "bundler"
|
||||
spec.add_development_dependency "rake"
|
||||
spec.add_development_dependency "test-unit"
|
||||
end
|
116
lib/mutex_m.rb
116
lib/mutex_m.rb
|
@ -1,116 +0,0 @@
|
|||
# frozen_string_literal: false
|
||||
#
|
||||
# mutex_m.rb -
|
||||
# $Release Version: 3.0$
|
||||
# $Revision: 1.7 $
|
||||
# Original from mutex.rb
|
||||
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
||||
# modified by matz
|
||||
# patched by akira yamada
|
||||
#
|
||||
# --
|
||||
|
||||
# = mutex_m.rb
|
||||
#
|
||||
# When 'mutex_m' is required, any object that extends or includes Mutex_m will
|
||||
# be treated like a Mutex.
|
||||
#
|
||||
# Start by requiring the standard library Mutex_m:
|
||||
#
|
||||
# require "mutex_m.rb"
|
||||
#
|
||||
# From here you can extend an object with Mutex instance methods:
|
||||
#
|
||||
# obj = Object.new
|
||||
# obj.extend Mutex_m
|
||||
#
|
||||
# Or mixin Mutex_m into your module to your class inherit Mutex instance
|
||||
# methods --- remember to call super() in your class initialize method.
|
||||
#
|
||||
# class Foo
|
||||
# include Mutex_m
|
||||
# def initialize
|
||||
# # ...
|
||||
# super()
|
||||
# end
|
||||
# # ...
|
||||
# end
|
||||
# obj = Foo.new
|
||||
# # this obj can be handled like Mutex
|
||||
#
|
||||
module Mutex_m
|
||||
|
||||
VERSION = "0.2.0"
|
||||
Ractor.make_shareable(VERSION) if defined?(Ractor)
|
||||
|
||||
def Mutex_m.define_aliases(cl) # :nodoc:
|
||||
cl.alias_method(:locked?, :mu_locked?)
|
||||
cl.alias_method(:lock, :mu_lock)
|
||||
cl.alias_method(:unlock, :mu_unlock)
|
||||
cl.alias_method(:try_lock, :mu_try_lock)
|
||||
cl.alias_method(:synchronize, :mu_synchronize)
|
||||
end
|
||||
|
||||
def Mutex_m.append_features(cl) # :nodoc:
|
||||
super
|
||||
define_aliases(cl) unless cl.instance_of?(Module)
|
||||
end
|
||||
|
||||
def Mutex_m.extend_object(obj) # :nodoc:
|
||||
super
|
||||
obj.mu_extended
|
||||
end
|
||||
|
||||
def mu_extended # :nodoc:
|
||||
unless (defined? locked? and
|
||||
defined? lock and
|
||||
defined? unlock and
|
||||
defined? try_lock and
|
||||
defined? synchronize)
|
||||
Mutex_m.define_aliases(singleton_class)
|
||||
end
|
||||
mu_initialize
|
||||
end
|
||||
|
||||
# See Thread::Mutex#synchronize
|
||||
def mu_synchronize(&block)
|
||||
@_mutex.synchronize(&block)
|
||||
end
|
||||
|
||||
# See Thread::Mutex#locked?
|
||||
def mu_locked?
|
||||
@_mutex.locked?
|
||||
end
|
||||
|
||||
# See Thread::Mutex#try_lock
|
||||
def mu_try_lock
|
||||
@_mutex.try_lock
|
||||
end
|
||||
|
||||
# See Thread::Mutex#lock
|
||||
def mu_lock
|
||||
@_mutex.lock
|
||||
end
|
||||
|
||||
# See Thread::Mutex#unlock
|
||||
def mu_unlock
|
||||
@_mutex.unlock
|
||||
end
|
||||
|
||||
# See Thread::Mutex#sleep
|
||||
def sleep(timeout = nil)
|
||||
@_mutex.sleep(timeout)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mu_initialize # :nodoc:
|
||||
@_mutex = Thread::Mutex.new
|
||||
end
|
||||
|
||||
def initialize(*args) # :nodoc:
|
||||
mu_initialize
|
||||
super
|
||||
end
|
||||
ruby2_keywords(:initialize) if respond_to?(:ruby2_keywords, true)
|
||||
end
|
|
@ -1,79 +0,0 @@
|
|||
# frozen_string_literal: false
|
||||
require 'test/unit'
|
||||
require 'mutex_m'
|
||||
|
||||
class TestMutexM < Test::Unit::TestCase
|
||||
def test_cv_wait
|
||||
o = Object.new
|
||||
o.instance_variable_set(:@foo, nil)
|
||||
o.extend(Mutex_m)
|
||||
c = Thread::ConditionVariable.new
|
||||
t = Thread.start {
|
||||
o.synchronize do
|
||||
until foo = o.instance_variable_get(:@foo)
|
||||
c.wait(o)
|
||||
end
|
||||
foo
|
||||
end
|
||||
}
|
||||
sleep(0.0001)
|
||||
o.synchronize do
|
||||
o.instance_variable_set(:@foo, "abc")
|
||||
end
|
||||
c.signal
|
||||
assert_equal "abc", t.value
|
||||
end
|
||||
|
||||
class KeywordInitializeParent
|
||||
def initialize(x:)
|
||||
end
|
||||
end
|
||||
|
||||
class KeywordInitializeChild < KeywordInitializeParent
|
||||
include Mutex_m
|
||||
def initialize
|
||||
super(x: 1)
|
||||
end
|
||||
end
|
||||
|
||||
def test_initialize_with_keyword_arg
|
||||
assert KeywordInitializeChild.new
|
||||
end
|
||||
|
||||
class NoArgInitializeParent
|
||||
def initialize
|
||||
end
|
||||
end
|
||||
|
||||
class NoArgInitializeChild < NoArgInitializeParent
|
||||
include Mutex_m
|
||||
def initialize
|
||||
super()
|
||||
end
|
||||
end
|
||||
|
||||
def test_initialize_no_args
|
||||
assert NoArgInitializeChild.new
|
||||
end
|
||||
|
||||
def test_alias_extended_object
|
||||
object = Object.new
|
||||
object.extend(Mutex_m)
|
||||
|
||||
assert object.respond_to?(:locked?)
|
||||
assert object.respond_to?(:lock)
|
||||
assert object.respond_to?(:unlock)
|
||||
assert object.respond_to?(:try_lock)
|
||||
assert object.respond_to?(:synchronize)
|
||||
end
|
||||
|
||||
def test_alias_included_class
|
||||
object = NoArgInitializeChild.new
|
||||
|
||||
assert object.respond_to?(:locked?)
|
||||
assert object.respond_to?(:lock)
|
||||
assert object.respond_to?(:unlock)
|
||||
assert object.respond_to?(:try_lock)
|
||||
assert object.respond_to?(:synchronize)
|
||||
end
|
||||
end
|
Загрузка…
Ссылка в новой задаче