Sha256: 684286561a83a029589413435c94b59487e717a6e05fe9e6f9187071042a3972

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2017-2024, by Samuel Williams.
# Copyright, 2017, by Kent Gruber.

require "fiber"
require_relative "list"

module Async
	# A synchronization primitive, which allows fibers to wait until a particular condition is (edge) triggered.
	# @public Since *Async v1*.
	class Condition
		# Create a new condition.
		def initialize
			@waiting = List.new
		end
		
		class FiberNode < List::Node
			def initialize(fiber)
				@fiber = fiber
			end
			
			def transfer(*arguments)
				@fiber.transfer(*arguments)
			end
			
			def alive?
				@fiber.alive?
			end
		end
		
		private_constant :FiberNode
		
		# Queue up the current fiber and wait on yielding the task.
		# @returns [Object]
		def wait
			@waiting.stack(FiberNode.new(Fiber.current)) do
				Fiber.scheduler.transfer
			end
		end
		
		# @deprecated Replaced by {#waiting?}
		def empty?
			@waiting.empty?
		end
		
		# @returns [Boolean] Is any fiber waiting on this notification?
		def waiting?
			@waiting.size > 0
		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)
			return if @waiting.empty?
			
			waiting = self.exchange
			
			waiting.each do |fiber|
				Fiber.scheduler.resume(fiber, value) if fiber.alive?
			end
			
			return nil
		end
		
		protected
		
		def exchange
			waiting = @waiting
			@waiting = List.new
			return waiting
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
async-2.21.0 lib/async/condition.rb
async-2.20.0 lib/async/condition.rb