Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_SYSTEM_MUTEX_H
#define BALL_SYSTEM_MUTEX_H
#ifndef BALL_COMMON_GLOBAL_H
# include <BALL/COMMON/global.h>
#endif
#include <QtCore/QMutex>
#include <QtCore/QReadWriteLock>
#include <QtCore/QMutexLocker>
#include <QtCore/QReadLocker>
#include <QtCore/QWriteLocker>
#include <boost/thread/mutex.hpp>
#undef BALL_USE_THREAD_CHECKER_API
#ifdef BALL_USE_THREAD_CHECKER_API
# include <libittnotify.h>
#endif
#define BALL_DEFAULT_MUTEX_TYPE QMutex
#define BALL_DEFAULT_MUTEXLOCKER_TYPE QMutexLocker
#define BALL_DEFAULT_READWRITELOCK_TYPE QReadWriteLock
#define BALL_DEFAULT_READLOCKER_TYPE QReadLocker
#define BALL_DEFAULT_WRITELOCKER_TYPE QWriteLocker
namespace BALL
{
template <class MutexType>
class TMutex
: public MutexType
{
public:
TMutex(bool is_recursive = false)
: MutexType()
{}
};
template <>
class TMutex<QMutex>
: public QMutex
{
public:
TMutex(bool is_recursive = false)
: QMutex( is_recursive ? Recursive : NonRecursive )
{
}
};
template <class MutexLockerType, class MutexType>
class TMutexLocker
: public MutexLockerType
{
public:
TMutexLocker(MutexType* mutex)
: MutexLockerType(mutex)
{}
};
template <class ReadWriteLockType>
class TReadWriteLock
: public ReadWriteLockType
{
public:
TReadWriteLock(bool is_recursive = false)
: ReadWriteLockType()
{}
};
template <>
class TReadWriteLock<QReadWriteLock>
: public QReadWriteLock
{
public:
TReadWriteLock(bool is_recursive = false)
: QReadWriteLock( is_recursive ? Recursive : NonRecursive )
{}
};
template <class ReadLockerType, class ReadWriteLockType>
class TReadLocker
: public ReadLockerType
{
public:
TReadLocker(ReadWriteLockType* lock)
: ReadLockerType(lock)
{}
};
template <class WriteLockerType, class ReadWriteLockType>
class TWriteLocker
: public WriteLockerType
{
public:
TWriteLocker(ReadWriteLockType* lock)
: WriteLockerType(lock)
{}
};
// Boost-based mutexes only require a mapping of tryLock to try_lock.
template <>
class TMutex<boost::mutex>
: public boost::mutex
{
public:
TMutex()
: boost::mutex()
{}
void lock()
{
return boost::mutex::lock();
}
bool tryLock()
{
return try_lock();
}
};
#ifdef BALL_USE_THREAD_CHECKER_API
template <>
class TMutex<QMutex>
: public QMutex
{
public:
TMutex()
: QMutex()
{
}
void lock()
{
__itt_notify_sync_prepare((void *)this);
QMutex::lock();
__itt_notify_sync_acquired((void*)this);
}
void unlock()
{
__itt_notify_sync_releasing((void *)this);
QMutex::unlock();
}
bool tryLock()
{
__itt_notify_sync_prepare((void*)this);
bool result = QMutex::tryLock();
if (result)
__itt_notify_sync_acquired((void*)this);
else
__itt_notify_sync_cancel((void*)this);
return result;
}
};
// TODO: similar instantiation for TReadWriteLock
#else
// required for visual studio
#ifdef BALL_COMPILER_MSVC
//template class BALL_EXPORT TMutex<BALL_DEFAULT_MUTEX_TYPE>;
//template class BALL_EXPORT TReadWriteLock<BALL_DEFAULT_READWRITELOCK_TYPE>;
template class BALL_EXPORT TMutexLocker<BALL_DEFAULT_MUTEXLOCKER_TYPE, BALL_DEFAULT_MUTEX_TYPE>;
template class BALL_EXPORT TReadLocker<BALL_DEFAULT_READLOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE>;
template class BALL_EXPORT TWriteLocker<BALL_DEFAULT_WRITELOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE>;
#endif
#endif
// the standard mutex to use
typedef TMutex<BALL_DEFAULT_MUTEX_TYPE> Mutex;
typedef TReadWriteLock<BALL_DEFAULT_READWRITELOCK_TYPE> ReadWriteLock;
typedef TMutexLocker<BALL_DEFAULT_MUTEXLOCKER_TYPE, BALL_DEFAULT_MUTEX_TYPE> MutexLocker;
typedef TReadLocker<BALL_DEFAULT_READLOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE> ReadLocker;
typedef TWriteLocker<BALL_DEFAULT_WRITELOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE> WriteLocker;
}
#endif // BALL_SYSTEM_MUTEX_H
global.h
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
BALL::TMutexLocker
Definition mutex.h:74
BALL::TMutexLocker::TMutexLocker
TMutexLocker(MutexType *mutex)
Definition mutex.h:76
BALL::TMutex< QMutex >::TMutex
TMutex(bool is_recursive=false)
Definition mutex.h:55
BALL::TMutex< boost::mutex >::tryLock
bool tryLock()
Definition mutex.h:165
BALL::TMutex< boost::mutex >::TMutex
TMutex()
Definition mutex.h:156
BALL::TMutex< boost::mutex >::lock
void lock()
Definition mutex.h:160
BALL::TMutex
Definition mutex.h:43
BALL::TMutex::TMutex
TMutex(bool is_recursive=false)
Definition mutex.h:45
BALL::TReadLocker
Definition mutex.h:122
BALL::TReadLocker::TReadLocker
TReadLocker(ReadWriteLockType *lock)
Definition mutex.h:124
BALL::TReadWriteLock< QReadWriteLock >::TReadWriteLock
TReadWriteLock(bool is_recursive=false)
Definition mutex.h:104
BALL::TReadWriteLock
Definition mutex.h:92
BALL::TReadWriteLock::TReadWriteLock
TReadWriteLock(bool is_recursive=false)
Definition mutex.h:94
BALL::TWriteLocker
Definition mutex.h:142
BALL::TWriteLocker::TWriteLocker
TWriteLocker(ReadWriteLockType *lock)
Definition mutex.h:144
MutexLockerType
MutexType
ReadLockerType
ReadWriteLockType
WriteLockerType
BALL
Definition constants.h:13
BALL::MutexLocker
TMutexLocker< BALL_DEFAULT_MUTEXLOCKER_TYPE, BALL_DEFAULT_MUTEX_TYPE > MutexLocker
Definition mutex.h:226
BALL::WriteLocker
TWriteLocker< BALL_DEFAULT_WRITELOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE > WriteLocker
Definition mutex.h:228
BALL::Mutex
TMutex< BALL_DEFAULT_MUTEX_TYPE > Mutex
Definition mutex.h:224
BALL::ReadLocker
TReadLocker< BALL_DEFAULT_READLOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE > ReadLocker
Definition mutex.h:227
BALL::ReadWriteLock
TReadWriteLock< BALL_DEFAULT_READWRITELOCK_TYPE > ReadWriteLock
Definition mutex.h:225
boost
Definition graphAlgorithms.h:27