00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 * Thanks: Jose Renato Santos 00017 * 00018 */ 00019 00020 /////////////////////////////////////////////////////////////////////////////// 00021 // RioMutex.cpp: implementation of the CMutex class. 00022 ////////////////////////////////////////////////////////////////////// 00023 // ### should probably use "critical sections" on NT, not Mutex 00024 00025 #ifdef WINDOWS 00026 // Windows implementation 00027 // This was included to use in Visual Studio 6.0 00028 #include "stdafx.h" 00029 #else 00030 // Linux implementation 00031 #endif 00032 00033 /* ------------------------------ */ 00034 00035 #include <iostream> 00036 00037 #ifdef WINDOWS 00038 #include <windows.h> 00039 #endif 00040 00041 #include "RioMutex.h" 00042 00043 ////////////////////////////////////////////////////////////////////// 00044 // Construction/Destruction 00045 ////////////////////////////////////////////////////////////////////// 00046 00047 CMutex::CMutex() 00048 { 00049 #ifdef WINDOWS 00050 // WINDOWS32 implementation 00051 m_handle = CreateMutex( NULL, FALSE, NULL ); 00052 if( m_handle == INVALID_HANDLE_VALUE ) 00053 { 00054 m_isopen = false; 00055 } 00056 else 00057 { 00058 m_isopen = true; 00059 } 00060 #else 00061 // UNIX implementation 00062 int result = pthread_mutex_init( &m_mutex, NULL ); 00063 if( result == 0 ) 00064 { 00065 m_isopen = true; 00066 } 00067 else 00068 { 00069 m_isopen = false; 00070 } 00071 #endif 00072 } 00073 00074 CMutex::~CMutex() 00075 { 00076 if( m_isopen ) 00077 { 00078 #ifdef WINDOWS 00079 // WINDOWS32 implementation 00080 CloseHandle (m_handle); 00081 #else 00082 // UNIX implementation 00083 pthread_mutex_destroy(&m_mutex); 00084 #endif 00085 } 00086 } 00087 00088 void CMutex::Wait() 00089 { 00090 #ifdef WINDOWS 00091 // WINDOWS32 implementation 00092 WaitForSingleObject(m_handle,INFINITE); 00093 #else 00094 // UNIX implementation 00095 pthread_mutex_lock( &m_mutex ); 00096 #endif 00097 } 00098 00099 void CMutex::Release() 00100 { 00101 #ifdef WINDOWS 00102 // WINDOWS32 implementation 00103 ReleaseMutex( m_handle ); 00104 #else 00105 // UNIX implementation 00106 pthread_mutex_unlock( &m_mutex ); 00107 #endif 00108 } 00109 00110 bool CMutex::IsOpen() 00111 { 00112 return m_isopen; 00113 }