diff options
author | Shantanu Tushar <shantanu.tushar@kdab.com> | 2020-10-24 19:16:16 +0200 |
---|---|---|
committer | Shantanu Tushar <shantanu.tushar@kdab.com> | 2021-04-01 15:38:08 +0200 |
commit | 0592123a0c68b65c83555dc0f023c4bc77030f31 (patch) | |
tree | 83346e8e0e54d9fbd9c80734195eb9b320332711 | |
parent | b3281eb6f45b2ee43ed0137e8076136f78a75b1f (diff) |
Add std::chrono overloads for QLockFile functions
This makes it convenient to use QLockFile for projects which are able to
use std::chrono to denote durations.
Change-Id: Ib4520f6142412bdefe659fccc1e6d15b81af2f25
Reviewed-by: David Faure <david.faure@kdab.com>
-rw-r--r-- | src/corelib/io/qlockfile.cpp | 49 | ||||
-rw-r--r-- | src/corelib/io/qlockfile.h | 15 |
2 files changed, 64 insertions, 0 deletions
diff --git a/src/corelib/io/qlockfile.cpp b/src/corelib/io/qlockfile.cpp index ba2dc28339..9dc0102574 100644 --- a/src/corelib/io/qlockfile.cpp +++ b/src/corelib/io/qlockfile.cpp @@ -180,6 +180,26 @@ void QLockFile::setStaleLockTime(int staleLockTime) d->staleLockTime = staleLockTime; } +/*! \fn void QLockFile::setStaleLockTime(std::chrono::milliseconds value) + \overload + \since 6.2 + + Sets \a staleLockTime to be an interval after which a lock file is considered + stale. + The default value is 30 seconds. + If your application typically keeps the file locked for more than 30 seconds + (for instance while saving megabytes of data for 2 minutes), you should set + a bigger value using setStaleLockTime(). + + The value of \a staleLockTime is used by lock() and tryLock() in order + to determine when an existing lock file is considered stale, i.e. left over + by a crashed process. This is useful for the case where the PID got reused + meanwhile, so one way to detect a stale lock file is by the fact that + it has been around for a long time. + + \sa staleLockTime() +*/ + /*! Returns the time in milliseconds after which a lock file is considered stale. @@ -192,6 +212,16 @@ int QLockFile::staleLockTime() const return d->staleLockTime; } +/*! \fn std::chrono::milliseconds staleLockTimeAsDuration() const + \overload + \since 6.2 + + Returns a std::chrono::milliseconds object which denotes the time after + which a lock file is considered stale. + + \sa setStaleLockTime() +*/ + /*! Returns \c true if the lock was acquired by this QLockFile instance, otherwise returns \c false. @@ -287,6 +317,25 @@ bool QLockFile::tryLock(int timeout) return false; } +/*! \fn bool QLockFile::tryLock(std::chrono::milliseconds timeout) + \overload + \since 6.2 + + Attempts to create the lock file. This function returns \c true if the + lock was obtained; otherwise it returns \c false. If another process (or + another thread) has created the lock file already, this function will + wait for at most \a timeout for the lock file to become available. + + If the lock was obtained, it must be released with unlock() + before another process (or thread) can successfully lock it. + + Calling this function multiple times on the same lock from the same + thread without unlocking first is not allowed, this function will + \e always return false when attempting to lock the file recursively. + + \sa lock(), unlock() +*/ + /*! \fn void QLockFile::unlock() Releases the lock, by deleting the lock file. diff --git a/src/corelib/io/qlockfile.h b/src/corelib/io/qlockfile.h index ba955241b6..aa5ad5e772 100644 --- a/src/corelib/io/qlockfile.h +++ b/src/corelib/io/qlockfile.h @@ -43,6 +43,10 @@ #include <QtCore/qstring.h> #include <QtCore/qscopedpointer.h> +#if __has_include(<chrono>) +# include <chrono> +#endif + QT_BEGIN_NAMESPACE class QLockFilePrivate; @@ -62,6 +66,17 @@ public: void setStaleLockTime(int); int staleLockTime() const; +#if __has_include(<chrono>) + bool tryLock(std::chrono::milliseconds timeout) { return tryLock(int(timeout.count())); } + + void setStaleLockTime(std::chrono::milliseconds value) { setStaleLockTime(int(value.count())); } + + std::chrono::milliseconds staleLockTimeAsDuration() const + { + return std::chrono::milliseconds(staleLockTime()); + } +#endif + bool isLocked() const; bool getLockInfo(qint64 *pid, QString *hostname, QString *appname) const; bool removeStaleLockFile(); |