lib/kernel/sync.rb in async-2.17.0 vs lib/kernel/sync.rb in async-2.18.0

- old
+ new

@@ -1,10 +1,11 @@ # frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2024, by Samuel Williams. # Copyright, 2020, by Brian Morearty. +# Copyright, 2024, by Patrik Wenger. require_relative "../async/reactor" # Extensions to all Ruby objects. module Kernel @@ -13,20 +14,24 @@ # @yields {|task| ...} The block that will execute asynchronously. # @parameter task [Async::Task] The task that is executing the given block. # # @public Since `stable-v1`. # @asynchronous Will block until given block completes executing. - def Sync(&block) + def Sync(annotation: nil, &block) if task = ::Async::Task.current? - yield task + if annotation + task.annotate(annotation) {yield task} + else + yield task + end elsif scheduler = Fiber.scheduler ::Async::Task.run(scheduler, &block).wait else # This calls Fiber.set_scheduler(self): reactor = Async::Reactor.new begin - return reactor.run(finished: ::Async::Condition.new, &block).wait + return reactor.run(annotation: annotation, finished: ::Async::Condition.new, &block).wait ensure Fiber.set_scheduler(nil) end end end