1 // SALOME Utils : general SALOME's definitions and tools
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 // File: Utils_Mutex.cxx
24 // Author: Sergey ANIKIN
29 #include <Utils_Mutex.hxx>
31 Utils_Mutex::Utils_Mutex()
34 pthread_mutex_init( &myMutex, 0 );
35 pthread_mutex_init( &myHelperMutex, 0 );
38 Utils_Mutex::~Utils_Mutex()
40 pthread_mutex_destroy( &myHelperMutex );
41 pthread_mutex_destroy( &myMutex );
44 void Utils_Mutex::lock()
46 pthread_mutex_lock( &myHelperMutex );
48 if ( myCount > 0 && myThread == pthread_self() ) {
52 pthread_mutex_unlock( &myHelperMutex );
53 pthread_mutex_lock( &myMutex );
54 pthread_mutex_lock( &myHelperMutex );
56 myThread = pthread_self();
59 pthread_mutex_unlock( &myHelperMutex );
62 void Utils_Mutex::unlock()
64 pthread_mutex_lock( &myHelperMutex );
66 if ( myThread == pthread_self() ) {
67 if ( myCount && (--myCount) < 1 ) {
69 pthread_mutex_unlock( &myMutex );
73 pthread_mutex_unlock( &myHelperMutex );
76 Utils_Locker::Utils_Locker( Utils_Mutex* mutex )
79 if ( myMutex ) myMutex->lock();
82 Utils_Locker::~Utils_Locker()
84 if ( myMutex ) myMutex->unlock();