Skip to content

Commit

Permalink
Cleanup|libcore: Use C++11 standard mutex class
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 12, 2017
1 parent 5255f8f commit 71efce1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 111 deletions.
52 changes: 14 additions & 38 deletions doomsday/sdk/libcore/include/de/concurrency/guard.h
Expand Up @@ -77,73 +77,49 @@ class ReadWriteLockable;
class DENG2_PUBLIC Guard
{
public:
enum LockMode
{
Reading,
Writing
};
enum LockMode { Reading, Writing };

public:
/**
* The target object is locked.
*/
inline Guard(Lockable const &target)
: _target(&target), _rwTarget(0)
{
inline Guard(Lockable const &target) : _target(&target), _rwTarget(0) {
_target->lock();
}

/**
* The target object is locked.
*/
inline Guard(Lockable const *target)
: _target(target), _rwTarget(0)
{
inline Guard(Lockable const *target) : _target(target), _rwTarget(0) {
DENG2_ASSERT(target != 0);
_target->lock();
}

inline Guard(ReadWriteLockable const &target, LockMode mode)
: _target(0), _rwTarget(&target)
{
if (mode == Reading)
{
inline Guard(ReadWriteLockable const &target, LockMode mode) : _target(0), _rwTarget(&target) {
if (mode == Reading) {
_rwTarget->lockForRead();
}
else
{
else {
_rwTarget->lockForWrite();
}
}

inline Guard(ReadWriteLockable const *target, LockMode mode)
: _target(0), _rwTarget(target)
{

inline Guard(ReadWriteLockable const *target, LockMode mode) : _target(0), _rwTarget(target) {
DENG2_ASSERT(_rwTarget != 0);

if (mode == Reading)
{
if (mode == Reading) {
_rwTarget->lockForRead();
}
else
{
else {
_rwTarget->lockForWrite();
}
}

/**
* The target object is unlocked.
*/
inline ~Guard()
{
if (_target)
{
_target->unlock();
}
if (_rwTarget)
{
_rwTarget->unlock();
}
inline ~Guard() {
if (_target) _target->unlock();
if (_rwTarget) _rwTarget->unlock();
}

private:
Expand Down
35 changes: 12 additions & 23 deletions doomsday/sdk/libcore/include/de/concurrency/lockable.h
Expand Up @@ -23,11 +23,9 @@

#include "../libcore.h"

#include <QMutex>
#include <mutex>

namespace de {

//#define DENG2_LOCKABLE_LOCK_COUNT

/**
* A mutex that can be used to synchronize access to a resource. All classes
Expand All @@ -40,43 +38,34 @@ namespace de {
class DENG2_PUBLIC Lockable
{
public:
Lockable();
virtual ~Lockable();

/// Acquire the lock. Blocks until the operation succeeds.
void lock() const;
inline void lock() const {
_mutex.lock();
}

/// Release the lock.
void unlock() const;

#ifdef DENG2_LOCKABLE_LOCK_COUNT
/// Returns true, if the lock is currently locked.
bool isLocked() const;
#endif

public:
mutable QMutex _mutex;
inline void unlock() const {
_mutex.unlock();
}

#ifdef DENG2_LOCKABLE_LOCK_COUNT
mutable int _lockCount;
mutable QMutex _countMutex;
#endif
private:
mutable std::recursive_mutex _mutex;
};

template <typename Type>
struct LockableT : public Lockable
{
typedef Type ValueType;
Type value;

LockableT() {}
LockableT(Type const &initial) : value(initial) {}
LockableT(Type &&initial) : value(initial) {}

operator Type &() { return value; }
operator Type const &() const { return value; }
};

} // namespace de

#endif // LIBDENG2_LOCKABLE_H
51 changes: 1 addition & 50 deletions doomsday/sdk/libcore/src/concurrency/lockable.cpp
Expand Up @@ -23,55 +23,6 @@

namespace de {

Lockable::Lockable()
: _mutex(QMutex::Recursive)
#ifdef DENG2_LOCKABLE_LOCK_COUNT
, _lockCount(0)
#endif
{}

Lockable::~Lockable()
{
#ifdef DENG2_LOCKABLE_LOCK_COUNT
DENG2_ASSERT(!isLocked()); // You should unlock before deleting!
#endif
}

void Lockable::lock() const
{
_mutex.lock();

#ifdef DENG2_LOCKABLE_LOCK_COUNT
_countMutex.lock();
_lockCount++;
_countMutex.unlock();
#endif
}

void Lockable::unlock() const
{
#ifdef DENG2_LOCKABLE_LOCK_COUNT
_countMutex.lock();
_lockCount--;
#endif

// Release the lock.
_mutex.unlock();

#ifdef DENG2_LOCKABLE_LOCK_COUNT
DENG2_ASSERT(_lockCount >= 0);
_countMutex.unlock();
#endif
}

#ifdef DENG2_LOCKABLE_LOCK_COUNT
bool Lockable::isLocked() const
{
_countMutex.lock();
bool result = (_lockCount > 0);
_countMutex.unlock();
return result;
}
#endif
// nothing here

} // namespace de

0 comments on commit 71efce1

Please sign in to comment.