Back to index

salome-kernel  6.5.0
Utils_Mutex.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library 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 GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Utils : general SALOME's definitions and tools
00024 //  File:       Utils_Mutex.cxx
00025 //  Author:     Sergey ANIKIN
00026 //  Module :    SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Utils/Utils_Mutex.cxx,v 1.5.2.1.10.2.12.1 2012-04-12 14:05:32 vsr Exp $
00028 //
00029 #include "Utils_Mutex.hxx"
00030 
00031 Utils_Mutex::Utils_Mutex() 
00032 : myCount( 0 )
00033 {
00034   pthread_mutex_init( &myMutex, 0 );
00035   pthread_mutex_init( &myHelperMutex, 0 );
00036 }
00037 
00038 Utils_Mutex::~Utils_Mutex()
00039 {
00040   pthread_mutex_destroy( &myHelperMutex );
00041   pthread_mutex_destroy( &myMutex );
00042 }
00043 
00044 void Utils_Mutex::lock()
00045 {
00046   pthread_mutex_lock( &myHelperMutex );
00047 
00048 #ifndef WIN32 
00049   if ( myCount > 0 && myThread == pthread_self() ) {
00050 #else
00051   if ( myCount > 0 && myThread.p == pthread_self().p ) {
00052 #endif
00053     myCount++;
00054   }
00055   else {
00056     pthread_mutex_unlock( &myHelperMutex );
00057     pthread_mutex_lock( &myMutex );
00058     pthread_mutex_lock( &myHelperMutex );
00059     myCount = 1;
00060     myThread = pthread_self();
00061   }
00062   
00063   pthread_mutex_unlock( &myHelperMutex );
00064 }
00065 
00066 void Utils_Mutex::unlock()
00067 {
00068   pthread_mutex_lock( &myHelperMutex );
00069 
00070 #ifndef WIN32  
00071   if ( myThread == pthread_self() ) {
00072 #else
00073   if ( myThread.p == pthread_self().p ) {
00074 #endif
00075     if ( myCount && (--myCount) < 1 ) {
00076       myCount = 0;
00077       pthread_mutex_unlock( &myMutex );   
00078     }
00079   }
00080   
00081   pthread_mutex_unlock( &myHelperMutex );
00082 }
00083 
00084 Utils_Locker::Utils_Locker( Utils_Mutex* mutex )
00085 : myMutex( mutex ) 
00086 { 
00087   if ( myMutex ) myMutex->lock(); 
00088 }
00089 
00090 Utils_Locker::~Utils_Locker() 
00091 {
00092   if ( myMutex ) myMutex->unlock(); 
00093 }