Sha256: 2d39be6d2be97d68848ad0420c84b24c2c39bc8342d7a992e45f451e60262c21

Contents?: true

Size: 1.16 KB

Versions: 12

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.

require_relative 'notification'

module Async
	module IO
		class Threads
			def initialize(parent: nil)
				@parent = parent
			end
			
			if Async::Scheduler.supported?
				def async(parent: (@parent or Task.current))
					parent.async do
						thread = ::Thread.new do
							yield
						end
						
						thread.join
					rescue Stop
						if thread&.alive?
							thread.raise(Stop)
						end
						
						begin
							thread.join
						rescue Stop
							# Ignore.
						end
					end
				end
			else
				def async(parent: (@parent or Task.current))
					parent.async do |task|
						notification = Async::IO::Notification.new
						
						thread = ::Thread.new do
							yield
						ensure
							notification.signal
						end
						
						task.annotate "Waiting for thread to finish..."
						
						notification.wait
						
						thread.value
					ensure
						if thread&.alive?
							thread.raise(Stop)
							
							begin
								thread.join
							rescue Stop
								# Ignore.
							end
						end
						
						notification&.close
					end
				end
			end
		end
	end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
async-io-1.43.2 lib/async/io/threads.rb
async-io-1.43.1 lib/async/io/threads.rb
async-io-1.43.0 lib/async/io/threads.rb
async-io-1.42.1 lib/async/io/threads.rb
async-io-1.42.0 lib/async/io/threads.rb
async-io-1.41.0 lib/async/io/threads.rb
async-io-1.39.0 lib/async/io/threads.rb
async-io-1.38.1 lib/async/io/threads.rb
async-io-1.38.0 lib/async/io/threads.rb
async-io-1.37.0 lib/async/io/threads.rb
async-io-1.36.1 lib/async/io/threads.rb
async-io-1.36.0 lib/async/io/threads.rb