1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

Fix a race condition in getting the process exit code on Win32.

llvm-svn: 77953
This commit is contained in:
Daniel Dunbar 2009-08-03 05:02:46 +00:00
parent 2781e4bd89
commit c5047d9d96
3 changed files with 31 additions and 8 deletions

View File

@ -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_; }

View File

@ -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) {

View File

@ -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)