16 #include <sys/types.h>
31 LockFileManager::readLockFile(
StringRef LockFileName) {
47 PIDStr = PIDStr.
substr(PIDStr.find_first_not_of(
" "));
49 if (!PIDStr.getAsInteger(10, PID))
50 return std::make_pair(std::string(Hostname), PID);
57 bool LockFileManager::processStillExecuting(
StringRef Hostname,
int PID) {
58 #if LLVM_ON_UNIX && !defined(__ANDROID__)
62 gethostname(MyHostname, 255);
64 if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
71 LockFileManager::LockFileManager(
StringRef FileName)
73 this->FileName = FileName;
74 LockFileName = FileName;
75 LockFileName +=
".lock";
79 if ((Owner = readLockFile(LockFileName)))
83 UniqueLockFileName = LockFileName;
84 UniqueLockFileName +=
"-%%%%%%%%";
89 UniqueLockFileName)) {
103 gethostname(hostname, 255);
104 Out << hostname <<
' ' << getpid();
106 Out <<
"localhost 1";
133 if (
stat(UniqueLockFileName.
c_str(), &StatBuf) == 0 &&
134 StatBuf.st_nlink == 2)
142 if ((Owner = readLockFile(LockFileName)))
178 struct timespec Interval;
180 Interval.tv_nsec = 1000000;
183 unsigned MaxSeconds = 300;
184 bool LockFileGone =
false;
193 nanosleep(&Interval, NULL);
196 bool LockFileJustDisappeared =
false;
203 LockFileJustDisappeared =
true;
218 if (LockFileJustDisappeared) {
225 Interval.tv_nsec = 1000000;
234 !processStillExecuting((*Owner).first, (*Owner).second)) {
242 Interval.tv_sec *= 2;
243 Interval.tv_nsec *= 2;
244 if (Interval.tv_nsec >= 1000000000) {
246 Interval.tv_nsec -= 1000000000;
251 Interval < MaxSeconds * 1000
253 Interval.tv_sec < (time_t)MaxSeconds
StringRef substr(size_t Start, size_t N=npos) const
StringRef getBuffer() const
std::string str() const
str - Get the contents as an std::string.
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
An error occurred while trying to create or find the lock file.
error_code create_hard_link(const Twine &to, const Twine &from)
Create a hard link from from to to.
LockFileState getState() const
Determine the state of the lock file.
LockFileState
Describes the state of a lock file.
void waitForUnlock()
For a shared lock, wait until the owner releases the lock.
int stat(const char *path, struct stat *buf);
error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
static error_code getFile(Twine Filename, OwningPtr< MemoryBuffer > &result, int64_t FileSize=-1, bool RequiresNullTerminator=true)
StringRef str() const
Explicit conversion to StringRef.
error_code remove(const Twine &path, bool &existed)
Remove path. Equivalent to POSIX remove().
The lock file has been created and is owned by this instance of the object.
bool exists(file_status status)
Does file exist?
The lock file already exists and is owned by some other instance.
tier< T1, T2 > tie(T1 &f, T2 &s)
error_code make_error_code(errc _e)