Get rid of the Pid_ member in the Program class.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81247 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Mikhail Glushenkov
2009-09-08 19:50:55 +00:00
parent a607202a68
commit edf8e48c21
3 changed files with 31 additions and 24 deletions

View File

@@ -30,9 +30,7 @@ namespace sys {
/// @brief An abstraction for finding and executing programs. /// @brief An abstraction for finding and executing programs.
class Program { class Program {
/// Opaque handle for target specific data. /// Opaque handle for target specific data.
void *Data; void *Data_;
unsigned Pid_;
// Noncopyable. // Noncopyable.
Program(const Program& other); Program(const Program& other);
@@ -46,7 +44,7 @@ namespace sys {
~Program(); ~Program();
/// Return process ID of this program. /// Return process ID of this program.
unsigned GetPid() { return Pid_; } unsigned GetPid();
/// This function executes the program using the \p arguments provided. The /// This function executes the program using the \p arguments provided. The
/// invoked program will inherit the stdin, stdout, and stderr file /// invoked program will inherit the stdin, stdout, and stderr file

View File

@@ -34,10 +34,14 @@
namespace llvm { namespace llvm {
using namespace sys; using namespace sys;
Program::Program() : Pid_(0) {} Program::Program() : Data_(0) {}
Program::~Program() {} Program::~Program() {}
unsigned Program::GetPid() {
return reinterpret_cast<unsigned>(Data_);
}
// This function just uses the PATH environment variable to find the program. // This function just uses the PATH environment variable to find the program.
Path Path
Program::FindProgramByName(const std::string& progName) { Program::FindProgramByName(const std::string& progName) {
@@ -209,7 +213,7 @@ Program::Execute(const Path& path,
break; break;
} }
Pid_ = child; Data_ = reinterpret_cast<void*>(child);
return true; return true;
} }
@@ -221,7 +225,7 @@ Program::Wait(unsigned secondsToWait,
#ifdef HAVE_SYS_WAIT_H #ifdef HAVE_SYS_WAIT_H
struct sigaction Act, Old; struct sigaction Act, Old;
if (Pid_ == 0) { if (Data_ == 0) {
MakeErrMsg(ErrMsg, "Process not started!"); MakeErrMsg(ErrMsg, "Process not started!");
return -1; return -1;
} }
@@ -237,7 +241,7 @@ Program::Wait(unsigned secondsToWait,
// Parent process: Wait for the child process to terminate. // Parent process: Wait for the child process to terminate.
int status; int status;
int child = this->Pid_; pid_t child = reinterpret_cast<pid_t>(Data_);
while (wait(&status) != child) while (wait(&status) != child)
if (secondsToWait && errno == EINTR) { if (secondsToWait && errno == EINTR) {
// Kill the child. // Kill the child.
@@ -285,12 +289,13 @@ Program::Wait(unsigned secondsToWait,
bool bool
Program::Kill(std::string* ErrMsg) { Program::Kill(std::string* ErrMsg) {
if (Pid_ == 0) { if (Data_ == 0) {
MakeErrMsg(ErrMsg, "Process not started!"); MakeErrMsg(ErrMsg, "Process not started!");
return true; return true;
} }
return (kill(Pid_, SIGKILL) == 0); pid_t pid = reinterpret_cast<pid_t>(Data_);
return (kill(pid, SIGKILL) == 0);
} }
bool Program::ChangeStdinToBinary(){ bool Program::ChangeStdinToBinary(){

View File

@@ -25,16 +25,21 @@
namespace llvm { namespace llvm {
using namespace sys; using namespace sys;
Program::Program() : Pid_(0), Data(0) {} Program::Program() : Data_(0) {}
Program::~Program() { Program::~Program() {
if (Data) { if (Data_) {
HANDLE hProcess = (HANDLE) Data; HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
CloseHandle(hProcess); CloseHandle(hProcess);
Data = 0; Data_ = 0;
} }
} }
unsigned Program::GetPid() {
HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
return GetProcessId(hProcess);
}
// This function just uses the PATH environment variable to find the program. // This function just uses the PATH environment variable to find the program.
Path Path
Program::FindProgramByName(const std::string& progName) { Program::FindProgramByName(const std::string& progName) {
@@ -132,10 +137,10 @@ Program::Execute(const Path& path,
const Path** redirects, const Path** redirects,
unsigned memoryLimit, unsigned memoryLimit,
std::string* ErrMsg) { std::string* ErrMsg) {
if (Data) { if (Data_) {
HANDLE hProcess = (HANDLE) Data; HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
CloseHandle(Data); CloseHandle(Data_);
Data = 0; Data_ = 0;
} }
if (!path.canExecute()) { if (!path.canExecute()) {
@@ -264,8 +269,7 @@ Program::Execute(const Path& path,
path.str() + "'"); path.str() + "'");
return false; return false;
} }
Pid_ = pi.dwProcessId; Data_ = reinterpret_cast<void*>(pi.hProcess);
Data = pi.hProcess;
// Make sure these get closed no matter what. // Make sure these get closed no matter what.
AutoHandle hThread(pi.hThread); AutoHandle hThread(pi.hThread);
@@ -301,12 +305,12 @@ Program::Execute(const Path& path,
int int
Program::Wait(unsigned secondsToWait, Program::Wait(unsigned secondsToWait,
std::string* ErrMsg) { std::string* ErrMsg) {
if (Data == 0) { if (Data_ == 0) {
MakeErrMsg(ErrMsg, "Process not started!"); MakeErrMsg(ErrMsg, "Process not started!");
return -1; return -1;
} }
HANDLE hProcess = (HANDLE) Data; HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
// Wait for the process to terminate. // Wait for the process to terminate.
DWORD millisecondsToWait = INFINITE; DWORD millisecondsToWait = INFINITE;
@@ -337,12 +341,12 @@ Program::Wait(unsigned secondsToWait,
bool bool
Program::Kill(std::string* ErrMsg) { Program::Kill(std::string* ErrMsg) {
if (Data == 0) { if (Data_ == 0) {
MakeErrMsg(ErrMsg, "Process not started!"); MakeErrMsg(ErrMsg, "Process not started!");
return true; return true;
} }
HANDLE hProcess = reinterpret_cast<HANDLE>(Data); HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
return TerminateProcess(hProcess, 1); return TerminateProcess(hProcess, 1);
} }