diff options
-rw-r--r-- | include/llvm/System/Program.h | 9 | ||||
-rw-r--r-- | lib/System/Unix/Program.inc | 4 | ||||
-rw-r--r-- | lib/System/Win32/Program.inc | 26 |
3 files changed, 31 insertions, 8 deletions
diff --git a/include/llvm/System/Program.h b/include/llvm/System/Program.h index 177fa7c..7f96245 100644 --- a/include/llvm/System/Program.h +++ b/include/llvm/System/Program.h @@ -29,6 +29,9 @@ namespace sys { /// @since 1.4 /// @brief An abstraction for finding and executing programs. class Program { + /// Opaque handle for target specific data. + void *Data; + unsigned Pid_; // Noncopyable. @@ -39,9 +42,9 @@ namespace sys { /// @{ public: - Program() : Pid_(0) - {} - + Program(); + ~Program(); + /// Return process ID of this program. unsigned GetPid() { return Pid_; } diff --git a/lib/System/Unix/Program.inc b/lib/System/Unix/Program.inc index 3c5a54c..3a562e4 100644 --- a/lib/System/Unix/Program.inc +++ b/lib/System/Unix/Program.inc @@ -35,6 +35,10 @@ namespace llvm { using namespace sys; +Program::Program() : Pid_(0) {} + +Program::~Program() {} + // This function just uses the PATH environment variable to find the program. Path Program::FindProgramByName(const std::string& progName) { diff --git a/lib/System/Win32/Program.inc b/lib/System/Win32/Program.inc index 963946a..005f631 100644 --- a/lib/System/Win32/Program.inc +++ b/lib/System/Win32/Program.inc @@ -25,6 +25,16 @@ namespace llvm { using namespace sys; +Program::Program() : Pid_(0), Data(0) {} + +Program::~Program() { + if (Data) { + HANDLE hProcess = (HANDLE) Data; + CloseHandle(hProcess); + Data = 0; + } +} + // This function just uses the PATH environment variable to find the program. Path Program::FindProgramByName(const std::string& progName) { @@ -122,6 +132,12 @@ Program::Execute(const Path& path, const Path** redirects, unsigned memoryLimit, std::string* ErrMsg) { + if (Data) { + HANDLE hProcess = (HANDLE) Data; + CloseHandle(Data); + Data = 0; + } + if (!path.canExecute()) { if (ErrMsg) *ErrMsg = "program not executable"; @@ -250,9 +266,9 @@ Program::Execute(const Path& path, return false; } Pid_ = pi.dwProcessId; - + Data = pi.hProcess; + // Make sure these get closed no matter what. - AutoHandle hProcess(pi.hProcess); AutoHandle hThread(pi.hThread); // Assign the process to a job if a memory limit is defined. @@ -286,13 +302,13 @@ Program::Execute(const Path& path, int Program::Wait(unsigned secondsToWait, std::string* ErrMsg) { - if (Pid_ == 0) { + if (Data == 0) { MakeErrMsg(ErrMsg, "Process not started!"); return -1; } - AutoHandle hProcess = OpenProcess(SYNCHRONIZE, FALSE, Pid_); - + HANDLE hProcess = (HANDLE) Data; + // Wait for the process to terminate. DWORD millisecondsToWait = INFINITE; if (secondsToWait > 0) |