In Files

  • singleton.rb

Class/Module Index [+]

Quicksearch

Ups

Public Class Methods

__sleep() click to toggle source
 
               # File singleton.rb, line 187
def __sleep
  sleep(rand(0.08))
end
            
_instantiate?() click to toggle source
 
               # File singleton.rb, line 176
def _instantiate?
  @enter.push Thread.current[:i]
  while false.equal?(@singleton__instance__)
    @singleton__mutex__.unlock
    sleep 0.08
    @singleton__mutex__.lock
  end
  @leave.push Thread.current[:i]
  @singleton__instance__
end
            
instantiate_all() click to toggle source
 
               # File singleton.rb, line 203
def instantiate_all
  @enter = []
  @leave = []
  1.upto(9) {|i|
    Thread.new {
      begin
        Thread.current[:i] = i
        __sleep
        instance
      rescue RuntimeError => mes
        puts mes
      end
    }
  }
  puts "Before there were #{num_of_instances(self)}"
  sleep 3
  puts "Now there is #{num_of_instances(self)}"
  puts "#{@enter.join '; '} was the order of threads entering the waiting loop"
  puts "#{@leave.join '; '} was the order of threads leaving the waiting loop"
end
            
new() click to toggle source
 
               # File singleton.rb, line 191
def new
  begin
    __sleep
    raise  "boom - thread ##{Thread.current[:i]} failed to create instance"
  ensure
    # simple flip-flop
    class << self
      remove_method :new
    end
  end
end
            
new() click to toggle source
 
               # File singleton.rb, line 169
def initialize
  self.class.__sleep
  puts "initialize called by thread ##{Thread.current[:i]}"
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please see Improve the docs, or visit Documenting-ruby.org.

blog comments powered by Disqus