Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qmutexpool.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qmutexpool.cpp,v 1.2 2003/07/10 02:40:12 llornkcor Exp $
00003 **
00004 ** ...
00005 **
00006 ** Copyright (C) 2002 Trolltech AS.  All rights reserved.
00007 **
00008 ** This file is part of the tools module of the Qt GUI Toolkit.
00009 **
00010 ** This file may be distributed under the terms of the Q Public License
00011 ** as defined by Trolltech AS of Norway and appearing in the file
00012 ** LICENSE.QPL included in the packaging of this file.
00013 **
00014 ** This file may be distributed and/or modified under the terms of the
00015 ** GNU General Public License version 2 as published by the Free Software
00016 ** Foundation and appearing in the file LICENSE.GPL included in the
00017 ** packaging of this file.
00018 **
00019 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00020 ** licenses may use this file in accordance with the Qt Commercial License
00021 ** Agreement provided with the Software.
00022 **
00023 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00024 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00025 **
00026 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00027 **   information about Qt Commercial License Agreements.
00028 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00029 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00030 **
00031 ** Contact info@trolltech.com if any conditions of this licensing are
00032 ** not clear to you.
00033 **
00034 **********************************************************************/
00035 
00036 #include "qmutexpool_p.h"
00037 
00038 #ifdef QT_THREAD_SUPPORT
00039 
00040 #include <qthread.h>
00041 
00042 QMutexPool *qt_global_mutexpool = 0;
00043 
00044 
00106 QMutexPool::QMutexPool( bool recursive, int size )
00107     : mutex( FALSE ), count( size ), recurs( recursive )
00108 {
00109     mutexes = new QMutex*[count];
00110     for ( int index = 0; index < count; ++index ) {
00111         mutexes[index] = 0;
00112     }
00113 }
00114 
00119 QMutexPool::~QMutexPool()
00120 {
00121     QMutexLocker locker( &mutex );
00122     for ( int index = 0; index < count; ++index ) {
00123         delete mutexes[index];
00124         mutexes[index] = 0;
00125     }
00126     delete [] mutexes;
00127     mutexes = 0;
00128 }
00129 
00134 QMutex *QMutexPool::get( void *address )
00135 {
00136     int index = (int) ( (unsigned long) address % count );
00137 
00138     if ( ! mutexes[index] ) {
00139         // mutex not created, create one
00140 
00141         QMutexLocker locker( &mutex );
00142         // we need to check once again that the mutex hasn't been created, since
00143         // 2 threads could be trying to create a mutex as the same index...
00144         if ( ! mutexes[index] ) {
00145             mutexes[index] = new QMutex( recurs );
00146         }
00147     }
00148 
00149     return mutexes[index];
00150 }
00151 
00152 #endif

Generated on Sat Nov 5 16:18:31 2005 for OPIE by  doxygen 1.4.2