]> git.tdb.fi Git - builder.git/commitdiff
Be more efficient when running commands synchronously
authorMikko Rasa <tdb@tdb.fi>
Sat, 14 Jul 2012 19:21:22 +0000 (22:21 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 15 Jul 2012 14:05:48 +0000 (17:05 +0300)
source/externaltask.cpp
source/externaltask.h

index fea331a72207004e14acd85b5ebfb94fb910a840..5f5d394597b3a7d073d80bc2caa830172ba97d92 100644 (file)
@@ -91,12 +91,21 @@ void ExternalTask::start()
 }
 
 Task::Status ExternalTask::check()
+{
+       return do_wait(false);
+}
+
+Task::Status ExternalTask::wait()
+{
+       return do_wait(true);
+}
+
+Task::Status ExternalTask::do_wait(bool block)
 {
        if(pid>0)
        {
-               // XXX This is sub-optimal, should have support for a blocking wait
                int status;
-               if(waitpid(pid, &status, WNOHANG)==pid)
+               if(waitpid(pid, &status, (block ? 0 : WNOHANG))==pid)
                {
                        if(WIFEXITED(status))
                                exit_code = WEXITSTATUS(status);
@@ -145,9 +154,7 @@ string ExternalTask::run_and_capture_output(const Arguments &argv, const FS::Pat
        task.set_stdout(CAPTURE);
        task.set_stderr(IGNORE);
        task.start();
-       Task::Status status;
-       while((status=task.check())==RUNNING) ;
-       if(status!=SUCCESS)
+       if(task.wait()!=SUCCESS)
                throw runtime_error(format("%s failed", argv.front()));
        return task.get_output();
 }
index d997f9a7b4891ad473aea0d2cfdffb8bcd53bef5..a5f10ec0b0c1486ce6b7b951d8dd10e668923cfb 100644 (file)
@@ -45,7 +45,11 @@ public:
        virtual std::string get_command() const;
        virtual void start();
        virtual Status check();
+       Status wait();
+private:
+       Status do_wait(bool);
 
+public:
        void set_stdout(Destination);
        void set_stderr(Destination);
        const std::string &get_output() const { return output; }