Class: Ups
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Singleton
__init__, #_dump, #clone, #dup
Constructor Details
#initialize ⇒ Ups
Returns a new instance of Ups.
169
170
171
172
|
# File 'lib/rubysl/singleton/singleton.rb', line 169
def initialize
self.class.__sleep
puts "initialize called by thread ##{Thread.current[:i]}"
end
|
Class Method Details
.__sleep ⇒ Object
187
188
189
|
# File 'lib/rubysl/singleton/singleton.rb', line 187
def __sleep
sleep(rand(0.08))
end
|
._instantiate? ⇒ Boolean
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/rubysl/singleton/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 ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/rubysl/singleton/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 ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/rubysl/singleton/singleton.rb', line 191
def new
begin
__sleep
raise "boom - thread ##{Thread.current[:i]} failed to create instance"
ensure
class << self
remove_method :new
end
end
end
|