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

lock.h

Go to the documentation of this file.
00001 /* Copyright (C) 2008 by Marc Maurer <uwog@uwog.net>
00002  *
00003  * This program is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU General Public License
00005  * as published by the Free Software Foundation; either version 2
00006  * of the License, or (at your option) any later version.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00016  * 02110-1301 USA.
00017  */
00018 
00019 #ifndef __ABICOLLAB_LOCK__
00020 #define __ABICOLLAB_LOCK__
00021 
00022 #ifndef _WIN32
00023 #include <pthread.h>
00024 #endif
00025 
00026 namespace abicollab
00027 {
00028 
00029 class scoped_lock;
00030 
00031 class mutex
00032 {
00033 friend class scoped_lock;
00034 
00035 public:
00036     mutex()
00037     {
00038 #ifdef _WIN32
00039         repr = CreateMutex(0, FALSE, 0);
00040 #else
00041         pthread_mutex_init(&repr, NULL);
00042 #endif
00043     }
00044 
00045     ~mutex()
00046     {
00047 #ifdef _WIN32
00048         CloseHandle(repr);
00049 #else
00050         pthread_mutex_destroy(&repr);
00051 #endif
00052     }
00053 
00054 private:
00055     // we are noncopyable
00056     mutex( const mutex& );
00057     const mutex& operator=( const mutex& );
00058 
00059 #ifdef _WIN32
00060     HANDLE repr;
00061 #else
00062     pthread_mutex_t repr;
00063 #endif
00064 };
00065 
00066 class scoped_lock
00067 {
00068 public:
00069     scoped_lock(mutex& mutex)
00070         : m_mutex(mutex)
00071     {
00072 #ifdef _WIN32
00073         WaitForSingleObject(m_mutex.repr, INFINITE);
00074 #else
00075         pthread_mutex_lock(&m_mutex.repr);
00076 #endif
00077     }
00078 
00079     ~scoped_lock()
00080     {
00081 #ifdef _WIN32
00082         ReleaseMutex(m_mutex.repr);
00083 #else
00084         pthread_mutex_unlock(&m_mutex.repr);
00085 #endif
00086     }
00087 
00088 private:
00089     // we are noncopyable
00090     scoped_lock( const scoped_lock& );
00091     const scoped_lock& operator=( const scoped_lock& );
00092 
00093     mutex& m_mutex;
00094 };
00095 
00096 }
00097 
00098 #endif /* __ABICOLLAB_LOCK__ */

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1