do not use ThreadsWait to wait for threads; it does not work properly

This commit is contained in:
Josh Holtrop 2017-05-23 15:24:52 -04:00
parent 2ffdf82d9a
commit 18c6c69871

View File

@ -1,7 +1,6 @@
require "fileutils" require "fileutils"
require "set" require "set"
require "shellwords" require "shellwords"
require "thwait"
module Rscons module Rscons
# The Environment class is the main programmatic interface to Rscons. It # The Environment class is the main programmatic interface to Rscons. It
@ -847,23 +846,9 @@ module Rscons
# @return [ThreadedCommand, nil] # @return [ThreadedCommand, nil]
# The {ThreadedCommand} object that is finished. # The {ThreadedCommand} object that is finished.
def wait_for_threaded_commands(options = {}) def wait_for_threaded_commands(options = {})
if @threaded_commands.empty?
if options[:nonblock]
return nil
else
raise "No threaded commands to wait for"
end
end
options[:which] ||= @threaded_commands options[:which] ||= @threaded_commands
threads = options[:which].map(&:thread) threads = options[:which].map(&:thread)
tw = ThreadsWait.new(*threads) if finished_thread = find_finished_thread(threads, options[:nonblock])
finished_thread =
begin
tw.next_wait(options[:nonblock])
rescue ThreadsWait::ErrNoFinishedThread
nil
end
if finished_thread
threaded_command = @threaded_commands.find do |tc| threaded_command = @threaded_commands.find do |tc|
tc.thread == finished_thread tc.thread == finished_thread
end end
@ -872,6 +857,34 @@ module Rscons
end end
end end
# Check if any of the requested threads are finished.
#
# @param threads [Array<Thread>]
# The threads to check.
# @param nonblock [Boolean]
# Whether to be non-blocking. If true, nil will be returned if no thread
# is finished. If false, the method will wait until one of the threads
# is finished.
#
# @return [Thread, nil]
# The finished thread, if any.
def find_finished_thread(threads, nonblock)
if nonblock
threads.find do |thread|
!thread.alive?
end
else
if threads.empty?
raise "No threads to wait for"
end
loop do
thread = find_finished_thread(threads, true)
return thread if thread
sleep 0.1
end
end
end
# Return a string representation of a command. # Return a string representation of a command.
# #
# @param command [Array<String>] # @param command [Array<String>]