[ruby/singleton] Only use RactorLocalSingleton if Ractor is defined
https://github.com/ruby/singleton/commit/f684d36a47
This commit is contained in:
parent
a6e96df573
commit
d0c1eef511
@ -196,43 +196,45 @@ module Singleton
|
|||||||
# Returns the singleton instance.
|
# Returns the singleton instance.
|
||||||
end
|
end
|
||||||
|
|
||||||
module RactorLocalSingleton
|
if defined?(Ractor)
|
||||||
include Singleton::SingletonInstanceMethods
|
module RactorLocalSingleton
|
||||||
|
include Singleton::SingletonInstanceMethods
|
||||||
|
|
||||||
module RactorLocalSingletonClassMethods
|
module RactorLocalSingletonClassMethods
|
||||||
include Singleton::SingletonClassMethods
|
include Singleton::SingletonClassMethods
|
||||||
def instance
|
def instance
|
||||||
set_mutex(Thread::Mutex.new) if Ractor.current[mutex_key].nil?
|
set_mutex(Thread::Mutex.new) if Ractor.current[mutex_key].nil?
|
||||||
return Ractor.current[instance_key] if Ractor.current[instance_key]
|
|
||||||
Ractor.current[mutex_key].synchronize {
|
|
||||||
return Ractor.current[instance_key] if Ractor.current[instance_key]
|
return Ractor.current[instance_key] if Ractor.current[instance_key]
|
||||||
set_instance(new())
|
Ractor.current[mutex_key].synchronize {
|
||||||
}
|
return Ractor.current[instance_key] if Ractor.current[instance_key]
|
||||||
Ractor.current[instance_key]
|
set_instance(new())
|
||||||
|
}
|
||||||
|
Ractor.current[instance_key]
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def instance_key
|
||||||
|
:"__RactorLocalSingleton_instance_with_class_id_#{object_id}__"
|
||||||
|
end
|
||||||
|
|
||||||
|
def mutex_key
|
||||||
|
:"__RactorLocalSingleton_mutex_with_class_id_#{object_id}__"
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_instance(val)
|
||||||
|
Ractor.current[instance_key] = val
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_mutex(val)
|
||||||
|
Ractor.current[mutex_key] = val
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def self.module_with_class_methods
|
||||||
|
RactorLocalSingletonClassMethods
|
||||||
def instance_key
|
|
||||||
:"__RactorLocalSingleton_instance_with_class_id_#{object_id}__"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def mutex_key
|
extend Singleton::SingletonClassProperties
|
||||||
:"__RactorLocalSingleton_mutex_with_class_id_#{object_id}__"
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_instance(val)
|
|
||||||
Ractor.current[instance_key] = val
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_mutex(val)
|
|
||||||
Ractor.current[mutex_key] = val
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.module_with_class_methods
|
|
||||||
RactorLocalSingletonClassMethods
|
|
||||||
end
|
|
||||||
|
|
||||||
extend Singleton::SingletonClassProperties
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user