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

qwaitcondition_unix.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qwaitcondition_unix.cpp,v 1.2 2003/07/10 02:40:12 llornkcor Exp $
00003 **
00004 ** QWaitCondition class for Unix
00005 **
00006 ** Created : 20010725
00007 **
00008 ** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
00009 **
00010 ** This file is part of the tools module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech AS of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #if defined(QT_THREAD_SUPPORT)
00039 
00040 #include "qplatformdefs.h"
00041 
00042 typedef pthread_mutex_t Q_MUTEX_T;
00043 
00044 #include "qwaitcondition.h"
00045 #include "qmutex.h"
00046 #include "qmutex_p.h"
00047 
00048 #include <errno.h>
00049 #include <string.h>
00050 
00051 
00052 struct QWaitConditionPrivate {
00053     pthread_cond_t cond;
00054 };
00055 
00056 
00145 QWaitCondition::QWaitCondition()
00146 {
00147     d = new QWaitConditionPrivate;
00148 
00149     int ret = pthread_cond_init(&d->cond, NULL);
00150 
00151 #ifdef QT_CHECK_RANGE
00152     if (ret)
00153         qWarning( "Wait condition init failure: %s", strerror( ret ) );
00154 #endif
00155 }
00156 
00157 
00161 QWaitCondition::~QWaitCondition()
00162 {
00163     int ret = pthread_cond_destroy(&d->cond);
00164 
00165     if (ret) {
00166 #ifdef QT_CHECK_RANGE
00167         qWarning( "Wait condition destroy failure: %s", strerror( ret ) );
00168 #endif
00169 
00170         // seems we have threads waiting on us, lets wake them up
00171         pthread_cond_broadcast(&d->cond);
00172     }
00173 
00174     delete d;
00175 }
00176 
00184 void QWaitCondition::wakeOne()
00185 {
00186     int ret = pthread_cond_signal(&d->cond);
00187 
00188 #ifdef QT_CHECK_RANGE
00189     if (ret)
00190         qWarning("Wait condition wakeOne failure: %s", strerror(ret));
00191 #endif
00192 }
00193 
00201 void QWaitCondition::wakeAll()
00202 {
00203     int ret = pthread_cond_broadcast(&d->cond);
00204 
00205 #ifdef QT_CHECK_RANGE
00206     if (ret)
00207         qWarning("Wait condition wakeAll failure: %s", strerror(ret));
00208 #endif
00209 }
00210 
00225 bool QWaitCondition::wait(unsigned long time)
00226 {
00227     pthread_mutex_t mutex;
00228     pthread_mutex_init( &mutex, 0 );
00229     pthread_mutex_lock( &mutex );
00230 
00231     int ret;
00232     if (time != ULONG_MAX) {
00233         struct timeval tv;
00234         gettimeofday(&tv, 0);
00235 
00236         timespec ti;
00237         ti.tv_nsec = ( tv.tv_usec + ( time % 1000 ) * 1000 ) * 1000;
00238         ti.tv_sec = tv.tv_sec + (time / 1000) + ( ti.tv_nsec / 1000000000 );
00239         ti.tv_nsec %= 1000000000;
00240 
00241         ret = pthread_cond_timedwait(&d->cond, &mutex, &ti);
00242     } else
00243         ret = pthread_cond_wait(&d->cond, &mutex);
00244 
00245 #ifdef QT_CHECK_RANGE
00246     if (ret && ret != ETIMEDOUT)
00247         qWarning("Wait condition wait failure: %s",strerror(ret));
00248 #endif
00249 
00250     pthread_mutex_unlock( &mutex );
00251     pthread_mutex_destroy( &mutex );
00252 
00253     return (ret == 0);
00254 }
00255 
00280 bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
00281 {
00282     if (! mutex)
00283         return FALSE;
00284 
00285     if (mutex->d->type() == Q_MUTEX_RECURSIVE) {
00286 #ifdef QT_CHECK_RANGE
00287         qWarning("Wait condition warning: using recursive mutexes with\n"
00288                  "                        wait conditions is undefined!");
00289 #endif
00290         return FALSE;
00291     }
00292 
00293     int ret;
00294     if (time != ULONG_MAX) {
00295         struct timeval tv;
00296         gettimeofday(&tv, 0);
00297 
00298         timespec ti;
00299         ti.tv_nsec = ( tv.tv_usec + ( time % 1000 ) * 1000 ) * 1000;
00300         ti.tv_sec = tv.tv_sec + (time / 1000) + ( ti.tv_nsec / 1000000000 );
00301         ti.tv_nsec %= 1000000000;
00302 
00303         ret = pthread_cond_timedwait(&d->cond, &mutex->d->handle, &ti);
00304     } else
00305         ret = pthread_cond_wait(&d->cond, &mutex->d->handle);
00306 
00307 #ifdef QT_CHECK_RANGE
00308     if (ret && ret != ETIMEDOUT)
00309         qWarning("Wait condition wait failure: %s",strerror(ret));
00310 #endif
00311 
00312     return (ret == 0);
00313 }
00314 
00315 #endif // QT_THREAD_SUPPORT

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