QLockFile: {set}StaleLockTime: use chrono first

This is similar to the tryLock() commit, making the chrono overloads the
main methods, this way the lock time isn't limited by the size of int,
but rather by std::chrono::milliseconds (which can be as up to int64_t).

Task-number: QTBUG-110059
Change-Id: I8d1652748b16be2154233f7db57ed485bcab62c7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-02-28 01:09:13 +02:00
parent 779bdf481c
commit a0e3da2625
3 changed files with 30 additions and 19 deletions

View File

@ -60,7 +60,7 @@ static QString machineName()
When protecting for a short-term operation, it is acceptable to call lock() and wait
until any running operation finishes.
When protecting a resource over a long time, however, the application should always
call setStaleLockTime(0) and then tryLock() with a short timeout, in order to
call setStaleLockTime(0ms) and then tryLock() with a short timeout, in order to
warn the user that the resource is locked.
If the process holding the lock crashes, the lock file stays on disk and can prevent
@ -138,20 +138,24 @@ QString QLockFile::fileName() const
meanwhile, so one way to detect a stale lock file is by the fact that
it has been around for a long time.
This is an overloaded function, equivalent to calling:
\code
setStaleLockTime(std::chrono::milliseconds{staleLockTime});
\endcode
\sa staleLockTime()
*/
void QLockFile::setStaleLockTime(int staleLockTime)
{
Q_D(QLockFile);
d->staleLockTime = staleLockTime;
setStaleLockTime(std::chrono::milliseconds{staleLockTime});
}
/*! \fn void QLockFile::setStaleLockTime(std::chrono::milliseconds value)
\overload
/*!
\since 6.2
Sets the interval after which a lock file is considered stale to \a value.
The default value is 30 seconds.
Sets the interval after which a lock file is considered stale to \a staleLockTime.
The default value is 30s.
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().
@ -164,6 +168,11 @@ void QLockFile::setStaleLockTime(int staleLockTime)
\sa staleLockTime()
*/
void QLockFile::setStaleLockTime(std::chrono::milliseconds staleLockTime)
{
Q_D(QLockFile);
d->staleLockTime = staleLockTime;
}
/*!
Returns the time in milliseconds after which
@ -173,8 +182,7 @@ void QLockFile::setStaleLockTime(int staleLockTime)
*/
int QLockFile::staleLockTime() const
{
Q_D(const QLockFile);
return d->staleLockTime;
return int(staleLockTimeAsDuration().count());
}
/*! \fn std::chrono::milliseconds QLockFile::staleLockTimeAsDuration() const
@ -186,6 +194,11 @@ int QLockFile::staleLockTime() const
\sa setStaleLockTime()
*/
std::chrono::milliseconds QLockFile::staleLockTimeAsDuration() const
{
Q_D(const QLockFile);
return d->staleLockTime;
}
/*!
Returns \c true if the lock was acquired by this QLockFile instance,
@ -427,8 +440,10 @@ bool QLockFilePrivate::isApparentlyStale() const
}
}
const qint64 age = QFileInfo(fileName).lastModified(QTimeZone::UTC).msecsTo(QDateTime::currentDateTimeUtc());
return staleLockTime > 0 && qAbs(age) > staleLockTime;
const QDateTime lastMod = QFileInfo(fileName).lastModified(QTimeZone::UTC);
using namespace std::chrono;
const milliseconds age{lastMod.msecsTo(QDateTime::currentDateTimeUtc())};
return staleLockTime > 0ms && abs(age) > staleLockTime;
}
/*!

View File

@ -37,12 +37,8 @@ public:
}
#endif
void setStaleLockTime(std::chrono::milliseconds value) { setStaleLockTime(int(value.count())); }
std::chrono::milliseconds staleLockTimeAsDuration() const
{
return std::chrono::milliseconds(staleLockTime());
}
void setStaleLockTime(std::chrono::milliseconds value);
std::chrono::milliseconds staleLockTimeAsDuration() const;
bool isLocked() const;
bool getLockInfo(qint64 *pid, QString *hostname, QString *appname) const;

View File

@ -53,8 +53,8 @@ public:
#else
int fileHandle = -1;
#endif
// "int milliseconds" is big enough for 24 days
int staleLockTime = 30 * 1000; // 30 seconds
std::chrono::milliseconds staleLockTime = std::chrono::seconds{30};
QLockFile::LockError lockError = QLockFile::NoError;
bool isLocked = false;