File indexing completed on 2026-09-19 09:32:13
0001
0002
0003
0004
0005 #ifndef QLOCKFILE_H
0006 #define QLOCKFILE_H
0007
0008 #include <QtCore/qstring.h>
0009 #include <QtCore/qscopedpointer.h>
0010
0011 #include <chrono>
0012 #include <memory>
0013
0014 QT_BEGIN_NAMESPACE
0015
0016 class QLockFilePrivate;
0017
0018 class Q_CORE_EXPORT QLockFile
0019 {
0020 public:
0021 explicit QLockFile(const QString &fileName);
0022 ~QLockFile();
0023
0024 QString fileName() const;
0025
0026 bool lock();
0027 QT_CORE_INLINE_SINCE(6, 10)
0028 bool tryLock(int timeout);
0029 void unlock();
0030
0031 QT_CORE_INLINE_SINCE(6, 10)
0032 void setStaleLockTime(int);
0033 QT_CORE_INLINE_SINCE(6, 10)
0034 int staleLockTime() const;
0035
0036 bool tryLock(std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
0037
0038 void setStaleLockTime(std::chrono::milliseconds value);
0039 std::chrono::milliseconds staleLockTimeAsDuration() const;
0040
0041 bool isLocked() const;
0042 bool getLockInfo(qint64 *pid, QString *hostname, QString *appname) const;
0043 bool removeStaleLockFile();
0044
0045 enum LockError {
0046 NoError = 0,
0047 LockFailedError = 1,
0048 PermissionError = 2,
0049 UnknownError = 3
0050 };
0051 LockError error() const;
0052
0053 private:
0054 std::unique_ptr<QLockFilePrivate> d_ptr;
0055
0056 Q_DECLARE_PRIVATE(QLockFile)
0057 Q_DISABLE_COPY(QLockFile)
0058 };
0059
0060 #if QT_CORE_INLINE_IMPL_SINCE(6, 10)
0061 bool QLockFile::tryLock(int timeout)
0062 {
0063 return tryLock(std::chrono::milliseconds{timeout});
0064 }
0065
0066 void QLockFile::setStaleLockTime(int staleLockTime)
0067 {
0068 setStaleLockTime(std::chrono::milliseconds{staleLockTime});
0069 }
0070
0071 int QLockFile::staleLockTime() const
0072 {
0073 return int(staleLockTimeAsDuration().count());
0074 }
0075 #endif
0076
0077 QT_END_NAMESPACE
0078
0079 #endif