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:
parent
779bdf481c
commit
a0e3da2625
@ -60,7 +60,7 @@ static QString machineName()
|
|||||||
When protecting for a short-term operation, it is acceptable to call lock() and wait
|
When protecting for a short-term operation, it is acceptable to call lock() and wait
|
||||||
until any running operation finishes.
|
until any running operation finishes.
|
||||||
When protecting a resource over a long time, however, the application should always
|
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.
|
warn the user that the resource is locked.
|
||||||
|
|
||||||
If the process holding the lock crashes, the lock file stays on disk and can prevent
|
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
|
meanwhile, so one way to detect a stale lock file is by the fact that
|
||||||
it has been around for a long time.
|
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()
|
\sa staleLockTime()
|
||||||
*/
|
*/
|
||||||
void QLockFile::setStaleLockTime(int staleLockTime)
|
void QLockFile::setStaleLockTime(int staleLockTime)
|
||||||
{
|
{
|
||||||
Q_D(QLockFile);
|
setStaleLockTime(std::chrono::milliseconds{staleLockTime});
|
||||||
d->staleLockTime = staleLockTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \fn void QLockFile::setStaleLockTime(std::chrono::milliseconds value)
|
/*!
|
||||||
\overload
|
|
||||||
\since 6.2
|
\since 6.2
|
||||||
|
|
||||||
Sets the interval after which a lock file is considered stale to \a value.
|
Sets the interval after which a lock file is considered stale to \a staleLockTime.
|
||||||
The default value is 30 seconds.
|
The default value is 30s.
|
||||||
|
|
||||||
If your application typically keeps the file locked for more than 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
|
(for instance while saving megabytes of data for 2 minutes), you should set
|
||||||
a bigger value using setStaleLockTime().
|
a bigger value using setStaleLockTime().
|
||||||
@ -164,6 +168,11 @@ void QLockFile::setStaleLockTime(int staleLockTime)
|
|||||||
|
|
||||||
\sa staleLockTime()
|
\sa staleLockTime()
|
||||||
*/
|
*/
|
||||||
|
void QLockFile::setStaleLockTime(std::chrono::milliseconds staleLockTime)
|
||||||
|
{
|
||||||
|
Q_D(QLockFile);
|
||||||
|
d->staleLockTime = staleLockTime;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Returns the time in milliseconds after which
|
Returns the time in milliseconds after which
|
||||||
@ -173,8 +182,7 @@ void QLockFile::setStaleLockTime(int staleLockTime)
|
|||||||
*/
|
*/
|
||||||
int QLockFile::staleLockTime() const
|
int QLockFile::staleLockTime() const
|
||||||
{
|
{
|
||||||
Q_D(const QLockFile);
|
return int(staleLockTimeAsDuration().count());
|
||||||
return d->staleLockTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \fn std::chrono::milliseconds QLockFile::staleLockTimeAsDuration() const
|
/*! \fn std::chrono::milliseconds QLockFile::staleLockTimeAsDuration() const
|
||||||
@ -186,6 +194,11 @@ int QLockFile::staleLockTime() const
|
|||||||
|
|
||||||
\sa setStaleLockTime()
|
\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,
|
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());
|
const QDateTime lastMod = QFileInfo(fileName).lastModified(QTimeZone::UTC);
|
||||||
return staleLockTime > 0 && qAbs(age) > staleLockTime;
|
using namespace std::chrono;
|
||||||
|
const milliseconds age{lastMod.msecsTo(QDateTime::currentDateTimeUtc())};
|
||||||
|
return staleLockTime > 0ms && abs(age) > staleLockTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -37,12 +37,8 @@ public:
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void setStaleLockTime(std::chrono::milliseconds value) { setStaleLockTime(int(value.count())); }
|
void setStaleLockTime(std::chrono::milliseconds value);
|
||||||
|
std::chrono::milliseconds staleLockTimeAsDuration() const;
|
||||||
std::chrono::milliseconds staleLockTimeAsDuration() const
|
|
||||||
{
|
|
||||||
return std::chrono::milliseconds(staleLockTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isLocked() const;
|
bool isLocked() const;
|
||||||
bool getLockInfo(qint64 *pid, QString *hostname, QString *appname) const;
|
bool getLockInfo(qint64 *pid, QString *hostname, QString *appname) const;
|
||||||
|
@ -53,8 +53,8 @@ public:
|
|||||||
#else
|
#else
|
||||||
int fileHandle = -1;
|
int fileHandle = -1;
|
||||||
#endif
|
#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;
|
QLockFile::LockError lockError = QLockFile::NoError;
|
||||||
bool isLocked = false;
|
bool isLocked = false;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user