2015-09-14 13:53:49 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file VLockGuard.h
|
|
|
|
** @author Alex Zaharov <alexzkhr@gmail.com>
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 14 9, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2015-09-14 13:53:49 +02:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2015 Valentina project
|
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#ifndef VLOCKGUARD_H
|
|
|
|
#define VLOCKGUARD_H
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-08-06 20:42:40 +02:00
|
|
|
#include "../vmisc/diagnostic.h"
|
|
|
|
|
2015-10-26 08:57:20 +01:00
|
|
|
#include <QFileInfo>
|
2015-09-14 13:53:49 +02:00
|
|
|
#include <QLockFile>
|
2019-07-04 16:13:33 +02:00
|
|
|
#include <QSharedPointer>
|
2015-10-26 08:57:20 +01:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2015-09-14 13:53:49 +02:00
|
|
|
|
|
|
|
/*@brief
|
|
|
|
* This class creates Guarded object if and only if lock file taken. It keeps shared_ptr to object and lock-file.
|
|
|
|
* Can use optional object allocator and deleter.
|
|
|
|
*
|
|
|
|
* On older Qt lock assumed always taken and compile-time warning is shown.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
template <typename Guarded>
|
|
|
|
class VLockGuard
|
|
|
|
{
|
|
|
|
public:
|
2015-10-20 16:32:01 +02:00
|
|
|
explicit VLockGuard(const QString& lockName, int stale = 0, int timeout = 0);
|
2015-09-14 13:53:49 +02:00
|
|
|
|
|
|
|
template <typename Alloc>
|
|
|
|
VLockGuard(const QString& lockName, Alloc a, int stale = 0, int timeout=0);
|
|
|
|
|
|
|
|
template <typename Alloc, typename Delete>
|
|
|
|
VLockGuard(const QString& lockName, Alloc a, Delete d, int stale = 0, int timeout=0);
|
|
|
|
|
2019-07-04 16:13:33 +02:00
|
|
|
const QSharedPointer<Guarded> &GetProtected() const;
|
2015-09-14 13:53:49 +02:00
|
|
|
int GetLockError() const;
|
|
|
|
bool IsLocked() const;
|
2019-07-04 16:13:33 +02:00
|
|
|
void Unlock();
|
2015-10-26 08:57:20 +01:00
|
|
|
QString GetLockFile() const;
|
2015-09-14 13:53:49 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(VLockGuard<Guarded>)
|
|
|
|
|
2019-07-04 16:13:33 +02:00
|
|
|
QSharedPointer<Guarded> holder;
|
|
|
|
int lockError;
|
|
|
|
QString lockFile;
|
|
|
|
QSharedPointer<QLockFile> lock;
|
2015-09-14 13:53:49 +02:00
|
|
|
|
2015-10-12 13:52:48 +02:00
|
|
|
// cppcheck-suppress functionStatic
|
2015-09-14 13:53:49 +02:00
|
|
|
bool TryLock(const QString &lockName, int stale, int timeout);
|
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Guarded>
|
|
|
|
VLockGuard<Guarded>::VLockGuard(const QString &lockName, int stale, int timeout)
|
2017-05-30 18:52:38 +02:00
|
|
|
: holder(nullptr), lockError(0), lockFile(), lock(nullptr)
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
if (TryLock(lockName, stale, timeout))
|
|
|
|
{
|
|
|
|
holder.reset(new Guarded());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
//using allocator lambdas seems logically better than supplying pointer, because we will take ownership of allocated
|
|
|
|
//object
|
|
|
|
template <typename Guarded> template <typename Alloc>
|
|
|
|
VLockGuard<Guarded>::VLockGuard(const QString& lockName, Alloc a, int stale, int timeout)
|
2017-05-30 18:52:38 +02:00
|
|
|
: holder(nullptr), lockError(0), lockFile(), lock(nullptr)
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
if (TryLock(lockName, stale, timeout))
|
|
|
|
{
|
|
|
|
holder.reset(a());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Guarded> template <typename Alloc, typename Delete>
|
|
|
|
VLockGuard<Guarded>::VLockGuard(const QString& lockName, Alloc a, Delete d, int stale, int timeout)
|
2017-05-30 18:52:38 +02:00
|
|
|
: holder(nullptr), lockError(0), lockFile(), lock(nullptr)
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
if (TryLock(lockName, stale, timeout))
|
|
|
|
{
|
|
|
|
holder.reset(a(), d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Guarded>
|
2019-07-04 16:13:33 +02:00
|
|
|
inline const QSharedPointer<Guarded> &VLockGuard<Guarded>::GetProtected() const
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Guarded>
|
2019-07-04 16:13:33 +02:00
|
|
|
inline int VLockGuard<Guarded>::GetLockError() const
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
return lockError;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Guarded>
|
2019-07-04 16:13:33 +02:00
|
|
|
inline bool VLockGuard<Guarded>::IsLocked() const
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
2019-07-04 16:13:33 +02:00
|
|
|
return not holder.isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename Guarded>
|
|
|
|
inline void VLockGuard<Guarded>::Unlock()
|
|
|
|
{
|
|
|
|
if (IsLocked())
|
|
|
|
{
|
|
|
|
lock->unlock();
|
|
|
|
}
|
2015-09-14 13:53:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-26 08:57:20 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Guarded>
|
|
|
|
QString VLockGuard<Guarded>::GetLockFile() const
|
|
|
|
{
|
|
|
|
return lockFile;
|
|
|
|
}
|
|
|
|
|
2015-09-14 13:53:49 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Guarded>
|
|
|
|
bool VLockGuard<Guarded>::TryLock(const QString &lockName, int stale, int timeout)
|
|
|
|
{
|
|
|
|
bool res = true;
|
|
|
|
|
2016-07-20 10:39:15 +02:00
|
|
|
lockFile = lockName + QLatin1String(".lock");
|
2015-10-26 08:57:20 +01:00
|
|
|
#if defined(Q_OS_UNIX)
|
|
|
|
QFileInfo info(lockFile);
|
2016-07-20 10:39:15 +02:00
|
|
|
lockFile = info.absolutePath() + QLatin1String("/.") + info.fileName();
|
2015-10-26 08:57:20 +01:00
|
|
|
#endif
|
|
|
|
lock.reset(new QLockFile(lockFile));
|
|
|
|
|
2015-09-14 13:53:49 +02:00
|
|
|
lock->setStaleLockTime(stale);
|
2015-10-26 08:57:20 +01:00
|
|
|
lock->tryLock(timeout);
|
2015-09-14 13:53:49 +02:00
|
|
|
|
2015-10-09 11:50:37 +02:00
|
|
|
if (QLockFile::LockFailedError == lock->error())
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
2015-10-09 11:50:37 +02:00
|
|
|
// This happens if a stale lock file exists and another process uses that PID.
|
|
|
|
// Try removing the stale file, which will fail if a real process is holding a
|
|
|
|
// file-level lock. A false error is more problematic than not locking properly
|
|
|
|
// on corner-case systems.
|
|
|
|
lock->removeStaleLockFile();
|
|
|
|
lock->tryLock(timeout);
|
2015-09-14 13:53:49 +02:00
|
|
|
}
|
|
|
|
res = QLockFile::NoError == (lockError = lock->error());
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
lock.reset();
|
|
|
|
}
|
2017-08-21 15:47:17 +02:00
|
|
|
#if defined(Q_OS_WIN)
|
2015-10-26 08:57:20 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SetFileAttributesW(lockFile.toStdWString().c_str(), FILE_ATTRIBUTE_HIDDEN);
|
|
|
|
}
|
2017-08-21 15:47:17 +02:00
|
|
|
#endif
|
2015-09-14 13:53:49 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//use pointer and function below to persistent things like class-member, because lock is taken by constructor
|
|
|
|
//helper functions allow to write shorter creating and setting new lock-pointer
|
|
|
|
|
2016-08-06 20:42:40 +02:00
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_INTEL(1418)
|
2015-10-20 16:32:01 +02:00
|
|
|
|
2015-09-14 13:53:49 +02:00
|
|
|
template <typename Guarded>
|
2019-07-04 16:13:33 +02:00
|
|
|
void VlpCreateLock(QSharedPointer<VLockGuard<Guarded>>& r, const QString& lockName, int stale = 0, int timeout = 0)
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
r.reset(new VLockGuard<Guarded>(lockName, stale, timeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Guarded, typename Alloc>
|
2019-07-04 16:13:33 +02:00
|
|
|
void VlpCreateLock(QSharedPointer<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, int stale = 0,
|
2015-10-26 08:57:20 +01:00
|
|
|
int timeout = 0)
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
r.reset(new VLockGuard<Guarded>(lockName, a, stale, timeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Guarded, typename Alloc, typename Del>
|
2019-07-04 16:13:33 +02:00
|
|
|
void VlpCreateLock(QSharedPointer<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, Del d, int stale = 0,
|
2015-10-26 08:57:20 +01:00
|
|
|
int timeout = 0)
|
2015-09-14 13:53:49 +02:00
|
|
|
{
|
|
|
|
r.reset(new VLockGuard<Guarded>(lockName, a, d, stale, timeout));
|
|
|
|
}
|
|
|
|
|
2016-08-06 20:42:40 +02:00
|
|
|
QT_WARNING_POP
|
2015-10-20 16:32:01 +02:00
|
|
|
|
2015-09-14 13:53:49 +02:00
|
|
|
#endif // VLOCKGUARD_H
|