File indexing completed on 2024-05-12 15:57:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KISUPGRADETOWRITELOCKER_H
0008 #define KISUPGRADETOWRITELOCKER_H
0009 
0010 #include <QReadLocker>
0011 #include <QReadWriteLock>
0012 
0013 /**
0014  * @brief The KisUpgradeToWriteLocker class is use for RAII style unlocking
0015  * the read lock and then locking the lock for write. We basically "upgrade"
0016  * the lock to a write one.
0017  *
0018  * WARNING: during the *upgrade* the lock passes the "unlocked" state, so
0019  *          all the protected data you acquired during the "read" phase might
0020  *          have become invalidated!
0021  */
0022 class KisUpgradeToWriteLocker
0023 {
0024 public:
0025     KisUpgradeToWriteLocker(QReadLocker *locker)
0026         : m_locker(locker)
0027     {
0028         m_locker->unlock();
0029         m_locker->readWriteLock()->lockForWrite();
0030     }
0031 
0032     ~KisUpgradeToWriteLocker() {
0033         m_locker->readWriteLock()->unlock();
0034         m_locker->relock();
0035     }
0036 
0037 private:
0038     QReadLocker *m_locker;
0039 };
0040 
0041 
0042 
0043 #endif // KISUPGRADETOWRITELOCKER_H