Skip to content

Commit

Permalink
Lockfile will warn periodically if waiting more than 5s
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
league committed Apr 21, 2024
1 parent b566729 commit 64c6f73
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/fileutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "fileutils.hpp"
#include <sstream>
#include <iostream>

#if defined(HAVE_CXX_FILESYSTEM) && HAVE_CXX_FILESYSTEM
#include <filesystem>
Expand Down Expand Up @@ -193,6 +194,9 @@ std::string get_dirname(std::string filename) {
potentially write the PID into the lock file to help with tracking whether
the process died. */
LockFile::LockFile(std::string lockfile) : _lockfile(lockfile) {
time_t start = time(NULL), elapsed = 0;
int busycount = 0;
int busyinterval = 1 << 17;
while( true ) {
_fd = open(_lockfile.c_str(), O_CREAT, 600);
flock(_fd, LOCK_EX);
Expand All @@ -202,13 +206,48 @@ LockFile::LockFile(std::string lockfile) : _lockfile(lockfile) {
// Compare inodes
if( fd_stat.st_ino == lockfile_stat.st_ino ) {
// Got the lock
if(elapsed > 0) {
std::cerr << "NOTE: acquired " << lockfile
<< std::endl;
}
break;
}
busycount++;
close(_fd);
if(busycount % busyinterval == 0) {
elapsed = time(NULL) - start;
if(elapsed >= 5) {
std::cerr << "NOTE: waiting " << elapsed
<< "s for " << lockfile << std::endl;
busyinterval = busycount;
}
}
}
}

LockFile::~LockFile() {
unlink(_lockfile.c_str());
flock(_fd, LOCK_UN);
}


#ifdef FILEUTILS_LOCKFILE_TEST
// This is a little test program for LockFile, to simulate
// a leftover lock file. It should look something like this:

// Initiating lock...
// NOTE: waiting 5s for ./myfile.lock
// NOTE: waiting 9s for ./myfile.lock
// NOTE: waiting 17s for ./myfile.lock
// NOTE: waiting 34s for ./myfile.lock
// [Delete the file from another terminal]
// NOTE: acquired ./myfile.lock
// Lock acquired... exiting
int main() {
int fd = open("./myfile.lock", O_CREAT, 600);
std::cerr << "Initiating lock..." << std::endl;
LockFile("./myfile.lock");
std::cerr << "Lock acquired... exiting" << std::endl;
return 0;
}
#endif

0 comments on commit 64c6f73

Please sign in to comment.