Skip to content

Commit

Permalink
Try to make the class LockUtility RAII complaint
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementKunz committed Oct 23, 2024
1 parent 9fde38a commit fd3919a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/gui/clientgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ void ClientGui::setupSynthesisPopover() {
_workaroundManualVisibility = true;
#endif

qCInfo(lcClientGui) << "Tray menu workarounds:"
<< "noabouttoshow:" << _workaroundNoAboutToShowUpdate << "fakedoubleclick:" << _workaroundFakeDoubleClick
<< "showhide:" << _workaroundShowAndHideTray << "manualvisibility:" << _workaroundManualVisibility;
qCInfo(lcClientGui) << "Tray menu workarounds:" << "noabouttoshow:" << _workaroundNoAboutToShowUpdate
<< "fakedoubleclick:" << _workaroundFakeDoubleClick << "showhide:" << _workaroundShowAndHideTray
<< "manualvisibility:" << _workaroundManualVisibility;

connect(&_delayedTrayUpdateTimer, &QTimer::timeout, this, &ClientGui::onUpdateSystray);
_delayedTrayUpdateTimer.setInterval(2 * 1000);
Expand Down Expand Up @@ -761,8 +761,8 @@ void ClientGui::onNewDriveWizard() {

void ClientGui::onShowWindowsUpdateDialog(const VersionInfo &versionInfo) const {
static std::mutex mutex;
if (const std::unique_lock lock(mutex, std::try_to_lock); !lock.owns_lock()) return;

LockUtility lock(mutex);
if (!lock.ownLock()) return;
if (UpdateDialog dialog(versionInfo); dialog.exec() == QDialog::Accepted) {
GuiRequests::startInstaller();
} else if (dialog.skip()) {
Expand Down
21 changes: 6 additions & 15 deletions src/libcommon/utility/lockutility.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,15 @@ namespace KDC {

class LockUtility {
public:
/**
* Acquire the lock.
* @return Return false if already locked.
*/
[[nodiscard]] bool lock() {
const std::scoped_lock lock(_mutex);
if (_isLocked) return false;
_isLocked = true;
return _isLocked;
explicit LockUtility(std::mutex &mutex) : _mutex(mutex) { _ownLock = mutex.try_lock(); }
~LockUtility() {
if (_ownLock) _mutex.unlock();
}
/**
* Release the lock.
*/
void unlock() { _isLocked = false; }
[[nodiscard]] bool ownLock() const { return _ownLock; }

private:
std::mutex _mutex;
bool _isLocked{false};
std::mutex &_mutex;
bool _ownLock{false};
};

} // namespace KDC
21 changes: 13 additions & 8 deletions test/libcommon/utility/testlockutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
namespace KDC {

void TestLockUtility::testLock() {
LockUtility lock;
CPPUNIT_ASSERT(lock.lock());
CPPUNIT_ASSERT(!lock.lock());
lock.unlock();
CPPUNIT_ASSERT(lock.lock());
static std::mutex mutex;
{
LockUtility lock(mutex);
CPPUNIT_ASSERT(lock.ownLock());
CPPUNIT_ASSERT(!mutex.try_lock()); // mutex is locked
LockUtility lock2(mutex);
CPPUNIT_ASSERT(!lock2.ownLock());
CPPUNIT_ASSERT(lock.ownLock());
}
CPPUNIT_ASSERT(mutex.try_lock()); // lock has been released
}

int randomSleepTime() {
Expand All @@ -40,19 +45,19 @@ constexpr unsigned int nbThread = 3;
constexpr unsigned int nbTest = 5;
static unsigned int alreadyLockedCounter = 0;
static unsigned int lockAcquiredCounter = 0;
static LockUtility lock;
static std::mutex mutex;
void testCallback() {
int i = 0;
while (i < nbTest) {
LockUtility lock(mutex);
Utility::msleep(randomSleepTime());
if (!lock.lock()) {
if (!lock.ownLock()) {
alreadyLockedCounter++;
Utility::msleep(randomSleepTime());
continue;
}
lockAcquiredCounter++;
Utility::msleep(10);
lock.unlock();
i++;
}
}
Expand Down

0 comments on commit fd3919a

Please sign in to comment.