diff --git a/libraries/shared/src/TryLocker.h b/libraries/shared/src/TryLocker.h new file mode 100644 index 0000000000..e434ae4372 --- /dev/null +++ b/libraries/shared/src/TryLocker.h @@ -0,0 +1,31 @@ +// +// TryLocker.h +// libraries/shared/src +// +// Created by Brad Davis on 2015/03/16. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_TryLocker_h +#define hifi_TryLocker_h + +#include + +class MutexTryLocker { + QMutex & _mutex; + bool _locked{false}; +public: + MutexTryLocker(QMutex &m) : _mutex(m) {} + ~MutexTryLocker() { if (_locked) _mutex.unlock(); } + bool tryLock() { + if (_locked) { + return true; + } + return (_locked = _mutex.tryLock()); + } +} + +#endif // hifi_UUID_h