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 169
def initialize
  self.class.__sleep
  puts "initialize called by thread ##{Thread.current[:i]}"
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