00001 #ifndef ROOT_TQtLockGuard 00002 #define ROOT_TQtLockGuard 00003 00004 ////////////////////////////////////////////////////////////////////////// 00005 // // 00006 // TQtLockGuard - Qt-based implementation of ROOT TLockGuard class // 00007 // // 00008 // This class provides mutex resource management in a guaranteed and // 00009 // exception safe way. Use like this: // 00010 // { // 00011 // TQtLockGuard guard(mutex); // 00012 // ... // do something // 00013 // } // 00014 // when guard goes out of scope the mutex is unlocked in the TLockGuard // 00015 // destructor. The exception mechanism takes care of calling the dtors // 00016 // of local objects so it is exception safe. // 00017 // // 00018 // The macro Q__LOCKGUARD2(QMutex *mutex) // 00019 // creates first creates the QMutex object and then creates // 00020 // TQtLockGuard as above if the QT_THREAD_SUPPORT // 00021 // was provided // 00022 // // 00023 // NOTE: This class may be removed as soon as // 00024 // ---- ROOT TThreadImp class QThread-based implemantion // 00025 // is adopted by ROOT team (one needs to convince Fons ) // 00026 // // 00027 ////////////////////////////////////////////////////////////////////////// 00028 00029 #include "qmutex.h" 00030 00031 class TQtLockGuard { 00032 00033 private: 00034 QMutex *fMutex; 00035 00036 public: 00037 TQtLockGuard(QMutex *mutex); 00038 ~TQtLockGuard(); 00039 }; 00040 00041 //____________________________________________________________________ 00042 inline TQtLockGuard::TQtLockGuard(QMutex *mutex) 00043 { fMutex = mutex; if (fMutex) fMutex->lock(); } 00044 00045 //____________________________________________________________________ 00046 inline TQtLockGuard::~TQtLockGuard() 00047 { if (fMutex) fMutex->unlock(); } 00048 00049 00050 // Zero overhead macros in case not compiled with thread support 00051 #ifdef QT_THREAD_SUPPORT 00052 00053 #define Q__LOCKGUARD(mutex) TQtLockGuard QR__guard(mutex) 00054 00055 #define Q__LOCKGUARD2(mutex) { \ 00056 if (qApp && !mutex) { \ 00057 qApp->lock(); \ 00058 if (!mutex) mutex = new QMutex(true); \ 00059 qApp->unlock(); \ 00060 } \ 00061 Q__LOCKGUARD(mutex); \ 00062 } 00063 #else 00064 #define Q__LOCKGUARD(mutex) if (mutex) { } 00065 #define Q__LOCKGUARD2(mutex) if (mutex) { } 00066 #endif 00067 00068 #endif