diff options
author | David Boddie <david.boddie@nokia.com> | 2011-06-17 11:54:16 (GMT) |
---|---|---|
committer | David Boddie <david.boddie@nokia.com> | 2011-06-17 11:54:16 (GMT) |
commit | 2b39081bf101e53a70ff056d527dc3dfb8208bf0 (patch) | |
tree | a8e57489e18dbdd620b8880eda5c8e2d77049a07 /src/corelib/kernel | |
parent | 5e1112ec04dcef671700e326186ca56b790f7e08 (diff) | |
parent | c390312f8fbea14ac4df0c5185ca814054f448d8 (diff) | |
download | Qt-2b39081bf101e53a70ff056d527dc3dfb8208bf0.zip Qt-2b39081bf101e53a70ff056d527dc3dfb8208bf0.tar.gz Qt-2b39081bf101e53a70ff056d527dc3dfb8208bf0.tar.bz2 |
Merge branch '4.8' of scm.dev.nokia.troll.no:qt/qt
Conflicts:
doc/src/declarative/righttoleft.qdoc
examples/draganddrop/fridgemagnets/main.cpp
examples/script/context2d/main.cpp
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qcore_symbian_p.cpp | 74 | ||||
-rw-r--r-- | src/corelib/kernel/qcore_symbian_p.h | 13 | ||||
-rw-r--r-- | src/corelib/kernel/qcoreapplication.cpp | 7 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_symbian.cpp | 391 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_symbian_p.h | 40 | ||||
-rw-r--r-- | src/corelib/kernel/qsharedmemory.cpp | 26 | ||||
-rw-r--r-- | src/corelib/kernel/qsharedmemory_p.h | 12 | ||||
-rw-r--r-- | src/corelib/kernel/qsharedmemory_symbian.cpp | 50 | ||||
-rw-r--r-- | src/corelib/kernel/qsharedmemory_unix.cpp | 44 | ||||
-rw-r--r-- | src/corelib/kernel/qsharedmemory_win.cpp | 63 | ||||
-rw-r--r-- | src/corelib/kernel/qsystemsemaphore_p.h | 8 | ||||
-rw-r--r-- | src/corelib/kernel/qsystemsemaphore_symbian.cpp | 49 | ||||
-rw-r--r-- | src/corelib/kernel/qsystemsemaphore_unix.cpp | 52 | ||||
-rw-r--r-- | src/corelib/kernel/qsystemsemaphore_win.cpp | 17 |
14 files changed, 337 insertions, 509 deletions
diff --git a/src/corelib/kernel/qcore_symbian_p.cpp b/src/corelib/kernel/qcore_symbian_p.cpp index 5b52ec2..04acfb0 100644 --- a/src/corelib/kernel/qcore_symbian_p.cpp +++ b/src/corelib/kernel/qcore_symbian_p.cpp @@ -109,80 +109,6 @@ QHBufC::~QHBufC() delete m_hBufC; } -class QS60PluginResolver -{ -public: - QS60PluginResolver() - : initTried(false) {} - - ~QS60PluginResolver() { - lib.Close(); - } - - TLibraryFunction resolve(int ordinal) { - if (!initTried) { - init(); - initTried = true; - } - - if (lib.Handle()) - return lib.Lookup(ordinal); - else - return reinterpret_cast<TLibraryFunction>(NULL); - } - -private: - void init() - { - _LIT(KLibName_3_1, "qts60plugin_3_1" QT_LIBINFIX_UNICODE L".dll"); - _LIT(KLibName_3_2, "qts60plugin_3_2" QT_LIBINFIX_UNICODE L".dll"); - _LIT(KLibName_5_0, "qts60plugin_5_0" QT_LIBINFIX_UNICODE L".dll"); - - TPtrC libName; - TInt uidValue; - switch (QSysInfo::s60Version()) { - case QSysInfo::SV_S60_3_1: - libName.Set(KLibName_3_1); - uidValue = 0x2001E620; - break; - case QSysInfo::SV_S60_3_2: - libName.Set(KLibName_3_2); - uidValue = 0x2001E621; - break; - case QSysInfo::SV_S60_5_0: // Fall through to default - default: - // Default to 5.0 version, as any unknown platform is likely to be newer than that - libName.Set(KLibName_5_0); - uidValue = 0x2001E622; - break; - } - - TUidType libUid(KDynamicLibraryUid, KSharedLibraryUid, TUid::Uid(uidValue)); - lib.Load(libName, libUid); - - // Duplicate lib handle to enable process wide access to it. Since Duplicate overwrites - // existing handle without closing it, store original for subsequent closing. - RLibrary origHandleCloser = lib; - lib.Duplicate(RThread(), EOwnerProcess); - origHandleCloser.Close(); - } - - RLibrary lib; - bool initTried; -}; - -Q_GLOBAL_STATIC(QS60PluginResolver, qt_s60_plugin_resolver); - -/*! - \internal - Resolves a platform version specific function from S60 plugin. - If plugin is missing or resolving fails for another reason, NULL is returned. -*/ -Q_CORE_EXPORT TLibraryFunction qt_resolveS60PluginFunc(int ordinal) -{ - return qt_s60_plugin_resolver()->resolve(ordinal); -} - class QS60RFsSession { public: diff --git a/src/corelib/kernel/qcore_symbian_p.h b/src/corelib/kernel/qcore_symbian_p.h index 84c6fed..3019e05 100644 --- a/src/corelib/kernel/qcore_symbian_p.h +++ b/src/corelib/kernel/qcore_symbian_p.h @@ -142,19 +142,6 @@ inline uint qHash(TUid uid) return qHash(uid.iUid); } -// S60 version specific function ordinals that can be resolved -enum S60PluginFuncOrdinals -{ - S60Plugin_TimeFormatL = 1, - S60Plugin_GetTimeFormatSpec = 2, - S60Plugin_GetLongDateFormatSpec = 3, - S60Plugin_GetShortDateFormatSpec = 4, - S60Plugin_LocalizedDirectoryName = 5, - S60Plugin_GetSystemDrive = 6 -}; - -Q_CORE_EXPORT TLibraryFunction qt_resolveS60PluginFunc(int ordinal); - Q_CORE_EXPORT RFs& qt_s60GetRFs(); Q_CORE_EXPORT RSocketServ& qt_symbianGetSocketServer(); diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 750204f..7694a0f 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -116,8 +116,6 @@ private: }; #ifdef Q_OS_SYMBIAN -typedef TDriveNumber (*SystemDriveFunc)(RFs&); -static SystemDriveFunc PtrGetSystemDrive = 0; static CApaCommandLine* apaCommandLine = 0; static char *apaTail = 0; static QVector<char *> *apaArgv = 0; @@ -1950,10 +1948,7 @@ QString QCoreApplication::applicationDirPath() } if (err != KErrNone || (driveInfo.iDriveAtt & KDriveAttRom) || (driveInfo.iMediaAtt & KMediaAttWriteProtected)) { - if(!PtrGetSystemDrive) - PtrGetSystemDrive = reinterpret_cast<SystemDriveFunc>(qt_resolveS60PluginFunc(S60Plugin_GetSystemDrive)); - Q_ASSERT(PtrGetSystemDrive); - drive = PtrGetSystemDrive(fs); + drive = fs.GetSystemDrive(); fs.DriveToChar(drive, driveChar); } diff --git a/src/corelib/kernel/qeventdispatcher_symbian.cpp b/src/corelib/kernel/qeventdispatcher_symbian.cpp index 1d7ecce..b216075 100644 --- a/src/corelib/kernel/qeventdispatcher_symbian.cpp +++ b/src/corelib/kernel/qeventdispatcher_symbian.cpp @@ -59,8 +59,6 @@ QT_BEGIN_NAMESPACE #define WAKE_UP_PRIORITY CActive::EPriorityStandard #define TIMER_PRIORITY CActive::EPriorityHigh -#define NULLTIMER_PRIORITY CActive::EPriorityLow -#define COMPLETE_DEFERRED_ACTIVE_OBJECTS_PRIORITY CActive::EPriorityIdle static inline int qt_pipe_write(int socket, const char *data, qint64 len) { @@ -124,57 +122,25 @@ private: }; /* - * This class is designed to aid in implementing event handling in a more round robin fashion. We - * cannot change active objects that we do not own, but the active objects that Qt owns will use - * this as a base class with convenience functions. - * - * Here is how it works: On every RunL, the deriving class should call maybeQueueForLater(). - * This will return whether the active object has been queued, or whether it should run immediately. - * Queued objects will run again after other events have been processed. - * - * The QCompleteDeferredAOs class is a special object that runs after all others, which will - * reactivate the objects that were previously not run. + * This class can be used as a base class for Qt active objects. + * Socket active objects can use it to defer their activity. */ QActiveObject::QActiveObject(TInt priority, QEventDispatcherSymbian *dispatcher) : CActive(priority), - m_dispatcher(dispatcher), - m_hasAlreadyRun(false), - m_hasRunAgain(false), - m_iterationCount(1) + m_dispatcher(dispatcher) { } QActiveObject::~QActiveObject() { - if (m_hasRunAgain) - m_dispatcher->removeDeferredActiveObject(this); -} - -bool QActiveObject::maybeQueueForLater() -{ - Q_ASSERT(!m_hasRunAgain); - - if (!m_hasAlreadyRun || m_dispatcher->iterationCount() != m_iterationCount) { - // First occurrence of this event in this iteration. - m_hasAlreadyRun = true; - m_iterationCount = m_dispatcher->iterationCount(); - return false; - } else { - // The event has already occurred. - m_dispatcher->addDeferredActiveObject(this); - m_hasRunAgain = true; - return true; - } } bool QActiveObject::maybeDeferSocketEvent() { - Q_ASSERT(!m_hasRunAgain); Q_ASSERT(m_dispatcher); if (!m_dispatcher->areSocketEventsBlocked()) { return false; } - m_hasRunAgain = true; m_dispatcher->addDeferredSocketActiveObject(this); return true; } @@ -186,13 +152,11 @@ void QActiveObject::reactivateAndComplete() SetActive(); TRequestStatus *status = &iStatus; QEventDispatcherSymbian::RequestComplete(status, error); - - m_hasRunAgain = false; - m_hasAlreadyRun = false; } QWakeUpActiveObject::QWakeUpActiveObject(QEventDispatcherSymbian *dispatcher) - : QActiveObject(WAKE_UP_PRIORITY, dispatcher) + : CActive(WAKE_UP_PRIORITY), + m_dispatcher(dispatcher) { m_hostThreadId = RThread().Id(); CActiveScheduler::Add(this); @@ -224,17 +188,14 @@ void QWakeUpActiveObject::DoCancel() void QWakeUpActiveObject::RunL() { - if (maybeQueueForLater()) - return; - iStatus = KRequestPending; SetActive(); QT_TRYCATCH_LEAVING(m_dispatcher->wakeUpWasCalled()); } QTimerActiveObject::QTimerActiveObject(QEventDispatcherSymbian *dispatcher, SymbianTimerInfo *timerInfo) - : QActiveObject((timerInfo->interval) ? TIMER_PRIORITY : NULLTIMER_PRIORITY , dispatcher), - m_timerInfo(timerInfo), m_expectedTimeSinceLastEvent(0) + : CActive(TIMER_PRIORITY), + m_dispatcher(dispatcher), m_timerInfo(timerInfo), m_expectedTimeSinceLastEvent(0) { // start the timeout timer to ensure initialisation m_timeoutTimer.start(); @@ -311,9 +272,6 @@ void QTimerActiveObject::Run() return; } - if (maybeQueueForLater()) - return; - if (m_timerInfo->interval > 0) { // Start a new timer immediately so that we don't lose time. m_timerInfo->msLeft = m_timerInfo->interval; @@ -365,44 +323,6 @@ SymbianTimerInfo::~SymbianTimerInfo() delete timerAO; } -QCompleteDeferredAOs::QCompleteDeferredAOs(QEventDispatcherSymbian *dispatcher) - : CActive(COMPLETE_DEFERRED_ACTIVE_OBJECTS_PRIORITY), - m_dispatcher(dispatcher) -{ - CActiveScheduler::Add(this); - iStatus = KRequestPending; - SetActive(); -} - -QCompleteDeferredAOs::~QCompleteDeferredAOs() -{ - Cancel(); -} - -void QCompleteDeferredAOs::complete() -{ - if (iStatus.Int() == KRequestPending) { - TRequestStatus *status = &iStatus; - QEventDispatcherSymbian::RequestComplete(status, KErrNone); - } -} - -void QCompleteDeferredAOs::DoCancel() -{ - if (iStatus.Int() == KRequestPending) { - TRequestStatus *status = &iStatus; - QEventDispatcherSymbian::RequestComplete(status, KErrNone); - } -} - -void QCompleteDeferredAOs::RunL() -{ - iStatus = KRequestPending; - SetActive(); - - QT_TRYCATCH_LEAVING(m_dispatcher->reactivateDeferredActiveObjects()); -} - QSelectThread::QSelectThread() : m_quit(false) { @@ -677,8 +597,6 @@ void QSocketActiveObject::RunL() { if (maybeDeferSocketEvent()) return; - if (maybeQueueForLater()) - return; QT_TRYCATCH_LEAVING(run()); } @@ -708,6 +626,138 @@ void QSocketActiveObject::deleteLater() } } +/* Round robin active object scheduling for Qt apps. + * + * Qt and Symbian have different views on how events should be handled. Qt expects + * round-robin event processing, whereas Symbian implements a strict priority based + * system. + * + * This scheduler class, and its use in QEventDispatcherSymbian::processEvents, + * introduces round robin scheduling for high priority active objects, but leaves + * those with low priorities scheduled in priority order. + * The algorithm used is that, during each call to processEvents, any pre-existing + * runnable active object may run, but only once. Active objects with priority + * lower than EPriorityStandard can only run if no higher priority active object + * has run. + * This is done by implementing an alternative scheduling algorithm which requires + * access to the internal members of the active object system. The iSpare member of + * CActive is replaced with a flag indicating that the object is new (CBase zero + * initialization sets this), or not run, or ran. Only active objects with the + * not run flag are allowed to run. + */ +class QtRRActiveScheduler +{ +public: + static void MarkReadyToRun(); + enum RunResult { + NothingFound, + ObjectRun, + ObjectDelayed + }; + static RunResult RunMarkedIfReady(TInt &runPriority, TInt minimumPriority); + static bool UseRRActiveScheduler(); + +private: + // active scheduler access kit, for gaining access to the internals of active objects for + // alternative active scheduler implementations. + class TRequestStatusAccess + { + public: + enum { ERequestActiveFlags = 3 }; // TRequestStatus::EActive | TRequestStatus::ERequestPending + TInt iStatus; + TUint iFlags; + }; + + class CActiveDataAccess : public CBase + { + public: + TRequestStatusAccess iStatus; + TPriQueLink iLink; + enum TMarks + { + ENewObject, // CBase zero initialization sets this, new objects cannot be run in the processEvents in which they are created + ENotRun, // This object has not yet run in the current processEvents call + ERan // This object has run in the current processEvents call + }; + int iMark; //TAny* iSpare; + }; + + class CActiveFuncAccess : public CActive + { + public: + // these functions are needed in RunMarkedIfReady + using CActive::RunL; + using CActive::RunError; + }; + + class CActiveSchedulerAccess : public CBase + { + public: + using CBase::Extension_; + struct TLoop; + TLoop* iStack; + TPriQue<CActiveFuncAccess> iActiveQ; + TAny* iSpare; + }; +}; + +void QtRRActiveScheduler::MarkReadyToRun() +{ + CActiveScheduler *pS=CActiveScheduler::Current(); + if (pS!=NULL) + { + TDblQueIter<CActive> iterator(((CActiveSchedulerAccess*)pS)->iActiveQ); + for (CActive* active=iterator++; active!=NULL; active=iterator++) { + ((CActiveDataAccess*)active)->iMark = CActiveDataAccess::ENotRun; + } + } +} + +QtRRActiveScheduler::RunResult QtRRActiveScheduler::RunMarkedIfReady(TInt &runPriority, TInt minimumPriority) +{ + RunResult result = NothingFound; + TInt error=KErrNone; + CActiveScheduler *pS=CActiveScheduler::Current(); + if (pS!=NULL) { + TDblQueIter<CActiveFuncAccess> iterator(((CActiveSchedulerAccess*)pS)->iActiveQ); + for (CActiveFuncAccess *active=iterator++; active!=NULL; active=iterator++) { + CActiveDataAccess *dataAccess = (CActiveDataAccess*)active; + if (active->IsActive() && (active->iStatus!=KRequestPending)) { + int& mark = dataAccess->iMark; + if (mark == CActiveDataAccess::ENotRun && active->Priority()>=minimumPriority) { + mark = CActiveDataAccess::ERan; + runPriority = active->Priority(); + dataAccess->iStatus.iFlags&=~TRequestStatusAccess::ERequestActiveFlags; + int vptr = *(int*)active; // vptr can be used to identify type when debugging leaves + TRAP(error, active->RunL()); + if (error!=KErrNone) + error=active->RunError(error); + if (error) { + qWarning("Active object (ptr=0x%08x, vptr=0x%08x) leave: %i\n", active, vptr, error); + pS->Error(error); + } + return ObjectRun; + } + result = ObjectDelayed; + } + } + } + return result; +} + +bool QtRRActiveScheduler::UseRRActiveScheduler() +{ + // This code allows euser to declare incompatible active object / scheduler internal data structures + // in the future, disabling Qt's round robin scheduler use. + // By default the Extension_ function will set the second argument to NULL. We therefore use NULL to indicate + // that the data structures are compatible with before when this protocol was recognised. + // The extension id used is QtCore's UID. + CActiveSchedulerAccess *access = (CActiveSchedulerAccess *)CActiveScheduler::Current(); + TAny* schedulerCompatibilityNumber; + access->Extension_(0x2001B2DC, schedulerCompatibilityNumber, NULL); + return schedulerCompatibilityNumber == NULL; +} + #ifdef QT_SYMBIAN_PRIORITY_DROP class QIdleDetectorThread { @@ -811,7 +861,6 @@ QEventDispatcherSymbian::QEventDispatcherSymbian(QObject *parent) m_selectThread(0), m_activeScheduler(0), m_wakeUpAO(0), - m_completeDeferredAOs(0), m_interrupt(false), m_wakeUpDone(0), m_iterationCount(0), @@ -836,7 +885,6 @@ void QEventDispatcherSymbian::startingUp() CActiveScheduler::Install(m_activeScheduler); } m_wakeUpAO = q_check_ptr(new QWakeUpActiveObject(this)); - m_completeDeferredAOs = q_check_ptr(new QCompleteDeferredAOs(this)); // We already might have posted events, wakeup once to process them wakeUp(); } @@ -855,7 +903,6 @@ void QEventDispatcherSymbian::closingDown() delete m_selectThread; m_selectThread = 0; - delete m_completeDeferredAOs; delete m_wakeUpAO; if (m_activeScheduler) { delete m_activeScheduler; @@ -864,6 +911,7 @@ void QEventDispatcherSymbian::closingDown() bool QEventDispatcherSymbian::processEvents ( QEventLoop::ProcessEventsFlags flags ) { + const bool useRRScheduler = QtRRActiveScheduler::UseRRActiveScheduler(); bool handledAnyEvent = false; bool oldNoSocketEventsValue = m_noSocketEvents; bool oldInsideTimerEventValue = m_insideTimerEvent; @@ -894,8 +942,9 @@ bool QEventDispatcherSymbian::processEvents ( QEventLoop::ProcessEventsFlags fla handledAnyEvent = sendDeferredSocketEvents(); } - bool handledSymbianEvent = false; + QtRRActiveScheduler::RunResult handledSymbianEvent = QtRRActiveScheduler::NothingFound; m_interrupt = false; + int minPriority = KMinTInt; #ifdef QT_SYMBIAN_PRIORITY_DROP QElapsedTimer eventTimer; @@ -921,6 +970,10 @@ bool QEventDispatcherSymbian::processEvents ( QEventLoop::ProcessEventsFlags fla CActiveScheduler::Current()->WaitForAnyRequest(); } + if (useRRScheduler && handledSymbianEvent == QtRRActiveScheduler::NothingFound) { + QtRRActiveScheduler::MarkReadyToRun(); + } + #ifdef QT_SYMBIAN_PRIORITY_DROP if (idleDetectorThread()->hasRun()) { if (m_delay > baseDelay) @@ -936,11 +989,31 @@ bool QEventDispatcherSymbian::processEvents ( QEventLoop::ProcessEventsFlags fla eventTimer.start(); #endif - TInt error; - handledSymbianEvent = CActiveScheduler::RunIfReady(error, KMinTInt); - if (error) { - qWarning("CActiveScheduler::RunIfReady() returned error: %i\n", error); - CActiveScheduler::Current()->Error(error); + if (useRRScheduler) { + // Standard or above priority AOs are scheduled round robin. + // Lower priority AOs can only run if nothing higher priority has run. + int runPriority = minPriority; + handledSymbianEvent = QtRRActiveScheduler::RunMarkedIfReady(runPriority, minPriority); + minPriority = qMin(runPriority, int(CActive::EPriorityStandard)); + } else { + TInt error; + handledSymbianEvent = + CActiveScheduler::RunIfReady(error, minPriority) + ? QtRRActiveScheduler::ObjectRun + : QtRRActiveScheduler::NothingFound; + if (error) { + qWarning("CActiveScheduler::RunIfReady() returned error: %i\n", error); + CActiveScheduler::Current()->Error(error); + } + } + + if (handledSymbianEvent == QtRRActiveScheduler::NothingFound) { + // no runnable or delayed active object was found, the signal that caused us to get here must be bad + qFatal("QEventDispatcherSymbian::processEvents(): Caught Symbian stray signal"); + } else if (handledSymbianEvent == QtRRActiveScheduler::ObjectDelayed) { + // signal the thread to compensate for the un-handled signal absorbed + RThread().RequestSignal(); + break; } #ifdef QT_SYMBIAN_PRIORITY_DROP @@ -949,10 +1022,8 @@ bool QEventDispatcherSymbian::processEvents ( QEventLoop::ProcessEventsFlags fla m_avgEventTime = (m_avgEventTime * 95 + eventDur * 5) / 100; #endif - if (!handledSymbianEvent) { - qFatal("QEventDispatcherSymbian::processEvents(): Caught Symbian stray signal"); - } handledAnyEvent = true; + if (m_interrupt) { break; } @@ -1051,41 +1122,11 @@ bool QEventDispatcherSymbian::sendPostedEvents() //return false; } -inline void QEventDispatcherSymbian::addDeferredActiveObject(QActiveObject *object) -{ - queueDeferredActiveObjectsCompletion(); - m_deferredActiveObjects.append(object); -} - -inline void QEventDispatcherSymbian::removeDeferredActiveObject(QActiveObject *object) -{ - m_deferredActiveObjects.removeAll(object); - m_deferredSocketEvents.removeAll(object); -} - inline void QEventDispatcherSymbian::addDeferredSocketActiveObject(QActiveObject *object) { m_deferredSocketEvents.append(object); } -void QEventDispatcherSymbian::queueDeferredActiveObjectsCompletion() -{ - m_completeDeferredAOs->complete(); -} - -void QEventDispatcherSymbian::reactivateDeferredActiveObjects() -{ - while (!m_deferredActiveObjects.isEmpty()) { - QActiveObject *object = m_deferredActiveObjects.takeFirst(); - object->reactivateAndComplete(); - } - - // We do this because we want to return from processEvents. This is because - // each invocation of processEvents should only run each active object once. - // The active scheduler should run them continously, however. - m_interrupt = true; -} - bool QEventDispatcherSymbian::sendDeferredSocketEvents() { bool sentAnyEvents = false; @@ -1161,14 +1202,6 @@ void QEventDispatcherSymbian::registerTimer ( int timerId, int interval, QObject m_timerList.insert(timerId, timer); timer->timerAO->Start(); - - if (m_insideTimerEvent) - // If we are inside a timer event, we need to prevent event starvation - // by preventing newly created timers from running in the same event processing - // iteration. Do this by calling the maybeQueueForLater() function to "fake" that we have - // already run once. This will cause the next run to be added to the deferred - // queue instead. - timer->timerAO->maybeQueueForLater(); } bool QEventDispatcherSymbian::unregisterTimer ( int timerId ) @@ -1231,86 +1264,6 @@ void CQtActiveScheduler::Error(TInt aError) const QT_CATCH (const std::bad_alloc&) {} // ignore alloc fails, nothing more can be done } -bool QActiveObject::wait(CActive* ao, int ms) -{ - if (!ao->IsActive()) - return true; //request already complete - bool timedout = false; - if (ms > 0) { - TRequestStatus tstat; - RTimer t; - if (KErrNone != t.CreateLocal()) - return false; - t.HighRes(tstat, ms*1000); - User::WaitForRequest(tstat, ao->iStatus); - if (tstat != KRequestPending) { - timedout = true; - } else { - t.Cancel(); - //balance thread semaphore - User::WaitForRequest(tstat); - } - t.Close(); - } else { - User::WaitForRequest(ao->iStatus); - } - if (timedout) - return false; - - //evil cast to allow calling of protected virtual - ((QActiveObject*)ao)->RunL(); - - //clear active & pending flags - ao->iStatus = TRequestStatus(); - - return true; -} - -bool QActiveObject::wait(QList<CActive*> aos, int ms) -{ - QVector<TRequestStatus*> stati; - stati.reserve(aos.count() + 1); - foreach (CActive* ao, aos) { - if (!ao->IsActive()) - return true; //request already complete - stati.append(&(ao->iStatus)); - } - bool timedout = false; - TRequestStatus tstat; - RTimer t; - if (ms > 0) { - if (KErrNone != t.CreateLocal()) - return false; - t.HighRes(tstat, ms*1000); - stati.append(&tstat); - } - User::WaitForNRequest(stati.data(), stati.count()); - if (ms > 0) { - if (tstat != KRequestPending) { - timedout = true; - } else { - t.Cancel(); - //balance thread semaphore - User::WaitForRequest(tstat); - } - t.Close(); - } - if (timedout) - return false; - - foreach (CActive* ao, aos) { - if (ao->iStatus != KRequestPending) { - //evil cast to allow calling of protected virtual - ((QActiveObject*)ao)->RunL(); - - //clear active & pending flags - ao->iStatus = TRequestStatus(); - break; //only call one - } - } - return true; -} - QT_END_NAMESPACE #include "moc_qeventdispatcher_symbian_p.cpp" diff --git a/src/corelib/kernel/qeventdispatcher_symbian_p.h b/src/corelib/kernel/qeventdispatcher_symbian_p.h index e327a9c..1b81599 100644 --- a/src/corelib/kernel/qeventdispatcher_symbian_p.h +++ b/src/corelib/kernel/qeventdispatcher_symbian_p.h @@ -83,23 +83,14 @@ public: QActiveObject(TInt priority, QEventDispatcherSymbian *dispatcher); ~QActiveObject(); - bool maybeQueueForLater(); bool maybeDeferSocketEvent(); void reactivateAndComplete(); - - static bool wait(CActive* ao, int ms); - static bool wait(QList<CActive*> aos, int ms); protected: QEventDispatcherSymbian *m_dispatcher; - -private: - bool m_hasAlreadyRun : 1; - bool m_hasRunAgain : 1; - int m_iterationCount; }; -class QWakeUpActiveObject : public QActiveObject +class QWakeUpActiveObject : public CActive { public: QWakeUpActiveObject(QEventDispatcherSymbian *dispatcher); @@ -112,6 +103,7 @@ protected: void RunL(); private: + QEventDispatcherSymbian *m_dispatcher; TThreadId m_hostThreadId; }; @@ -132,7 +124,7 @@ struct SymbianTimerInfo : public QSharedData typedef QExplicitlySharedDataPointer<SymbianTimerInfo> SymbianTimerInfoPtr; // This is a bit of a proxy class. See comments in SetActive and Start for details. -class QTimerActiveObject : public QActiveObject +class QTimerActiveObject : public CActive { public: QTimerActiveObject(QEventDispatcherSymbian *dispatcher, SymbianTimerInfo *timerInfo); @@ -149,28 +141,13 @@ private: void StartTimer(); private: + QEventDispatcherSymbian *m_dispatcher; SymbianTimerInfo *m_timerInfo; QElapsedTimer m_timeoutTimer; int m_expectedTimeSinceLastEvent; RTimer m_rTimer; }; -class QCompleteDeferredAOs : public CActive -{ -public: - QCompleteDeferredAOs(QEventDispatcherSymbian *dispatcher); - ~QCompleteDeferredAOs(); - - void complete(); - -protected: - void DoCancel(); - void RunL(); - -private: - QEventDispatcherSymbian *m_dispatcher; -}; - class QSocketActiveObject : public QActiveObject { public: @@ -254,12 +231,6 @@ public: void wakeUpWasCalled(); void reactivateSocketNotifier(QSocketNotifier *notifier); - void addDeferredActiveObject(QActiveObject *object); - void removeDeferredActiveObject(QActiveObject *object); - void queueDeferredActiveObjectsCompletion(); - // Can be overridden to activate local active objects too, but do call baseclass! - virtual void reactivateDeferredActiveObjects(); - inline int iterationCount() const { return m_iterationCount; } void addDeferredSocketActiveObject(QActiveObject *object); @@ -282,7 +253,6 @@ private: QHash<QSocketNotifier *, QSocketActiveObject *> m_notifiers; QWakeUpActiveObject *m_wakeUpAO; - QCompleteDeferredAOs *m_completeDeferredAOs; volatile bool m_interrupt; QAtomicInt m_wakeUpDone; @@ -292,8 +262,6 @@ private: bool m_noSocketEvents; //deferred until socket events are enabled QList<QActiveObject *> m_deferredSocketEvents; - //deferred until idle - QList<QActiveObject *> m_deferredActiveObjects; int m_delay; int m_avgEventTime; diff --git a/src/corelib/kernel/qsharedmemory.cpp b/src/corelib/kernel/qsharedmemory.cpp index acb6044..2882279 100644 --- a/src/corelib/kernel/qsharedmemory.cpp +++ b/src/corelib/kernel/qsharedmemory.cpp @@ -247,14 +247,14 @@ void QSharedMemory::setNativeKey(const QString &key) if (isAttached()) detach(); d->cleanHandle(); - d->key = QString(); + d->key.clear(); d->nativeKey = key; } bool QSharedMemoryPrivate::initKey() { - if (!cleanHandle()) - return false; + cleanHandle(); + #ifndef QT_NO_SYSTEMSEMAPHORE systemSemaphore.setKey(QString(), 1); systemSemaphore.setKey(key, 1); @@ -285,7 +285,7 @@ bool QSharedMemoryPrivate::initKey() return false; } #endif - errorString = QString(); + errorString.clear(); error = QSharedMemory::NoError; return true; } @@ -342,28 +342,24 @@ bool QSharedMemory::create(int size, AccessMode mode) if (!d->initKey()) return false; + if (size <= 0) { + d->error = QSharedMemory::InvalidSize; + d->errorString = QSharedMemory::tr("%1: create size is less then 0").arg(QLatin1String("QSharedMemory::create")); + return false; + } + #ifndef QT_NO_SYSTEMSEMAPHORE #ifndef Q_OS_WIN // Take ownership and force set initialValue because the semaphore // might have already existed from a previous crash. d->systemSemaphore.setKey(d->key, 1, QSystemSemaphore::Create); #endif -#endif - QString function = QLatin1String("QSharedMemory::create"); -#ifndef QT_NO_SYSTEMSEMAPHORE QSharedMemoryLocker lock(this); - if (!d->key.isNull() && !d->tryLocker(&lock, function)) + if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::create"))) return false; #endif - if (size <= 0) { - d->error = QSharedMemory::InvalidSize; - d->errorString = - QSharedMemory::tr("%1: create size is less then 0").arg(function); - return false; - } - if (!d->create(size)) return false; diff --git a/src/corelib/kernel/qsharedmemory_p.h b/src/corelib/kernel/qsharedmemory_p.h index d5fafef..21b8612 100644 --- a/src/corelib/kernel/qsharedmemory_p.h +++ b/src/corelib/kernel/qsharedmemory_p.h @@ -70,12 +70,12 @@ namespace QSharedMemoryPrivate #include "private/qobject_p.h" #ifdef Q_OS_WIN -#include <qt_windows.h> +# include <qt_windows.h> #elif defined(Q_OS_SYMBIAN) -#include <e32std.h> -#include <sys/types.h> +# include <e32std.h> +# include <sys/types.h> #else -#include <sys/sem.h> +# include <sys/types.h> #endif QT_BEGIN_NAMESPACE @@ -139,7 +139,7 @@ public: key_t handle(); #endif bool initKey(); - bool cleanHandle(); + void cleanHandle(); bool create(int size); bool attach(QSharedMemory::AccessMode mode); bool detach(); @@ -151,7 +151,7 @@ public: #endif #ifndef QT_NO_SYSTEMSEMAPHORE - bool tryLocker(QSharedMemoryLocker *locker, const QString function) { + inline bool tryLocker(QSharedMemoryLocker *locker, const QString &function) { if (!locker->lock()) { errorString = QSharedMemory::tr("%1: unable to lock").arg(function); error = QSharedMemory::LockError; diff --git a/src/corelib/kernel/qsharedmemory_symbian.cpp b/src/corelib/kernel/qsharedmemory_symbian.cpp index fdd513a..cd1567a 100644 --- a/src/corelib/kernel/qsharedmemory_symbian.cpp +++ b/src/corelib/kernel/qsharedmemory_symbian.cpp @@ -41,19 +41,21 @@ #include "qsharedmemory.h" #include "qsharedmemory_p.h" -#include "qsystemsemaphore.h" + #include "qcore_symbian_p.h" #include <qdebug.h> -QT_BEGIN_NAMESPACE - #ifndef QT_NO_SHAREDMEMORY #define QSHAREDMEMORY_DEBUG -QSharedMemoryPrivate::QSharedMemoryPrivate() : QObjectPrivate(), - memory(0), size(0), error(QSharedMemory::NoError), - systemSemaphore(QString()), lockedByMe(false) +QT_BEGIN_NAMESPACE + +QSharedMemoryPrivate::QSharedMemoryPrivate() + : QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError), +#ifndef QT_NO_SYSTEMSEMAPHORE + systemSemaphore(QString()), lockedByMe(false) +#endif { } @@ -61,6 +63,7 @@ void QSharedMemoryPrivate::setErrorString(const QString &function, TInt errorCod { if (errorCode == KErrNone) return; + switch (errorCode) { case KErrAlreadyExists: error = QSharedMemory::AlreadyExists; @@ -88,40 +91,43 @@ void QSharedMemoryPrivate::setErrorString(const QString &function, TInt errorCod #if defined QSHAREDMEMORY_DEBUG qDebug() << errorString << "key" << key; #endif + break; } } key_t QSharedMemoryPrivate::handle() { + // don't allow making handles on empty keys + if (nativeKey.isEmpty()) { + error = QSharedMemory::KeyError; + errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle")); + return 0; + } + // Not really cost effective to check here if shared memory is attachable, as it requires // exactly the same call as attaching, so always assume handle is valid and return failure // from attach. return 1; } -bool QSharedMemoryPrivate::cleanHandle() +void QSharedMemoryPrivate::cleanHandle() { chunk.Close(); - return true; } bool QSharedMemoryPrivate::create(int size) { - QString function = QLatin1String("QSharedMemory::create"); - if (nativeKey.isEmpty()) { - error = QSharedMemory::KeyError; - errorString = QSharedMemory::tr("%1: key error").arg(function); + if (!handle()) return false; - } TPtrC ptr(qt_QString2TPtrC(nativeKey)); TInt err = chunk.CreateGlobal(ptr, size, size); - setErrorString(function, err); - - if (err != KErrNone) + if (err != KErrNone) { + setErrorString(QLatin1String("QSharedMemory::create"), err); return false; + } // Zero out the created chunk Mem::FillZ(chunk.Base(), chunk.Size()); @@ -133,12 +139,8 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode /* mode */) { // Grab a pointer to the memory block if (!chunk.Handle()) { - QString function = QLatin1String("QSharedMemory::handle"); - if (nativeKey.isEmpty()) { - error = QSharedMemory::KeyError; - errorString = QSharedMemory::tr("%1: unable to make key").arg(function); + if (!handle()) return false; - } TPtrC ptr(qt_QString2TPtrC(nativeKey)); @@ -147,7 +149,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode /* mode */) err = chunk.OpenGlobal(ptr, false); if (err != KErrNone) { - setErrorString(function, err); + setErrorString(QLatin1String("QSharedMemory::attach"), err); return false; } } @@ -168,6 +170,6 @@ bool QSharedMemoryPrivate::detach() return true; } -#endif //QT_NO_SHAREDMEMORY - QT_END_NAMESPACE + +#endif //QT_NO_SHAREDMEMORY diff --git a/src/corelib/kernel/qsharedmemory_unix.cpp b/src/corelib/kernel/qsharedmemory_unix.cpp index 2bbda49..e991ce9 100644 --- a/src/corelib/kernel/qsharedmemory_unix.cpp +++ b/src/corelib/kernel/qsharedmemory_unix.cpp @@ -43,25 +43,26 @@ #include "qsharedmemory.h" #include "qsharedmemory_p.h" -#include "qsystemsemaphore.h" -#include <qdir.h> -#include <qdebug.h> -#include <errno.h> +#include <qdebug.h> +#include <qfile.h> #ifndef QT_NO_SHAREDMEMORY #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> -#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> -#endif //QT_NO_SHAREDMEMORY +#endif // QT_NO_SHAREDMEMORY +#include <errno.h> #include "private/qcore_unix_p.h" #ifndef QT_NO_SHAREDMEMORY + +//#define QSHAREDMEMORY_DEBUG + QT_BEGIN_NAMESPACE QSharedMemoryPrivate::QSharedMemoryPrivate() @@ -78,6 +79,7 @@ void QSharedMemoryPrivate::setErrorString(const QString &function) // EINVAL is handled in functions so they can give better error strings switch (errno) { case EACCES: + case EPERM: errorString = QSharedMemory::tr("%1: permission denied").arg(function); error = QSharedMemory::PermissionDenied; break; @@ -98,9 +100,10 @@ void QSharedMemoryPrivate::setErrorString(const QString &function) default: errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg(errno); error = QSharedMemory::UnknownError; -#if defined QSHAREDMEMORY_DEBUG +#ifdef QSHAREDMEMORY_DEBUG qDebug() << errorString << "key" << key << "errno" << errno << EINVAL; #endif + break; } } @@ -117,21 +120,21 @@ key_t QSharedMemoryPrivate::handle() // don't allow making handles on empty keys if (nativeKey.isEmpty()) { - errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle:")); + errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle")); error = QSharedMemory::KeyError; return 0; } // ftok requires that an actual file exists somewhere if (!QFile::exists(nativeKey)) { - errorString = QSharedMemory::tr("%1: UNIX key file doesn't exist").arg(QLatin1String("QSharedMemory::handle:")); + errorString = QSharedMemory::tr("%1: UNIX key file doesn't exist").arg(QLatin1String("QSharedMemory::handle")); error = QSharedMemory::NotFound; return 0; } unix_key = ftok(QFile::encodeName(nativeKey).constData(), 'Q'); if (-1 == unix_key) { - errorString = QSharedMemory::tr("%1: ftok failed").arg(QLatin1String("QSharedMemory::handle:")); + errorString = QSharedMemory::tr("%1: ftok failed").arg(QLatin1String("QSharedMemory::handle")); error = QSharedMemory::KeyError; unix_key = 0; } @@ -149,20 +152,20 @@ key_t QSharedMemoryPrivate::handle() -1 error 0 already existed 1 created - */ +*/ int QSharedMemoryPrivate::createUnixKeyFile(const QString &fileName) { if (QFile::exists(fileName)) return 0; int fd = qt_safe_open(QFile::encodeName(fileName).constData(), - O_EXCL | O_CREAT | O_RDWR, 0640); + O_EXCL | O_CREAT | O_RDWR, 0640); if (-1 == fd) { if (errno == EEXIST) return 0; return -1; } else { - close(fd); + qt_safe_close(fd); } return 1; } @@ -170,25 +173,21 @@ int QSharedMemoryPrivate::createUnixKeyFile(const QString &fileName) #ifndef QT_NO_SHAREDMEMORY -bool QSharedMemoryPrivate::cleanHandle() +void QSharedMemoryPrivate::cleanHandle() { unix_key = 0; - return true; } bool QSharedMemoryPrivate::create(int size) { // build file if needed - bool createdFile = false; int built = createUnixKeyFile(nativeKey); if (built == -1) { - errorString = QSharedMemory::tr("%1: unable to make key").arg(QLatin1String("QSharedMemory::handle:")); + errorString = QSharedMemory::tr("%1: unable to make key").arg(QLatin1String("QSharedMemory::create")); error = QSharedMemory::KeyError; return false; } - if (built == 1) { - createdFile = true; - } + bool createdFile = built == 1; // get handle if (!handle()) { @@ -202,7 +201,7 @@ bool QSharedMemoryPrivate::create(int size) QString function = QLatin1String("QSharedMemory::create"); switch (errno) { case EINVAL: - errorString = QSharedMemory::tr("%1: system-imposed size restrictions").arg(QLatin1String("QSharedMemory::handle")); + errorString = QSharedMemory::tr("%1: system-imposed size restrictions").arg(function); error = QSharedMemory::InvalidSize; break; default: @@ -280,7 +279,7 @@ bool QSharedMemoryPrivate::detach() if (shmid_ds.shm_nattch == 0) { // mark for removal if (-1 == shmctl(id, IPC_RMID, &shmid_ds)) { - setErrorString(QLatin1String("QSharedMemory::remove")); + setErrorString(QLatin1String("QSharedMemory::detach")); switch (errno) { case EINVAL: return true; @@ -296,7 +295,6 @@ bool QSharedMemoryPrivate::detach() return true; } - QT_END_NAMESPACE #endif // QT_NO_SHAREDMEMORY diff --git a/src/corelib/kernel/qsharedmemory_win.cpp b/src/corelib/kernel/qsharedmemory_win.cpp index 3cc14fe..567214e 100644 --- a/src/corelib/kernel/qsharedmemory_win.cpp +++ b/src/corelib/kernel/qsharedmemory_win.cpp @@ -41,24 +41,30 @@ #include "qsharedmemory.h" #include "qsharedmemory_p.h" -#include "qsystemsemaphore.h" -#include <qdebug.h> -QT_BEGIN_NAMESPACE +#include <qdebug.h> #ifndef QT_NO_SHAREDMEMORY -QSharedMemoryPrivate::QSharedMemoryPrivate() : QObjectPrivate(), - memory(0), size(0), error(QSharedMemory::NoError), - systemSemaphore(QString()), lockedByMe(false), hand(0) +//#define QSHAREDMEMORY_DEBUG + +QT_BEGIN_NAMESPACE + +QSharedMemoryPrivate::QSharedMemoryPrivate() + : QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError), +#ifndef QT_NO_SYSTEMSEMAPHORE + systemSemaphore(QString()), lockedByMe(false), +#endif + hand(0) { } void QSharedMemoryPrivate::setErrorString(const QString &function) { - BOOL windowsError = GetLastError(); + DWORD windowsError = GetLastError(); if (windowsError == 0) return; + switch (windowsError) { case ERROR_ALREADY_EXISTS: error = QSharedMemory::AlreadyExists; @@ -89,19 +95,20 @@ void QSharedMemoryPrivate::setErrorString(const QString &function) default: errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg(windowsError); error = QSharedMemory::UnknownError; -#if defined QSHAREDMEMORY_DEBUG +#ifdef QSHAREDMEMORY_DEBUG qDebug() << errorString << "key" << key; #endif + break; } } HANDLE QSharedMemoryPrivate::handle() { if (!hand) { - QString function = QLatin1String("QSharedMemory::handle"); + // don't allow making handles on empty keys if (nativeKey.isEmpty()) { error = QSharedMemory::KeyError; - errorString = QSharedMemory::tr("%1: unable to make key").arg(function); + errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle")); return false; } #ifndef Q_OS_WINCE @@ -111,43 +118,34 @@ HANDLE QSharedMemoryPrivate::handle() // attach as it seems. hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0, (wchar_t*)nativeKey.utf16()); #endif - if (!hand) { - setErrorString(function); - return false; - } + if (!hand) + setErrorString(QLatin1String("QSharedMemory::handle")); } + return hand; } -bool QSharedMemoryPrivate::cleanHandle() +void QSharedMemoryPrivate::cleanHandle() { - if (hand != 0 && !CloseHandle(hand)) { - hand = 0; + if (hand != 0 && !CloseHandle(hand)) setErrorString(QLatin1String("QSharedMemory::cleanHandle")); - return false; - } hand = 0; - return true; } bool QSharedMemoryPrivate::create(int size) { - QString function = QLatin1String("QSharedMemory::create"); if (nativeKey.isEmpty()) { error = QSharedMemory::KeyError; - errorString = QSharedMemory::tr("%1: key error").arg(function); + errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::create")); return false; } // Create the file mapping. hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, (wchar_t*)nativeKey.utf16()); - setErrorString(function); + setErrorString(QLatin1String("QSharedMemory::create")); // hand is valid when it already exists unlike unix so explicitly check - if (error == QSharedMemory::AlreadyExists || !hand) - return false; - - return true; + return !(error == QSharedMemory::AlreadyExists || !hand); } bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode) @@ -167,7 +165,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode) // Windows doesn't set an error code on this one, // it should only be a kernel memory error. error = QSharedMemory::UnknownError; - errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach: ")); + errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach")); return false; } size = info.RegionSize; @@ -186,10 +184,11 @@ bool QSharedMemoryPrivate::detach() size = 0; // close handle - return cleanHandle(); -} - -#endif //QT_NO_SHAREDMEMORY + cleanHandle(); + return true; +} QT_END_NAMESPACE + +#endif // QT_NO_SHAREDMEMORY diff --git a/src/corelib/kernel/qsystemsemaphore_p.h b/src/corelib/kernel/qsystemsemaphore_p.h index d4e86e8..3e5f737 100644 --- a/src/corelib/kernel/qsystemsemaphore_p.h +++ b/src/corelib/kernel/qsystemsemaphore_p.h @@ -59,7 +59,7 @@ #include "qsharedmemory_p.h" #ifndef Q_OS_WINCE -# include <sys/types.h> +# include <sys/types.h> #endif #ifdef Q_OS_SYMBIAN @@ -70,11 +70,10 @@ QT_BEGIN_NAMESPACE class QSystemSemaphorePrivate { - public: QSystemSemaphorePrivate(); - QString makeKeyFileName() + inline QString makeKeyFileName() const { return QSharedMemoryPrivate::makePlatformSafeKey(key, QLatin1String("qipc_systemsem_")); } @@ -101,10 +100,10 @@ public: #elif defined(Q_OS_SYMBIAN) RSemaphore semaphore; #else + key_t unix_key; int semaphore; bool createdFile; bool createdSemaphore; - key_t unix_key; #endif QString errorString; QSystemSemaphore::SystemSemaphoreError error; @@ -115,4 +114,3 @@ QT_END_NAMESPACE #endif // QT_NO_SYSTEMSEMAPHORE #endif // QSYSTEMSEMAPHORE_P_H - diff --git a/src/corelib/kernel/qsystemsemaphore_symbian.cpp b/src/corelib/kernel/qsystemsemaphore_symbian.cpp index 96c19af..bb60df7 100644 --- a/src/corelib/kernel/qsystemsemaphore_symbian.cpp +++ b/src/corelib/kernel/qsystemsemaphore_symbian.cpp @@ -46,29 +46,32 @@ #include "qcore_symbian_p.h" #include <e32cmn.h> -QT_BEGIN_NAMESPACE #ifndef QT_NO_SYSTEMSEMAPHORE +//#define QSYSTEMSEMAPHORE_DEBUG + +QT_BEGIN_NAMESPACE + QSystemSemaphorePrivate::QSystemSemaphorePrivate() : - error(QSystemSemaphore::NoError) + error(QSystemSemaphore::NoError) { } void QSystemSemaphorePrivate::setErrorString(const QString &function, int err) { - if (err == KErrNone){ + if (err == KErrNone) return; - } + switch(err){ case KErrAlreadyExists: errorString = QCoreApplication::tr("%1: already exists", "QSystemSemaphore").arg(function); error = QSystemSemaphore::AlreadyExists; - break; + break; case KErrNotFound: errorString = QCoreApplication::tr("%1: does not exist", "QSystemSemaphore").arg(function); error = QSystemSemaphore::NotFound; - break; + break; case KErrNoMemory: case KErrInUse: errorString = QCoreApplication::tr("%1: out of resources", "QSystemSemaphore").arg(function); @@ -77,22 +80,21 @@ void QSystemSemaphorePrivate::setErrorString(const QString &function, int err) case KErrPermissionDenied: errorString = QCoreApplication::tr("%1: permission denied", "QSystemSemaphore").arg(function); error = QSystemSemaphore::PermissionDenied; - break; -default: - errorString = QCoreApplication::tr("%1: unknown error %2", "QSystemSemaphore").arg(function).arg(err); - error = QSystemSemaphore::UnknownError; - } - -#if defined QSYSTEMSEMAPHORE_DEBUG + break; + default: + errorString = QCoreApplication::tr("%1: unknown error %2", "QSystemSemaphore").arg(function).arg(err); + error = QSystemSemaphore::UnknownError; +#ifdef QSYSTEMSEMAPHORE_DEBUG qDebug() << errorString << "key" << key; #endif + break; + } } int QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode) { - if (semaphore.Handle()) { + if (semaphore.Handle()) return semaphore.Handle(); - } // don't allow making handles on empty keys if (key.isEmpty()) @@ -106,12 +108,13 @@ int QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode) while (err != KErrNoMemory && err != KErrNone && tryCount-- >= 0) { err = semaphore.CreateGlobal(name, initialValue, EOwnerProcess); if (err != KErrNoMemory && err != KErrNone) - err = semaphore.OpenGlobal(name,EOwnerProcess); + err = semaphore.OpenGlobal(name, EOwnerProcess); } - if (err){ - setErrorString(QLatin1String("QSystemSemaphore::handle"),err); + if (err) { + setErrorString(QLatin1String("QSystemSemaphore::handle"), err); return 0; } + return semaphore.Handle(); } @@ -125,14 +128,14 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count) if (0 == handle()) return false; - if (count > 0) { + if (count > 0) semaphore.Signal(count); - } else { + else semaphore.Wait(); - } + return true; } -#endif //QT_NO_SYSTEMSEMAPHORE - QT_END_NAMESPACE + +#endif // QT_NO_SYSTEMSEMAPHORE diff --git a/src/corelib/kernel/qsystemsemaphore_unix.cpp b/src/corelib/kernel/qsystemsemaphore_unix.cpp index 5e533e7..704afaf 100644 --- a/src/corelib/kernel/qsystemsemaphore_unix.cpp +++ b/src/corelib/kernel/qsystemsemaphore_unix.cpp @@ -42,9 +42,9 @@ #include "qsystemsemaphore.h" #include "qsystemsemaphore_p.h" +#include <qcoreapplication.h> #include <qdebug.h> #include <qfile.h> -#include <qcoreapplication.h> #ifndef QT_NO_SYSTEMSEMAPHORE @@ -62,11 +62,13 @@ #define EIDRM EINVAL #endif +//#define QSYSTEMSEMAPHORE_DEBUG + QT_BEGIN_NAMESPACE QSystemSemaphorePrivate::QSystemSemaphorePrivate() : - semaphore(-1), createdFile(false), - createdSemaphore(false), unix_key(-1), error(QSystemSemaphore::NoError) + unix_key(-1), semaphore(-1), createdFile(false), + createdSemaphore(false), error(QSystemSemaphore::NoError) { } @@ -95,33 +97,33 @@ void QSystemSemaphorePrivate::setErrorString(const QString &function) default: errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(errno); error = QSystemSemaphore::UnknownError; -#if defined QSYSTEMSEMAPHORE_DEBUG +#ifdef QSYSTEMSEMAPHORE_DEBUG qDebug() << errorString << "key" << key << "errno" << errno << EINVAL; #endif + break; } } /*! \internal - Setup unix_key - */ + Initialise the semaphore +*/ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) { - if (key.isEmpty()){ - errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); + if (-1 != unix_key) + return unix_key; + + if (key.isEmpty()) { + errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle")); error = QSystemSemaphore::KeyError; return -1; } // ftok requires that an actual file exists somewhere - if (-1 != unix_key) - return unix_key; - - // Create the file needed for ftok int built = QSharedMemoryPrivate::createUnixKeyFile(fileName); if (-1 == built) { - errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); + errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle")); error = QSystemSemaphore::KeyError; return -1; } @@ -130,7 +132,7 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) // Get the unix key for the created file unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q'); if (-1 == unix_key) { - errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:")); + errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle")); error = QSystemSemaphore::KeyError; return -1; } @@ -145,17 +147,16 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) cleanHandle(); return -1; } + if (mode == QSystemSemaphore::Create) { + createdSemaphore = true; + createdFile = true; + } } else { createdSemaphore = true; // Force cleanup of file, it is possible that it can be left over from a crash createdFile = true; } - if (mode == QSystemSemaphore::Create) { - createdSemaphore = true; - createdFile = true; - } - // Created semaphore so initialize its value. if (createdSemaphore && initialValue >= 0) { qt_semun init_op; @@ -173,8 +174,8 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) /*! \internal - Cleanup the unix_key - */ + Clean up the semaphore +*/ void QSystemSemaphorePrivate::cleanHandle() { unix_key = -1; @@ -189,8 +190,8 @@ void QSystemSemaphorePrivate::cleanHandle() if (-1 != semaphore) { if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) { setErrorString(QLatin1String("QSystemSemaphore::cleanHandle")); -#if defined QSYSTEMSEMAPHORE_DEBUG - qDebug() << QLatin1String("QSystemSemaphore::cleanHandle semctl failed."); +#ifdef QSYSTEMSEMAPHORE_DEBUG + qDebug("QSystemSemaphore::cleanHandle semctl failed."); #endif } semaphore = -1; @@ -201,7 +202,7 @@ void QSystemSemaphorePrivate::cleanHandle() /*! \internal - */ +*/ bool QSystemSemaphorePrivate::modifySemaphore(int count) { if (-1 == handle()) @@ -223,7 +224,7 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count) return modifySemaphore(count); } setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore")); -#if defined QSYSTEMSEMAPHORE_DEBUG +#ifdef QSYSTEMSEMAPHORE_DEBUG qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL; #endif return false; @@ -232,7 +233,6 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count) return true; } - QT_END_NAMESPACE #endif // QT_NO_SYSTEMSEMAPHORE diff --git a/src/corelib/kernel/qsystemsemaphore_win.cpp b/src/corelib/kernel/qsystemsemaphore_win.cpp index 30cab7e..0e9d645 100644 --- a/src/corelib/kernel/qsystemsemaphore_win.cpp +++ b/src/corelib/kernel/qsystemsemaphore_win.cpp @@ -44,18 +44,20 @@ #include "qcoreapplication.h" #include <qdebug.h> -QT_BEGIN_NAMESPACE - #ifndef QT_NO_SYSTEMSEMAPHORE +//#define QSYSTEMSEMAPHORE_DEBUG + +QT_BEGIN_NAMESPACE + QSystemSemaphorePrivate::QSystemSemaphorePrivate() : - semaphore(0), error(QSystemSemaphore::NoError) + semaphore(0), error(QSystemSemaphore::NoError) { } void QSystemSemaphorePrivate::setErrorString(const QString &function) { - BOOL windowsError = GetLastError(); + DWORD windowsError = GetLastError(); if (windowsError == 0) return; @@ -72,9 +74,10 @@ void QSystemSemaphorePrivate::setErrorString(const QString &function) default: errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(windowsError); error = QSystemSemaphore::UnknownError; -#if defined QSYSTEMSEMAPHORE_DEBUG +#ifdef QSYSTEMSEMAPHORE_DEBUG qDebug() << errorString << "key" << key; #endif + break; } } @@ -110,7 +113,7 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count) return false; if (count > 0) { - if (0 == ReleaseSemaphore(semaphore, count, 0)) { + if (0 == ReleaseSemaphore(semaphore, count, 0)) { setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore")); #if defined QSYSTEMSEMAPHORE_DEBUG qDebug() << QLatin1String("QSystemSemaphore::modifySemaphore ReleaseSemaphore failed"); @@ -130,6 +133,6 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count) return true; } -#endif //QT_NO_SYSTEMSEMAPHORE +#endif // QT_NO_SYSTEMSEMAPHORE QT_END_NAMESPACE |