qtlockedfile.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU
  18. ** General Public License version 3 as published by the Free Software
  19. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  20. ** included in the packaging of this file. Please review the following
  21. ** information to ensure the GNU General Public License requirements will
  22. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  23. **
  24. ****************************************************************************/
  25. #include "qtlockedfile.h"
  26. namespace SharedTools {
  27. /*!
  28. \class QtLockedFile
  29. \brief The QtLockedFile class extends QFile with advisory locking functions.
  30. A file may be locked in read or write mode. Multiple instances of
  31. \e QtLockedFile, created in multiple processes running on the same
  32. machine, may have a file locked in read mode. Exactly one instance
  33. may have it locked in write mode. A read and a write lock cannot
  34. exist simultaneously on the same file.
  35. The file locks are advisory. This means that nothing prevents
  36. another process from manipulating a locked file using QFile or
  37. file system functions offered by the OS. Serialization is only
  38. guaranteed if all processes that access the file use
  39. QtLockedFile. Also, while holding a lock on a file, a process
  40. must not open the same file again (through any API), or locks
  41. can be unexpectedly lost.
  42. The lock provided by an instance of \e QtLockedFile is released
  43. whenever the program terminates. This is true even when the
  44. program crashes and no destructors are called.
  45. */
  46. /*! \enum QtLockedFile::LockMode
  47. This enum describes the available lock modes.
  48. \value ReadLock A read lock.
  49. \value WriteLock A write lock.
  50. \value NoLock Neither a read lock nor a write lock.
  51. */
  52. /*!
  53. Constructs an unlocked \e QtLockedFile object. This constructor behaves in the same way
  54. as \e QFile::QFile().
  55. \sa QFile::QFile()
  56. */
  57. QtLockedFile::QtLockedFile()
  58. : QFile()
  59. {
  60. #ifdef Q_OS_WIN
  61. m_semaphore_hnd = 0;
  62. m_mutex_hnd = 0;
  63. #endif
  64. m_lock_mode = NoLock;
  65. }
  66. /*!
  67. Constructs an unlocked QtLockedFile object with file \a name. This constructor behaves in
  68. the same way as \e QFile::QFile(const QString&).
  69. \sa QFile::QFile()
  70. */
  71. QtLockedFile::QtLockedFile(const QString &name)
  72. : QFile(name)
  73. {
  74. #ifdef Q_OS_WIN
  75. m_semaphore_hnd = 0;
  76. m_mutex_hnd = 0;
  77. #endif
  78. m_lock_mode = NoLock;
  79. }
  80. /*!
  81. Returns \e true if this object has a in read or write lock;
  82. otherwise returns \e false.
  83. \sa lockMode()
  84. */
  85. bool QtLockedFile::isLocked() const
  86. {
  87. return m_lock_mode != NoLock;
  88. }
  89. /*!
  90. Returns the type of lock currently held by this object, or \e QtLockedFile::NoLock.
  91. \sa isLocked()
  92. */
  93. QtLockedFile::LockMode QtLockedFile::lockMode() const
  94. {
  95. return m_lock_mode;
  96. }
  97. /*!
  98. \fn bool QtLockedFile::lock(LockMode mode, bool block = true)
  99. Obtains a lock of type \a mode.
  100. If \a block is true, this
  101. function will block until the lock is acquired. If \a block is
  102. false, this function returns \e false immediately if the lock cannot
  103. be acquired.
  104. If this object already has a lock of type \a mode, this function returns \e true immediately. If this object has a lock of a different type than \a mode, the lock
  105. is first released and then a new lock is obtained.
  106. This function returns \e true if, after it executes, the file is locked by this object,
  107. and \e false otherwise.
  108. \sa unlock(), isLocked(), lockMode()
  109. */
  110. /*!
  111. \fn bool QtLockedFile::unlock()
  112. Releases a lock.
  113. If the object has no lock, this function returns immediately.
  114. This function returns \e true if, after it executes, the file is not locked by
  115. this object, and \e false otherwise.
  116. \sa lock(), isLocked(), lockMode()
  117. */
  118. /*!
  119. \fn QtLockedFile::~QtLockedFile()
  120. Destroys the \e QtLockedFile object. If any locks were held, they are released.
  121. */
  122. } // namespace SharedTools