messagingfw/watcherfw/src/watcher.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
child 41 0abbef78e78b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // watcher.cpp
       
    15 //
       
    16 
       
    17 #include <basched.h>
       
    18 #include <watcher.h>
       
    19 #include "watcherdef.h"
       
    20 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS 
       
    21 #include "cwatcher.h"
       
    22 #endif
       
    23 
       
    24 #include "cwatcherssastartupmgr.h"
       
    25 
       
    26 #pragma warning	(disable : 4702)
       
    27 
       
    28 _LIT(KThreadName1, "WatcherMainThread%d");
       
    29 _LIT(KThreadName2, "WatcherThread");
       
    30 const TInt KMinWatcherHeapSize = 0x8000;
       
    31 const TInt KMaxWatcherHeapSize = 0x200000;
       
    32 
       
    33 
       
    34 void DoLaunchWatchersL()
       
    35 	{
       
    36 	// Create scheduler
       
    37 	CBaActiveScheduler* scheduler = new(ELeave)CBaActiveScheduler;
       
    38 	CleanupStack::PushL(scheduler);
       
    39 
       
    40 	// Install scheduler
       
    41 	CActiveScheduler::Install(scheduler);
       
    42 
       
    43 	// Launch the start-up manager that will launch the watchers in the 
       
    44  	// correct start-up state
       
    45 	CWatcherSSAStartupMgr* startupMgr = CWatcherSSAStartupMgr::NewL();
       
    46 
       
    47 	// The System Starter is waiting for us (start_method = EWaitForStart)
       
    48 	// so we rendez-vous so it can start the next process in the SSC
       
    49 	RProcess::Rendezvous(KErrNone);
       
    50 
       
    51 	// We start the scheduler even if there are no watchers (vs previous
       
    52 	// behaviour) as the SSA manager is also an active object
       
    53 	CActiveScheduler::Start();
       
    54 	
       
    55 	// Tidy up
       
    56 	CleanupStack::PopAndDestroy(scheduler);
       
    57 	}
       
    58 
       
    59 static TInt DoLaunchThread(TAny*)
       
    60 	{
       
    61 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    62 	__ASSERT_ALWAYS(cleanup, Panic(ENoCleanup));
       
    63 	TRAP_IGNORE(DoLaunchWatchersL());		
       
    64 	delete cleanup;
       
    65 	return 0;
       
    66 	}
       
    67 
       
    68 //**********************************
       
    69 // CWatcher
       
    70 //**********************************
       
    71 
       
    72 EXPORT_C CWatcher* CWatcher::NewL(TInt aPriority)
       
    73 	{
       
    74 	return new(ELeave)CWatcher(aPriority);
       
    75 	}
       
    76 
       
    77 EXPORT_C CWatcher::~CWatcher()
       
    78 	{
       
    79 	Cancel();
       
    80 	}
       
    81 
       
    82 EXPORT_C void CWatcher::Start(TRequestStatus& aStatus)
       
    83 	{
       
    84 	// Try and create the watcher thread
       
    85 	iStatus = KRequestPending;
       
    86 
       
    87 	// Make sure the watching thread has a unique name
       
    88 	TName threadName;
       
    89 	threadName.Format(KThreadName1, iLaunchCount++);
       
    90 
       
    91 #ifdef __X86GCC__
       
    92 	// Some watchers overflow their stack on this platform when using KDefaultStackSize
       
    93 	TInt error = iThread.Create(threadName, DoLaunchThread, 2*KDefaultStackSize, KMinWatcherHeapSize, KMaxWatcherHeapSize, NULL, EOwnerThread);
       
    94 #else	
       
    95 	TInt error = iThread.Create(threadName, DoLaunchThread, KDefaultStackSize, KMinWatcherHeapSize, KMaxWatcherHeapSize, NULL, EOwnerThread);
       
    96 #endif // __X86GCC__
       
    97 	if (!error)
       
    98 		{
       
    99 		// Complete the request when the thread dies
       
   100 		iThread.Logon(iStatus);
       
   101 		iThread.Resume();
       
   102 		}
       
   103 	else
       
   104 		{
       
   105 		// Complete ourselves with the error
       
   106 		TRequestStatus* status = &iStatus;
       
   107 		User::RequestComplete(status, error);
       
   108 		}
       
   109 
       
   110 	aStatus = KRequestPending;
       
   111 	iObserver = &aStatus;
       
   112 	SetActive();
       
   113 	}
       
   114 
       
   115 CWatcher::CWatcher(TInt aPriority)
       
   116 : CActive(aPriority)
       
   117 	{
       
   118 	CActiveScheduler::Add(this);
       
   119 	}
       
   120 
       
   121 void CWatcher::DoCancel()
       
   122 	{
       
   123 	// Kill off the thread
       
   124 	iThread.Kill(KErrCancel);
       
   125 	iThread.Close();
       
   126 
       
   127 	User::RequestComplete(iObserver, KErrCancel);
       
   128 	}
       
   129 
       
   130 void CWatcher::RunL()
       
   131 	{
       
   132 	iThread.Close();
       
   133 
       
   134 	// Wait a bit
       
   135 	User::After(KWatcherDelay);
       
   136 	User::RequestComplete(iObserver, iStatus.Int());
       
   137 	}
       
   138 
       
   139 //**********************************
       
   140 // Watcher Exe
       
   141 //**********************************
       
   142 
       
   143 LOCAL_C void DoStartL()
       
   144 	{
       
   145 	User::LeaveIfError(User::RenameThread(KThreadName2));
       
   146 
       
   147 	CActiveScheduler::Install(new(ELeave)CActiveScheduler);
       
   148 	CleanupStack::PushL(CActiveScheduler::Current());
       
   149 	CWatcherWait* wait = CWatcherWait::NewLC();
       
   150 
       
   151 	// The watchers should always be running
       
   152 	CWatcher* watcher = CWatcher::NewL(CActive::EPriorityStandard);
       
   153 	FOREVER
       
   154 		{
       
   155 		wait->Start();
       
   156 		watcher->Start(wait->iStatus);
       
   157 		CActiveScheduler::Start();
       
   158 		}
       
   159 
       
   160 	// The following statements are never going to execute due to the endless loop above.
       
   161 	// They have been commented out to keep the compiler happy.
       
   162 	//delete watcher;
       
   163 	//CleanupStack::PopAndDestroy(4); // wait, CActiveScheduler, library, semaphore
       
   164 	}
       
   165 
       
   166 GLDEF_C TInt E32Main()
       
   167 	{       
       
   168 	__UHEAP_MARK;
       
   169 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   170 	TRAP_IGNORE(DoStartL());          
       
   171 	delete cleanup;      
       
   172 	__UHEAP_MARKEND;
       
   173 	return(KErrNone);
       
   174 	}
       
   175 
       
   176 EXPORT_C TInt WinsMain()
       
   177 	{
       
   178 	return KErrNone;
       
   179 	}
       
   180 
       
   181 
       
   182 #pragma warning	(default : 4702)