From 3ef37ce1888e7b4ff152cc570f970d9f238d4153 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 12 Jun 2019 12:27:28 +0100
Subject: [PATCH] BUG: fileMonitor: do not allow used watch also in free
 watches.

See #1328.
---
 .../MSwindows/fileMonitor/fileMonitor.C         | 17 +++++++++++------
 src/OSspecific/POSIX/fileMonitor/fileMonitor.C  | 17 +++++++++++------
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/src/OSspecific/MSwindows/fileMonitor/fileMonitor.C b/src/OSspecific/MSwindows/fileMonitor/fileMonitor.C
index 425a0c333d0..b40c49c4846 100644
--- a/src/OSspecific/MSwindows/fileMonitor/fileMonitor.C
+++ b/src/OSspecific/MSwindows/fileMonitor/fileMonitor.C
@@ -450,14 +450,16 @@ Foam::fileMonitor::~fileMonitor()
 // regIOobject)
 Foam::label Foam::fileMonitor::addWatch(const fileName& fName)
 {
-    label watchFd;
+    if (debug)
+    {
+        Pout<< "fileMonitor : adding watch on file " << fName << endl;
+    }
 
-    label sz = freeWatchFds_.size();
+    label watchFd;
 
-    if (sz)
+    if (freeWatchFds_.size())
     {
-        watchFd = freeWatchFds_[sz-1];
-        freeWatchFds_.setSize(sz-1);
+        watchFd = freeWatchFds_.remove();
     }
     else
     {
@@ -495,7 +497,10 @@ bool Foam::fileMonitor::removeWatch(const label watchFd)
             << watchFile_[watchFd] << endl;
     }
 
-    freeWatchFds_.append(watchFd);
+    if (!freeWatchFds_.found(watchFd))
+    {
+        freeWatchFds_.append(watchFd);
+    }
     return watcher_->removeWatch(watchFd);
 }
 
diff --git a/src/OSspecific/POSIX/fileMonitor/fileMonitor.C b/src/OSspecific/POSIX/fileMonitor/fileMonitor.C
index f8f9a46af72..80b5b4044de 100644
--- a/src/OSspecific/POSIX/fileMonitor/fileMonitor.C
+++ b/src/OSspecific/POSIX/fileMonitor/fileMonitor.C
@@ -446,14 +446,16 @@ Foam::fileMonitor::~fileMonitor()
 // regIOobject)
 Foam::label Foam::fileMonitor::addWatch(const fileName& fName)
 {
-    label watchFd;
+    if (debug)
+    {
+        Pout<< "fileMonitor : adding watch on file " << fName << endl;
+    }
 
-    label sz = freeWatchFds_.size();
+    label watchFd;
 
-    if (sz)
+    if (freeWatchFds_.size())
     {
-        watchFd = freeWatchFds_[sz-1];
-        freeWatchFds_.setSize(sz-1);
+        watchFd = freeWatchFds_.remove();
     }
     else
     {
@@ -491,7 +493,10 @@ bool Foam::fileMonitor::removeWatch(const label watchFd)
             << watchFile_[watchFd] << endl;
     }
 
-    freeWatchFds_.append(watchFd);
+    if (!freeWatchFds_.found(watchFd))
+    {
+        freeWatchFds_.append(watchFd);
+    }
     return watcher_->removeWatch(watchFd);
 }
 
-- 
GitLab