cmtkMutexLock.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //  Copyright 2004-2010 SRI International
00005 //
00006 //  This file is part of the Computational Morphometry Toolkit.
00007 //
00008 //  http://www.nitrc.org/projects/cmtk/
00009 //
00010 //  The Computational Morphometry Toolkit is free software: you can
00011 //  redistribute it and/or modify it under the terms of the GNU General Public
00012 //  License as published by the Free Software Foundation, either version 3 of
00013 //  the License, or (at your option) any later version.
00014 //
00015 //  The Computational Morphometry Toolkit is distributed in the hope that it
00016 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License along
00021 //  with the Computational Morphometry Toolkit.  If not, see
00022 //  <http://www.gnu.org/licenses/>.
00023 //
00024 //  $Revision: 2398 $
00025 //
00026 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00027 //
00028 //  $LastChangedBy: torstenrohlfing $
00029 //
00030 */
00031 
00032 #ifndef __cmtkMutexLock_h_included_
00033 #define __cmtkMutexLock_h_included_
00034 
00035 #include <cmtkconfig.h>
00036 
00037 #include <System/cmtkCannotBeCopied.h>
00038 
00039 #if defined(CMTK_USE_THREADS)
00040 #  include <pthread.h>
00041 #endif
00042 
00043 #ifdef _MSC_VER
00044 #  include <Windows.h>
00045 #  include <iostream>
00046 #endif
00047 
00048 namespace
00049 cmtk
00050 {
00051 
00054 
00059 class MutexLock :
00061   private CannotBeCopied
00062 {
00063 public:
00065   MutexLock() 
00066   {
00067 #if defined(CMTK_USE_THREADS)
00068     pthread_mutex_init( &this->m_MutexLock, NULL );
00069 #else
00070 #ifdef _MSC_VER
00071     this->m_MutexObject = CreateMutex( NULL /*default security attributes*/, FALSE /*initially not owned*/, NULL /*unnamed*/ );
00072     if ( ! this->m_MutexObject )
00073       {
00074       std::cerr << "FATAL: cannot create mutex object\n";
00075       exit( 1 );
00076       }
00077 #endif
00078 #endif
00079   }
00080 
00082   ~MutexLock() 
00083   {
00084 #if defined(CMTK_USE_THREADS)
00085     pthread_mutex_destroy( &this->m_MutexLock );
00086 #else
00087 #ifdef _MSC_VER
00088     CloseHandle( this->m_MutexObject );
00089 #endif
00090 #endif
00091   }
00092 
00094   void Lock() 
00095   {
00096 #if defined(CMTK_USE_THREADS)
00097     pthread_mutex_lock( &this->m_MutexLock );
00098 #else
00099 #ifdef _MSC_VER
00100     WaitForSingleObject( this->m_MutexObject, INFINITE );
00101 #endif
00102 #endif
00103   }
00104 
00106   void Unlock() 
00107   {
00108 #if defined(CMTK_USE_THREADS)
00109     pthread_mutex_unlock( &this->m_MutexLock );
00110 #else
00111 #ifdef _MSC_VER
00112     ReleaseMutex( this->m_MutexObject );
00113 #endif
00114 #endif
00115   }
00116 
00117 #if defined(CMTK_USE_THREADS)
00118 protected:
00121    pthread_mutex_t m_MutexLock;
00122 #else
00123 #ifdef _MSC_VER
00124   HANDLE m_MutexObject;
00125 #endif
00126 #endif
00127 };
00128 
00130 
00131 } // namespace cmtk
00132 
00133 #endif // #ifndef __cmtkMutexLock_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines