os/graphics/windowing/windowserverplugins/openwfc/src/openwfcthreadmanager.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/windowing/windowserverplugins/openwfc/src/openwfcthreadmanager.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,151 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "openwfcthreadmanager.h"
    1.20 +#include "openwfcjobmanager.h"
    1.21 +
    1.22 +
    1.23 +COpenWfcMonitorThread::COpenWfcMonitorThread(COpenWfcJobManger& aManager):
    1.24 +iEndThread(EFalse),
    1.25 +iManager(aManager)
    1.26 +	{
    1.27 +	
    1.28 +	}
    1.29 +
    1.30 +COpenWfcMonitorThread::~COpenWfcMonitorThread()
    1.31 +	{
    1.32 +	EndThread();
    1.33 +	TRequestStatus threadDies = KErrNone;
    1.34 +	iThread.Resume();
    1.35 +	iThread.Logon(threadDies);
    1.36 +	if (iThread.ExitType() == EExitPending || 
    1.37 +		threadDies != KRequestPending)
    1.38 +		{
    1.39 +		User::WaitForRequest(threadDies);
    1.40 +		}
    1.41 +	iThread.Close();
    1.42 +	iSemaphore.Close();
    1.43 +	}
    1.44 +
    1.45 +void COpenWfcMonitorThread::ConstructL(TInt aScreenNumber)
    1.46 +	{
    1.47 +	TBuf <255> threadManagerName;
    1.48 +	/*
    1.49 +	 * At a given instant in time, one screen can have at most one monitor
    1.50 +	 * thread.  Test code will, for a given screen, destroy and re-create
    1.51 +	 * the monitor thread.
    1.52 +	 * 
    1.53 +	 * At appears that the thread, once killed off, still lingers in
    1.54 +	 * the OS for a short time.  The newly created thread fails to create
    1.55 +	 * if it shares the same name as the one that was killed but still is
    1.56 +	 * lingering around.
    1.57 +	 * 
    1.58 +	 * A unique name is needed.  This is guaranteed by using a thread name
    1.59 +	 * which includes a microsecond counter.  This counter wraps every hour,
    1.60 +	 * roughly.  This is comfortably outside the race condition above.
    1.61 +	 * 
    1.62 +	 * To make debugging easier, we also include the screen number in
    1.63 +	 * the thread name.
    1.64 +	 * 
    1.65 +	 * Thus, the thread name is:
    1.66 +	 * WFCMonitorThread_<ScreenNumber>_<MicrosecondCounter>
    1.67 +	 */
    1.68 +	_LIT(KThreadName, "WFCMonitorThread_%d_%d");
    1.69 +	TTime now;
    1.70 +	now.UniversalTime();
    1.71 +	TInt microseconds = I64LOW(now.Int64());
    1.72 +	threadManagerName.Format(KThreadName, aScreenNumber, microseconds);
    1.73 +
    1.74 +	User::LeaveIfError(iThread.Create(threadManagerName, 
    1.75 +					   static_cast<TThreadFunction>(&COpenWfcMonitorThread::Main), 
    1.76 +			           KDefaultStackSize, 
    1.77 +			           NULL, 
    1.78 +			           this,
    1.79 +			           EOwnerProcess));	
    1.80 +	iThread.SetPriority(EPriorityNormal);
    1.81 +	User::LeaveIfError(iSemaphore.CreateLocal(0));
    1.82 +  Start();
    1.83 +	}
    1.84 +
    1.85 +COpenWfcMonitorThread* COpenWfcMonitorThread::NewLC(TInt aNumber, COpenWfcJobManger& aManager)
    1.86 +	{
    1.87 +	COpenWfcMonitorThread* self = new(ELeave) COpenWfcMonitorThread(aManager);
    1.88 +	CleanupStack::PushL(self);
    1.89 +	self->ConstructL(aNumber);
    1.90 +	return(self);
    1.91 +	}
    1.92 +
    1.93 +COpenWfcMonitorThread* COpenWfcMonitorThread::NewL(TInt aNumber, COpenWfcJobManger& aManager)
    1.94 +	{
    1.95 +	COpenWfcMonitorThread* self = COpenWfcMonitorThread::NewLC(aNumber, aManager);
    1.96 +	CleanupStack::Pop(self);
    1.97 +	return self;
    1.98 +	}
    1.99 +
   1.100 +TInt COpenWfcMonitorThread::Main(TAny *aSelf)
   1.101 +	{
   1.102 +	COpenWfcMonitorThread* self = static_cast<COpenWfcMonitorThread*>(aSelf);
   1.103 +    return self->Run();
   1.104 +	}
   1.105 +
   1.106 +void COpenWfcMonitorThread::Start()
   1.107 +	{
   1.108 +	iThread.Rendezvous(iThreadStatus);
   1.109 +	iThread.Resume();
   1.110 +	User::WaitForRequest(iThreadStatus);
   1.111 +	}
   1.112 +
   1.113 +void COpenWfcMonitorThread::Resume()
   1.114 +	{
   1.115 +	iThread.Resume();
   1.116 +	}
   1.117 +
   1.118 +void COpenWfcMonitorThread::EndThread()
   1.119 +	{
   1.120 +    iSemaphore.Signal();
   1.121 +	iEndThread = ETrue;
   1.122 +	}
   1.123 +
   1.124 +void COpenWfcMonitorThread::Suspend()
   1.125 +	{
   1.126 +	iThread.Suspend();
   1.127 +	}
   1.128 +
   1.129 +void COpenWfcMonitorThread::Signal()
   1.130 +    {
   1.131 +    iSemaphore.Signal();
   1.132 +    }
   1.133 +
   1.134 +TInt COpenWfcMonitorThread::Run()
   1.135 +	{
   1.136 +	RThread::Rendezvous(KErrNone);
   1.137 +    JQLOG(("** START * COpenWfcMonitorThread::Run()"));
   1.138 +    while(!iEndThread)
   1.139 +        {
   1.140 +        JQLOG(("** WAIT FOR SIGNAL * CCOpenwfcMonitorThread::Run()"));
   1.141 +        iSemaphore.Wait();
   1.142 +        
   1.143 +        if (!iEndThread)
   1.144 +            {
   1.145 +            iManager.DoExecuteJob();
   1.146 +            }
   1.147 +        }
   1.148 +
   1.149 +    /* Release any use of EGL by this thread. */
   1.150 +    eglReleaseThread();
   1.151 +
   1.152 +    JQLOG(("** EXIT * COpenWfcMonitorThread::Run()"));
   1.153 +	return KErrNone;
   1.154 +	}