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>
This commit is contained in:
Shantanu Tushar 2020-10-24 19:16:16 +02:00
parent b3281eb6f4
commit 0592123a0c
2 changed files with 64 additions and 0 deletions

View File

@ -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.

View File

@ -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();