From: Mikko Rasa Date: Sat, 14 Jul 2012 19:21:22 +0000 (+0300) Subject: Be more efficient when running commands synchronously X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=666bf60f80a3f220a112c2a97ed00b72871924d3;p=builder.git Be more efficient when running commands synchronously --- diff --git a/source/externaltask.cpp b/source/externaltask.cpp index fea331a..5f5d394 100644 --- a/source/externaltask.cpp +++ b/source/externaltask.cpp @@ -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(); } diff --git a/source/externaltask.h b/source/externaltask.h index d997f9a..a5f10ec 100644 --- a/source/externaltask.h +++ b/source/externaltask.h @@ -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; }