Sha256: 3226e7598696bbe98c8289584369198685374d6ae8d7117624db9fb694a42395
Contents?: true
Size: 1.26 KB
Versions: 3
Compression:
Stored size: 1.26 KB
Contents
# frozen_string_literal: true # Released under the MIT License. # Copyright, 2017-2022, by Samuel Williams. # Copyright, 2017, by Kent Gruber. require 'fiber' require_relative 'node' module Async # A synchronization primitive, which allows fibers to wait until a particular condition is (edge) triggered. # @public Since `stable-v1`. class Condition def initialize @waiting = [] end Queue = Struct.new(:fiber) do def transfer(*arguments) fiber&.transfer(*arguments) end def alive? fiber&.alive? end def nullify self.fiber = nil end end private_constant :Queue # Queue up the current fiber and wait on yielding the task. # @returns [Object] def wait queue = Queue.new(Fiber.current) @waiting << queue Fiber.scheduler.transfer ensure queue.nullify end # Is any fiber waiting on this notification? # @returns [Boolean] def empty? @waiting.empty? end # Signal to a given task that it should resume operations. # @parameter value [Object | Nil] The value to return to the waiting fibers. def signal(value = nil) waiting = @waiting @waiting = [] waiting.each do |fiber| Fiber.scheduler.resume(fiber, value) if fiber.alive? end return nil end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
async-2.2.1 | lib/async/condition.rb |
async-2.2.0 | lib/async/condition.rb |
async-2.1.0 | lib/async/condition.rb |