Skip to content

Commit

Permalink
added a waiting for lock message
Browse files Browse the repository at this point in the history
  • Loading branch information
dentalfloss1 committed May 13, 2024
1 parent 1233200 commit a3bfba3
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/fileutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

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

#if defined(HAVE_CXX_FILESYSTEM) && HAVE_CXX_FILESYSTEM
#include <filesystem>
#include <iostream>
#endif

std::string get_home_dir(void) {
Expand Down Expand Up @@ -193,16 +195,27 @@ 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) {
auto t1 = std::chrono::high_resolution_clock::now();
int counter = 0;
while( true ) {
_fd = open(_lockfile.c_str(), O_CREAT, 600);
flock(_fd, LOCK_EX);
struct stat fd_stat, lockfile_stat;
fstat(_fd, &fd_stat);
stat(_lockfile.c_str(), &lockfile_stat);
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(t2-t1).count();
if (counter==5000000){
std::cout << "Waiting for "<< lockfile<<" lock for " << duration << " seconds" << std::endl;
counter=0;
}

counter++;
// Compare inodes
if( fd_stat.st_ino == lockfile_stat.st_ino ) {
// Got the lock
break;

}
close(_fd);
}
Expand Down

0 comments on commit a3bfba3

Please sign in to comment.