sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "openwfcthreadmanager.h" sl@0: #include "openwfcjobmanager.h" sl@0: sl@0: sl@0: COpenWfcMonitorThread::COpenWfcMonitorThread(COpenWfcJobManger& aManager): sl@0: iEndThread(EFalse), sl@0: iManager(aManager) sl@0: { sl@0: sl@0: } sl@0: sl@0: COpenWfcMonitorThread::~COpenWfcMonitorThread() sl@0: { sl@0: EndThread(); sl@0: TRequestStatus threadDies = KErrNone; sl@0: iThread.Resume(); sl@0: iThread.Logon(threadDies); sl@0: if (iThread.ExitType() == EExitPending || sl@0: threadDies != KRequestPending) sl@0: { sl@0: User::WaitForRequest(threadDies); sl@0: } sl@0: iThread.Close(); sl@0: iSemaphore.Close(); sl@0: } sl@0: sl@0: void COpenWfcMonitorThread::ConstructL(TInt aScreenNumber) sl@0: { sl@0: TBuf <255> threadManagerName; sl@0: /* sl@0: * At a given instant in time, one screen can have at most one monitor sl@0: * thread. Test code will, for a given screen, destroy and re-create sl@0: * the monitor thread. sl@0: * sl@0: * At appears that the thread, once killed off, still lingers in sl@0: * the OS for a short time. The newly created thread fails to create sl@0: * if it shares the same name as the one that was killed but still is sl@0: * lingering around. sl@0: * sl@0: * A unique name is needed. This is guaranteed by using a thread name sl@0: * which includes a microsecond counter. This counter wraps every hour, sl@0: * roughly. This is comfortably outside the race condition above. sl@0: * sl@0: * To make debugging easier, we also include the screen number in sl@0: * the thread name. sl@0: * sl@0: * Thus, the thread name is: sl@0: * WFCMonitorThread__ sl@0: */ sl@0: _LIT(KThreadName, "WFCMonitorThread_%d_%d"); sl@0: TTime now; sl@0: now.UniversalTime(); sl@0: TInt microseconds = I64LOW(now.Int64()); sl@0: threadManagerName.Format(KThreadName, aScreenNumber, microseconds); sl@0: sl@0: User::LeaveIfError(iThread.Create(threadManagerName, sl@0: static_cast(&COpenWfcMonitorThread::Main), sl@0: KDefaultStackSize, sl@0: NULL, sl@0: this, sl@0: EOwnerProcess)); sl@0: iThread.SetPriority(EPriorityNormal); sl@0: User::LeaveIfError(iSemaphore.CreateLocal(0)); sl@0: Start(); sl@0: } sl@0: sl@0: COpenWfcMonitorThread* COpenWfcMonitorThread::NewLC(TInt aNumber, COpenWfcJobManger& aManager) sl@0: { sl@0: COpenWfcMonitorThread* self = new(ELeave) COpenWfcMonitorThread(aManager); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aNumber); sl@0: return(self); sl@0: } sl@0: sl@0: COpenWfcMonitorThread* COpenWfcMonitorThread::NewL(TInt aNumber, COpenWfcJobManger& aManager) sl@0: { sl@0: COpenWfcMonitorThread* self = COpenWfcMonitorThread::NewLC(aNumber, aManager); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: TInt COpenWfcMonitorThread::Main(TAny *aSelf) sl@0: { sl@0: COpenWfcMonitorThread* self = static_cast(aSelf); sl@0: return self->Run(); sl@0: } sl@0: sl@0: void COpenWfcMonitorThread::Start() sl@0: { sl@0: iThread.Rendezvous(iThreadStatus); sl@0: iThread.Resume(); sl@0: User::WaitForRequest(iThreadStatus); sl@0: } sl@0: sl@0: void COpenWfcMonitorThread::Resume() sl@0: { sl@0: iThread.Resume(); sl@0: } sl@0: sl@0: void COpenWfcMonitorThread::EndThread() sl@0: { sl@0: iSemaphore.Signal(); sl@0: iEndThread = ETrue; sl@0: } sl@0: sl@0: void COpenWfcMonitorThread::Suspend() sl@0: { sl@0: iThread.Suspend(); sl@0: } sl@0: sl@0: void COpenWfcMonitorThread::Signal() sl@0: { sl@0: iSemaphore.Signal(); sl@0: } sl@0: sl@0: TInt COpenWfcMonitorThread::Run() sl@0: { sl@0: RThread::Rendezvous(KErrNone); sl@0: JQLOG(("** START * COpenWfcMonitorThread::Run()")); sl@0: while(!iEndThread) sl@0: { sl@0: JQLOG(("** WAIT FOR SIGNAL * CCOpenwfcMonitorThread::Run()")); sl@0: iSemaphore.Wait(); sl@0: sl@0: if (!iEndThread) sl@0: { sl@0: iManager.DoExecuteJob(); sl@0: } sl@0: } sl@0: sl@0: /* Release any use of EGL by this thread. */ sl@0: eglReleaseThread(); sl@0: sl@0: JQLOG(("** EXIT * COpenWfcMonitorThread::Run()")); sl@0: return KErrNone; sl@0: }