/// @since 1.4
/// @brief An abstraction for finding and executing programs.
class Program {
+ /// Opaque handle for target specific data.
+ void *Data;
+
unsigned Pid_;
// Noncopyable.
/// @{
public:
- Program() : Pid_(0)
- {}
-
+ Program();
+ ~Program();
+
/// Return process ID of this program.
unsigned GetPid() { return Pid_; }
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) {
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) {
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";
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.
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)