#ifndef GMutexLocker_h
#define GMutexLocker_h
#if USE(GLIB)
#include <glib.h>
#include <wtf/Noncopyable.h>
namespace WTF {
template<typename T>
struct MutexWrapper;
template<>
struct MutexWrapper<GMutex> {
static void lock(GMutex* mutex)
{
g_mutex_lock(mutex);
}
static void unlock(GMutex* mutex)
{
g_mutex_unlock(mutex);
}
};
template<>
struct MutexWrapper<GRecMutex> {
static void lock(GRecMutex* mutex)
{
g_rec_mutex_lock(mutex);
}
static void unlock(GRecMutex* mutex)
{
g_rec_mutex_unlock(mutex);
}
};
template<typename T>
class GMutexLocker {
WTF_MAKE_NONCOPYABLE(GMutexLocker);
public:
explicit GMutexLocker(T& mutex)
: m_mutex(mutex)
, m_locked(false)
{
lock();
}
~GMutexLocker()
{
unlock();
}
void lock()
{
if (m_locked)
return;
MutexWrapper<T>::lock(&m_mutex);
m_locked = true;
}
void unlock()
{
if (!m_locked)
return;
m_locked = false;
MutexWrapper<T>::unlock(&m_mutex);
}
private:
T& m_mutex;
bool m_locked;
};
}
#endif // USE(GLIB)
#endif // GMutexLocker_h