From 18c6c6987165cb9387cddd70aef17d785ca7d07d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 23 May 2017 15:24:52 -0400 Subject: [PATCH] do not use ThreadsWait to wait for threads; it does not work properly --- lib/rscons/environment.rb | 45 +++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 58b68b0..dea7ea6 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -1,7 +1,6 @@ require "fileutils" require "set" require "shellwords" -require "thwait" module Rscons # The Environment class is the main programmatic interface to Rscons. It @@ -847,23 +846,9 @@ module Rscons # @return [ThreadedCommand, nil] # The {ThreadedCommand} object that is finished. 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 threads = options[:which].map(&:thread) - tw = ThreadsWait.new(*threads) - finished_thread = - begin - tw.next_wait(options[:nonblock]) - rescue ThreadsWait::ErrNoFinishedThread - nil - end - if finished_thread + if finished_thread = find_finished_thread(threads, options[:nonblock]) threaded_command = @threaded_commands.find do |tc| tc.thread == finished_thread end @@ -872,6 +857,34 @@ module Rscons end end + # Check if any of the requested threads are finished. + # + # @param threads [Array] + # 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. # # @param command [Array]