Module: Rbgo::Channel
- Included in:
- NetworkServiceFactory
- Defined in:
- lib/rbgo/select_chan.rb
Defined Under Namespace
Modules: Chan Classes: BufferChan, NonBufferChan
Class Method Summary collapse
-
.on_read(chan:, &blk) ⇒ Object
on_read.
-
.on_write(chan:, obj:, &blk) ⇒ Object
on_write.
-
.select_chan(*ops) ⇒ Object
select_chan.
Class Method Details
.on_read(chan:, &blk) ⇒ Object
on_read
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/rbgo/select_chan.rb', line 339 def on_read(chan:, &blk) raise ArgumentError.new('chan must be a Chan') unless chan.is_a? Chan op = Proc.new do res, ok = chan.deq(true) if blk.nil? [res, ok] else blk.call(res, ok) end end op.define_singleton_method(:register) do |cond| chan.send :register, observer: cond, mode: :r end op.define_singleton_method(:unregister) do |cond| chan.send :unregister, observer: cond, mode: :r end op end |
.on_write(chan:, obj:, &blk) ⇒ Object
on_write
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/rbgo/select_chan.rb', line 364 def on_write(chan:, obj:, &blk) raise ArgumentError.new('chan must be a Chan') unless chan.is_a? Chan op = Proc.new do res = chan.enq(obj, true) res = blk.call unless blk.nil? res end op.define_singleton_method(:register) do |cond| chan.send :register, observer: cond, mode: :w end op.define_singleton_method(:unregister) do |cond| chan.send :unregister, observer: cond, mode: :w end op end |
.select_chan(*ops) ⇒ Object
select_chan
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/rbgo/select_chan.rb', line 299 def select_chan(*ops) ops.shuffle! mutex = Mutex.new cond = ConditionVariable.new loop do ops.each do |op| begin return op.call rescue ThreadError end end return yield if block_given? ops.each do |op| op.register(cond) end mutex.synchronize do cond.wait mutex end end ensure ops.each do |op| op.unregister(cond) end end |