• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ut_mutexImpl.h

Go to the documentation of this file.
00001 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
00002 
00003 /* AbiSource Program Utilities
00004  * Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019  * 02110-1301 USA.
00020  */
00021 
00022 #ifndef UT_MUTEXIMPL_H
00023 #define UT_MUTEXIMPL_H
00024 
00025 #include <glib.h>
00026 #include "ut_assert.h"
00027 
00031 class UT_MutexImpl
00032 {
00033 public:
00034 
00035     UT_MutexImpl ()
00036         : mMutex ( 0 )
00037         {
00038 #if GLIB_CHECK_VERSION(2,32,0)
00039             mMutex = &mStaticMutex;
00040             g_mutex_init(&mStaticMutex);
00041 #else
00042             if (!g_thread_supported ())
00043                 g_thread_init (NULL);
00044             mMutex = g_mutex_new () ;
00045             UT_ASSERT ( mMutex ) ;
00046 #endif
00047         }
00048 
00049     ~UT_MutexImpl ()
00050         {
00051 #if GLIB_CHECK_VERSION(2,32,0)
00052             g_mutex_clear(&mStaticMutex);
00053 #else
00054             if ( mMutex )
00055                 g_mutex_free ( mMutex ) ;
00056 #endif
00057         }
00058 
00059   void lock ()
00060         {
00061             if ( mMutex && mLocker != g_thread_self())
00062                 g_mutex_lock ( mMutex ) ;
00063             mLocker = g_thread_self();
00064             iLockCount++;
00065         }
00066 
00067   void unlock ()
00068         {
00069             UT_ASSERT(mLocker == g_thread_self());
00070             if (--iLockCount == 0 && mMutex)
00071                 g_mutex_unlock ( mMutex ) ;
00072         }
00073 
00074 private:
00075 
00076     // no impls
00077     UT_MutexImpl (const UT_MutexImpl & other);
00078     UT_MutexImpl & operator=(const UT_MutexImpl & other);
00079 
00080 // TODO: when we require Glib 2.32 or up, we can remove all that
00081 // junk and just keep the static mutex.
00082 #if GLIB_CHECK_VERSION(2,32,0)
00083     GMutex mStaticMutex;
00084 #endif
00085     GMutex *mMutex;
00086 
00087     // Damn it, recursive locking is not guaranteed.
00088     GThread *mLocker;
00089     int iLockCount;
00090 };
00091 
00092 #endif /* UT_MUTEXIMPL_H */

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1