diff options
author | Evan Martin <martine@danga.com> | 2010-12-05 00:09:50 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2010-12-05 00:09:50 (GMT) |
commit | 42d237e7841679b27cd5dcae82d143d86a87a807 (patch) | |
tree | 253b0cf88ecf3b0404146a7c7945dd4c660afa04 /src/subprocess.h | |
parent | d0025e234cf3fc0a13f45d179880578285609b59 (diff) | |
download | Ninja-42d237e7841679b27cd5dcae82d143d86a87a807.zip Ninja-42d237e7841679b27cd5dcae82d143d86a87a807.tar.gz Ninja-42d237e7841679b27cd5dcae82d143d86a87a807.tar.bz2 |
move src into subdir
Diffstat (limited to 'src/subprocess.h')
-rw-r--r-- | src/subprocess.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/subprocess.h b/src/subprocess.h new file mode 100644 index 0000000..2c91f4a --- /dev/null +++ b/src/subprocess.h @@ -0,0 +1,42 @@ +#include <string> +#include <vector> +#include <queue> +using namespace std; + +// Subprocess wraps a single async subprocess. It is entirely +// passive: it expects the caller to notify it when its fds are ready +// for reading, as well as call Finish() to reap the child once done() +// is true. +struct Subprocess { + Subprocess(); + ~Subprocess(); + bool Start(const string& command); + void OnFDReady(int fd); + // Returns true on successful process exit. + bool Finish(); + + bool done() const { + return stdout_.fd_ == -1 && stderr_.fd_ == -1; + } + + struct Stream { + Stream(); + ~Stream(); + int fd_; + string buf_; + }; + Stream stdout_, stderr_; + pid_t pid_; +}; + +// SubprocessSet runs a poll() loop around a set of Subprocesses. +// DoWork() waits for any state change in subprocesses; finished_ +// is a queue of subprocesses as they finish. +struct SubprocessSet { + void Add(Subprocess* subprocess); + void DoWork(); + Subprocess* NextFinished(); + + vector<Subprocess*> running_; + queue<Subprocess*> finished_; +}; |