File indexing completed on 2024-12-22 04:12:49
0001 /**************************************************************************** 0002 ** 0003 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). 0004 ** Contact: http://www.qt-project.org/legal 0005 ** 0006 ** This file is part of Qt Creator. 0007 ** 0008 ** Commercial License Usage 0009 ** Licensees holding valid commercial Qt licenses may use this file in 0010 ** accordance with the commercial license agreement provided with the 0011 ** Software or, alternatively, in accordance with the terms contained in 0012 ** a written agreement between you and Digia. For licensing terms and 0013 ** conditions see http://www.qt.io/licensing. For further information 0014 ** use the contact form at http://www.qt.io/contact-us. 0015 ** 0016 ** GNU Lesser General Public License Usage 0017 ** Alternatively, this file may be used under the terms of the GNU Lesser 0018 ** General Public License version 2.1 or version 3 as published by the Free 0019 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and 0020 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the 0021 ** following information to ensure the GNU Lesser General Public License 0022 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and 0023 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 0024 ** 0025 ** In addition, as a special exception, Digia gives you certain additional 0026 ** rights. These rights are described in the Digia Qt LGPL Exception 0027 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 0028 ** 0029 ****************************************************************************/ 0030 0031 #include "qtlockedfile.h" 0032 0033 0034 0035 /*! 0036 \class QtLockedFile 0037 0038 \brief The QtLockedFile class extends QFile with advisory locking functions. 0039 0040 A file may be locked in read or write mode. Multiple instances of 0041 \e QtLockedFile, created in multiple processes running on the same 0042 machine, may have a file locked in read mode. Exactly one instance 0043 may have it locked in write mode. A read and a write lock cannot 0044 exist simultaneously on the same file. 0045 0046 The file locks are advisory. This means that nothing prevents 0047 another process from manipulating a locked file using QFile or 0048 file system functions offered by the OS. Serialization is only 0049 guaranteed if all processes that access the file use 0050 QtLockedFile. Also, while holding a lock on a file, a process 0051 must not open the same file again (through any API), or locks 0052 can be unexpectedly lost. 0053 0054 The lock provided by an instance of \e QtLockedFile is released 0055 whenever the program terminates. This is true even when the 0056 program crashes and no destructors are called. 0057 */ 0058 0059 /*! \enum QtLockedFile::LockMode 0060 0061 This enum describes the available lock modes. 0062 0063 \var ReadLock A read lock. 0064 \var WriteLock A write lock. 0065 \var NoLock Neither a read lock nor a write lock. 0066 */ 0067 0068 /*! 0069 Constructs an unlocked \e QtLockedFile object. This constructor behaves in the same way 0070 as \e QFile::QFile(). 0071 0072 \sa QFile::QFile() 0073 */ 0074 QtLockedFile::QtLockedFile() 0075 : QFile() 0076 { 0077 #ifdef Q_OS_WIN 0078 m_semaphore_hnd = 0; 0079 m_mutex_hnd = 0; 0080 #endif 0081 m_lock_mode = NoLock; 0082 } 0083 0084 /*! 0085 Constructs an unlocked QtLockedFile object with file \a name. This constructor behaves in 0086 the same way as \e QFile::QFile(const QString&). 0087 0088 \sa QFile::QFile() 0089 */ 0090 QtLockedFile::QtLockedFile(const QString &name) 0091 : QFile(name) 0092 { 0093 #ifdef Q_OS_WIN 0094 m_semaphore_hnd = 0; 0095 m_mutex_hnd = 0; 0096 #endif 0097 m_lock_mode = NoLock; 0098 } 0099 0100 /*! 0101 Returns \e true if this object has a in read or write lock; 0102 otherwise returns \e false. 0103 0104 \sa lockMode() 0105 */ 0106 bool QtLockedFile::isLocked() const 0107 { 0108 return m_lock_mode != NoLock; 0109 } 0110 0111 /*! 0112 Returns the type of lock currently held by this object, or \e QtLockedFile::NoLock. 0113 0114 \sa isLocked() 0115 */ 0116 QtLockedFile::LockMode QtLockedFile::lockMode() const 0117 { 0118 return m_lock_mode; 0119 } 0120 0121 /*! 0122 \fn bool QtLockedFile::lock(LockMode mode, bool block = true) 0123 0124 Obtains a lock of type \a mode. 0125 0126 If \a block is true, this 0127 function will block until the lock is acquired. If \a block is 0128 false, this function returns \e false immediately if the lock cannot 0129 be acquired. 0130 0131 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 0132 is first released and then a new lock is obtained. 0133 0134 This function returns \e true if, after it executes, the file is locked by this object, 0135 and \e false otherwise. 0136 0137 \sa unlock(), isLocked(), lockMode() 0138 */ 0139 0140 /*! 0141 \fn bool QtLockedFile::unlock() 0142 0143 Releases a lock. 0144 0145 If the object has no lock, this function returns immediately. 0146 0147 This function returns \e true if, after it executes, the file is not locked by 0148 this object, and \e false otherwise. 0149 0150 \sa lock(), isLocked(), lockMode() 0151 */ 0152 0153 /*! 0154 \fn QtLockedFile::~QtLockedFile() 0155 0156 Destroys the \e QtLockedFile object. If any locks were held, they are released. 0157 */