Get idle counters at once (#294)

to avoid a race with usage==0 & openCount>0
This commit is contained in:
Ben RUBSON 2017-03-10 08:47:04 +01:00 committed by rfjakob
parent efbdd29716
commit 618db2374c
3 changed files with 12 additions and 14 deletions

View File

@ -74,20 +74,15 @@ void EncFS_Context::setRoot(const std::shared_ptr<DirNode> &r) {
bool EncFS_Context::isMounted() { return root.get() != nullptr; }
int EncFS_Context::getAndResetUsageCounter() {
void EncFS_Context::getAndResetUsageCounter(int *usage, int *openCount) {
Lock lock(contextMutex);
int count = usageCount;
*usage = usageCount;
usageCount = 0;
return count;
*openCount = openFiles.size();
}
int EncFS_Context::openFileCount() const {
Lock lock(contextMutex);
return openFiles.size();
}
std::shared_ptr<FileNode> EncFS_Context::lookupNode(const char *path) {
Lock lock(contextMutex);

View File

@ -44,8 +44,7 @@ class EncFS_Context {
std::shared_ptr<FileNode> lookupNode(const char *path);
int getAndResetUsageCounter();
int openFileCount() const;
void getAndResetUsageCounter(int *usage, int *openCount);
FileNode *putNode(const char *path, std::shared_ptr<FileNode> &&node);

View File

@ -718,15 +718,19 @@ static void *idleMonitor(void *_arg) {
pthread_mutex_lock(&ctx->wakeupMutex);
while (ctx->running) {
int usage = ctx->getAndResetUsageCounter();
int usage, openCount;
ctx->getAndResetUsageCounter(&usage, &openCount);
if (usage == 0 && ctx->isMounted())
++idleCycles;
else
else {
if (idleCycles >= timeoutCycles)
RLOG(INFO) << "Filesystem no longer inactive: "
<< arg->opts->mountPoint;
idleCycles = 0;
}
if (idleCycles >= timeoutCycles) {
int openCount = ctx->openFileCount();
if (openCount == 0) {
if (unmountFS(ctx)) {
// wait for main thread to wake us up