File: notification.rb

package info (click to toggle)
ruby-async 2.36.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 400 kB
  • sloc: ruby: 1,938; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2025, by Samuel Williams.

require_relative "condition"

module Async
	# A synchronization primitive, which allows fibers to wait until a notification is received. Does not block the task which signals the notification. Waiting tasks are resumed on next iteration of the reactor.
	# @public Since *Async v1*.
	class Notification < Condition
		# Signal to a given task that it should resume operations.
		#
		# @returns [Boolean] if a task was signalled.
		def signal(value = nil)
			return false if empty?
			
			Fiber.scheduler.push Signal.new(self.exchange, value)
			
			return true
		end
		
		Signal = Struct.new(:ready, :value) do
			def alive?
				true
			end
			
			def transfer
				ready.num_waiting.times do
					ready.push(value)
				end
				
				ready.close
			end
		end
		
		private_constant :Signal
	end
end