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


369
370
371
372
373
# File 'lib/celluloid/io/stream.rb', line 369

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


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

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