From 9a862bc75ae56ec0c0be6e91db343b63b0087d08 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 14 Aug 2011 11:54:53 -0400 Subject: [PATCH] File - locking - Made locking an integral part of the File object, so that a client may now call File::lock multiple times with no effect beyond the first call. This simplifies situations like the 'cal' command that must scan for recurrence changes, modify tasks, then optionally run a second report, all without locking issues. --- src/File.cpp | 20 +++++++++++++++++++- src/File.h | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/File.cpp b/src/File.cpp index 7a818f5dc..884742555 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -42,6 +42,7 @@ File::File () : Path::Path () , fh (NULL) , h (-1) +, locked (false) { } @@ -50,6 +51,7 @@ File::File (const Path& other) : Path::Path (other) , fh (NULL) , h (-1) +, locked (false) { } @@ -58,6 +60,7 @@ File::File (const File& other) : Path::Path (other) , fh (NULL) , h (-1) +, locked (false) { } @@ -66,6 +69,7 @@ File::File (const std::string& in) : Path::Path (in) , fh (NULL) , h (-1) +, locked (false) { } @@ -82,6 +86,7 @@ File& File::operator= (const File& other) if (this != &other) Path::operator= (other); + locked = false; return *this; } @@ -117,6 +122,7 @@ bool File::open () if (fh) { h = fileno (fh); + locked = false; return true; } } @@ -138,6 +144,7 @@ void File::close () fclose (fh); fh = NULL; h = -1; + locked = false; } } @@ -152,17 +159,28 @@ bool File::lock () ; if (retry <= 3) + { + locked = true; return true; + } } + locked = false; return false; } //////////////////////////////////////////////////////////////////////////////// bool File::waitForLock () { + if (locked) + return true; + if (fh && h != -1) - return flock (h, LOCK_EX) == 0 ? true : false; + if (flock (h, LOCK_EX) == 0) + { + locked = true; + return true; + } return false; } diff --git a/src/File.h b/src/File.h index 2c5ccc292..5a60bbd8f 100644 --- a/src/File.h +++ b/src/File.h @@ -82,6 +82,7 @@ public: private: FILE* fh; int h; + bool locked; }; #endif