Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
- Make LockDirectory and UnlockDirectory free functions
- Replace Utils::sleep_seconds with std sleep_for / chrono intervals
- Make DirectoryLock non-copyable and non-movable
  • Loading branch information
zmeyc committed Mar 8, 2021
1 parent c5fa08c commit bdc9db5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
89 changes: 47 additions & 42 deletions src/disk_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <sstream>
#include <fstream>
#include <iostream>
#include <thread>
#include <chrono>

#ifndef _WIN32
#include <sys/types.h>
Expand Down Expand Up @@ -94,46 +96,8 @@ namespace DiskUtil {
return false;
}
}
}

class DirectoryLock
{
public:
DirectoryLock(const std::string &dirname, bool lock = true)
{
dirname_ = dirname;
if (lock) {
Lock();
}
}

virtual ~DirectoryLock()
{
Unlock();
}

bool Lock()
{
if (fd_ == -1) {
fd_ = LockDirectory(dirname_);
}
return fd_ != -1;
}

bool Unlock()
{
if (fd_ == -1) {
return false;
}
if (!UnlockDirectory(fd_, dirname_)) {
return false;
}
fd_ = -1;
return true;
}

private:
static int LockDirectory(
static inline int LockDirectory(
std::string dirname)
{
#ifdef _WIN32
Expand All @@ -146,19 +110,20 @@ class DirectoryLock
return -1;
}
while (0 != flock(dir_fd, LOCK_EX | LOCK_NB)) {
using namespace std::chrono_literals;
if (EWOULDBLOCK == errno) {
Util::sleep_seconds(10);
std::this_thread::sleep_for(10s);
} else {
std::cerr << "Unable to lock directory (retrying in 1 minute): "
<< ". Error: " << strerror(errno) << std::endl;
Util::sleep_seconds(60);
std::this_thread::sleep_for(60s);
}
}
return dir_fd;
#endif
}

static bool UnlockDirectory(
static inline bool UnlockDirectory(
int dir_fd,
std::string dirname)
{
Expand All @@ -178,7 +143,47 @@ class DirectoryLock
return true;
#endif
}
}

class DirectoryLock
{
public:
DirectoryLock(const std::string &dirname, bool lock = true)
{
dirname_ = dirname;
if (lock) {
Lock();
}
}

DirectoryLock(const DirectoryLock&) = delete;

virtual ~DirectoryLock()
{
Unlock();
}

bool Lock()
{
if (fd_ == -1) {
fd_ = DiskUtil::LockDirectory(dirname_);
}
return fd_ != -1;
}

bool Unlock()
{
if (fd_ == -1) {
return false;
}
if (!DiskUtil::UnlockDirectory(fd_, dirname_)) {
return false;
}
fd_ = -1;
return true;
}

private:
int fd_ = -1;
std::string dirname_;
};
Expand Down
5 changes: 4 additions & 1 deletion src/plotter_disk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <string>
#include <vector>
#include <memory>
#include <thread>
#include <chrono>

#include "chia_filesystem.hpp"

Expand Down Expand Up @@ -420,7 +422,8 @@ class DiskPlotter {
}

if (!bRenamed) {
Util::sleep_seconds(5 * 60);
using namespace std::chrono_literals;
std::this_thread::sleep_for(5min);
}
} while (!bRenamed);
}
Expand Down
9 changes: 0 additions & 9 deletions src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,6 @@ namespace Util {
double b = ldexp(frac, exp);
return b;
}

void sleep_seconds(int seconds) {
#ifdef _WIN32
Sleep(seconds * 1000);
#else
sleep(seconds);
#endif
}

}

#endif // SRC_CPP_UTIL_HPP_

0 comments on commit bdc9db5

Please sign in to comment.