Class: Celluloid::IO::Stream::Latch

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/io/stream.rb

Overview

Perform an operation exclusively, uncontested by other tasks

Instance Method Summary collapse

Constructor Details

#initializeLatch

Returns a new instance of Latch.



366
367
368
369
370
# File 'lib/celluloid/io/stream.rb', line 366

def initialize
  @owner = nil
  @waiters = 0
  @condition = Celluloid::Condition.new
end

Instance Method Details

#synchronizeObject

Synchronize an operation across all tasks in the current actor



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/celluloid/io/stream.rb', line 373

def synchronize
  actor = Thread.current[:celluloid_actor]
  return yield unless actor

  if @owner || @waiters > 0
    @waiters += 1
    @condition.wait
    @waiters -= 1
  end

  @owner = Task.current

  begin
    ret = yield
  ensure
    @owner = nil
    @condition.signal if @waiters > 0
  end

  ret
end