1/* 2 * Copyright (C) 2013 Collabora Ltd. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20#ifndef GMutexLocker_h 21#define GMutexLocker_h 22 23#if USE(GLIB) 24 25#include <glib.h> 26 27#include <wtf/FastMalloc.h> 28#include <wtf/Noncopyable.h> 29 30namespace WebCore { 31 32class GMutexLocker { 33 WTF_MAKE_NONCOPYABLE(GMutexLocker); WTF_MAKE_FAST_ALLOCATED; 34 35public: 36 inline explicit GMutexLocker(GMutex* mutex) 37 : m_mutex(mutex) 38 , m_val(0) 39 { 40 lock(); 41 } 42 43 inline ~GMutexLocker() { unlock(); } 44 45 inline void lock() 46 { 47 if (m_mutex && !m_val) { 48 g_mutex_lock(m_mutex); 49 m_val = 1; 50 } 51 } 52 53 inline void unlock() 54 { 55 if (m_mutex && m_val) { 56 m_val = 0; 57 g_mutex_unlock(m_mutex); 58 } 59 } 60 61 inline GMutex* mutex() const { return m_mutex; } 62 63private: 64 GMutex* m_mutex; 65 uint8_t m_val; 66}; 67 68} 69 70#endif // USE(GLIB) 71 72#endif // GMutexLocker_h 73