• 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., 59 Temple Place - Suite 330, Boston, MA
00019  * 02111-1307, 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 (!g_thread_supported ()) g_thread_init (NULL);
00039 
00040             mMutex = g_mutex_new () ;
00041             UT_ASSERT ( mMutex ) ;
00042         }
00043 
00044     ~UT_MutexImpl ()
00045         {
00046             if ( mMutex )
00047                 g_mutex_free ( mMutex ) ;
00048         }
00049 
00050   void lock ()
00051         {
00052             if ( mMutex && mLocker != g_thread_self())
00053                 g_mutex_lock ( mMutex ) ;
00054             mLocker = g_thread_self();
00055             iLockCount++;
00056         }
00057 
00058   void unlock ()
00059         {
00060             UT_ASSERT(mLocker == g_thread_self());
00061             if (--iLockCount == 0 && mMutex)
00062                 g_mutex_unlock ( mMutex ) ;
00063         }
00064 
00065 private:
00066 
00067     // no impls
00068     UT_MutexImpl (const UT_MutexImpl & other);
00069     UT_MutexImpl & operator=(const UT_MutexImpl & other);
00070 
00071     GMutex *mMutex;
00072 
00073     // Damn it, recursive locking is not guaranteed.
00074     GThread *mLocker;
00075     int iLockCount;
00076 };
00077 
00078 #endif /* UT_MUTEXIMPL_H */

Generated on Mon May 28 2012 for AbiWord by  doxygen 1.7.1