28 #ifndef SHAREDSTRUCTURES_QT_H
29 #define SHAREDSTRUCTURES_QT_H
31 #include "commonGlobal.h"
33 #include <qsemaphore.h>
35 #include <qeventloop.h>
39 #include "sharedStructures.h"
46 #define ITOM_PYNOTACCESSIBLE
49 #if !defined(Q_MOC_RUN) || defined(ITOMCOMMONQT_MOC) //only moc this file in itomCommonQtLib but not in other libraries or executables linking against this itomCommonQtLib
51 typedef QMap<QString, ito::Param> ParamMap;
52 typedef ParamMap::iterator ParamMapIterator;
78 m_numOfListeners(numberOfListeners),
79 m_instCounter(numberOfListeners + 1),
80 m_enableDelete(false),
81 m_callerStillWaiting(true),
82 returnValue(
ito::retOk)
84 QMutexLocker mutexLocker(&internalMutex);
85 m_pSemaphore =
new QSemaphore(m_numOfListeners);
86 m_pSemaphore->acquire(m_numOfListeners);
95 Q_ASSERT_X(m_enableDelete,
"~ItomSharedSemaphore",
"it is not allowed to directly destroy ItomSharedSemaphore. Always use ItomSharedSemaphore::deleteSemaphore(...)");
97 if(m_pSemaphore->available() < m_numOfListeners)
99 m_pSemaphore->release( m_numOfListeners - m_pSemaphore->available());
100 qDebug(
"ItomSharedSemaphore is not fully available at moment of destruction");
126 success = m_pSemaphore->tryAcquire(m_numOfListeners, timeout);
132 m_pSemaphore->acquire(m_numOfListeners);
136 QMutexLocker mutexLocker(&internalMutex);
139 qDebug() <<
"ItomSharedSemaphore run into a timeout. Number of attempted listeners: " << m_numOfListeners <<
", already freed: " << m_pSemaphore->available();
143 m_pSemaphore->release(m_numOfListeners);
146 m_callerStillWaiting =
false;
168 bool waitAndProcessEvents(
int timeout, QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
174 inline void release() { m_pSemaphore->release(1); }
180 inline int available()
const {
return m_pSemaphore->available(); }
207 QMutexLocker mutexLocker(&internalMutex);
210 if(m_instCounter <= 0)
212 m_enableDelete =
true;
213 mutexLocker.unlock();
280 #endif //#if !defined(Q_MOC_RUN) || defined(ITOMCOMMONQT_MOC)
~ItomSharedSemaphore()
destructor (do not call directly, instead free the semaphore by ItomSharedSemaphore::deleteSemaphore ...
Definition: sharedStructuresQt.h:93
Locker-class for ItomSharedSemaphore. The functionality is equal to QMutexLocker in Qt...
Definition: sharedStructuresQt.h:232
QSemaphore * m_pSemaphore
Definition: sharedStructuresQt.h:60
void deleteSemaphore(void)
static method to decrease the reference counter of any ItomSharedSemaphore or delete it if the refere...
Definition: sharedStructuresQt.h:205
~ItomSharedSemaphoreLocker()
destructor.
Definition: sharedStructuresQt.h:242
Class for managing status values (like errors or warning)
Definition: retVal.h:54
ItomSharedSemaphoreLocker(ItomSharedSemaphore *semaphore)
constructor with ItomSharedSemaphore-pointer as parameter. This semaphore will be guarded by this loc...
Definition: sharedStructuresQt.h:236
ItomSharedSemaphore * getSemaphore() const
returns the pointer to the guarded ItomSharedSemaphore.
Definition: sharedStructuresQt.h:252
Definition: apiFunctionsGraph.cpp:39
int m_instCounter
Definition: sharedStructuresQt.h:61
bool m_enableDelete
Definition: sharedStructuresQt.h:62
bool isCallerStillWaiting()
indicates whether caller-method is still waiting that the lock is released by the listener(s)...
Definition: sharedStructuresQt.h:190
semaphore which can be used for asychronous thread communication. By using this class it is possible ...
Definition: sharedStructuresQt.h:57
QMutex internalMutex
Definition: sharedStructuresQt.h:65
ItomSharedSemaphoreLocker()
empty constructor. The locker will not guard any semaphore yet.
Definition: sharedStructuresQt.h:239
ItomSharedSemaphore(int numberOfListeners=1)
constructor
Definition: sharedStructuresQt.h:77
ItomSharedSemaphoreLocker & operator=(ItomSharedSemaphore *newSemaphoreInstance)
assigns another ItomSharedSemaphore to this locker.
Definition: sharedStructuresQt.h:261
ItomSharedSemaphore * m_semaphore
Definition: sharedStructuresQt.h:275
ItomSharedSemaphore * operator->() const
returns the pointer to the guarded ItomSharedSemaphore.
Definition: sharedStructuresQt.h:255
int m_numOfListeners
Definition: sharedStructuresQt.h:63
int available() const
checks whether the semaphore is still locked or not
Definition: sharedStructuresQt.h:180
void release()
decreases the number of locks by one
Definition: sharedStructuresQt.h:174
ito::RetVal returnValue
Definition: sharedStructuresQt.h:221
bool m_callerStillWaiting
Definition: sharedStructuresQt.h:64
bool wait(int timeout)
The call of this method returns if a certain timeout has been expired or every listener released the ...
Definition: sharedStructuresQt.h:120