kerneltest/e32test/cppexceptions/t_unmap2.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2004-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 the License "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 // e32test\cppexceptions\t_unmap2.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <e32std_private.h>
       
    20 #include <e32base.h>
       
    21 #include <e32base_private.h>
       
    22 #include <e32test.h>
       
    23 #include "d_unmap.h"
       
    24 
       
    25 _LIT(KTestName, "t_unmap2");
       
    26 RTest test(KTestName);
       
    27 RSemaphore WaitSemaphore;
       
    28 RSemaphore SignalSemaphore;
       
    29 
       
    30 void TestLibraryCleanup();
       
    31 
       
    32 TInt E32Main()
       
    33    	{
       
    34 	test.Start(_L("Check code seg unmapping over User::Leave()/C++ exceptions."));
       
    35 
       
    36 	__UHEAP_MARK;
       
    37    	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    38    	TInt r = KErrNoMemory;
       
    39    	if (cleanup)
       
    40    		{
       
    41 		test.Printf(_L("Opening semaphores for barrier with other process\n"));
       
    42 		test(WaitSemaphore.Open(1)==KErrNone);
       
    43 		test(SignalSemaphore.Open(2)==KErrNone);
       
    44 		TRAP(r, TestLibraryCleanup());
       
    45 		test.Printf(_L("Returned %d, expected %d\n"), r, KErrGeneral);
       
    46 		test(r == KErrGeneral);
       
    47 		WaitSemaphore.Close();
       
    48 		SignalSemaphore.Close();
       
    49 		r = KErrNone;
       
    50    		}
       
    51 
       
    52 	delete cleanup;
       
    53  	__UHEAP_MARKEND;
       
    54 
       
    55 	test.End();
       
    56    	return r;
       
    57    	}
       
    58 
       
    59 void Checkpoint(TAny*)
       
    60 	{
       
    61 	test.Printf(_L("Checkpointing...\n"));
       
    62 	SignalSemaphore.Signal();
       
    63 	WaitSemaphore.Wait();
       
    64 	}
       
    65 
       
    66 void TestLibraryCleanup()
       
    67 	{
       
    68     test.Printf(_L("Loading DLL.\n"));
       
    69 	RLibrary library;
       
    70 	test(library.Load(KLeavingDll2) == KErrNone);
       
    71 
       
    72     test.Printf(_L("Pushing cleanup item to synchronise after closing library handle.\n"));
       
    73 	CleanupStack::PushL(TCleanupItem(&Checkpoint, NULL));
       
    74 
       
    75     test.Printf(_L("Pushing handle to dynamically loaded DLL onto the cleanup stack.\n"));
       
    76 	CleanupClosePushL(library);
       
    77 
       
    78 	test.Printf(_L("Pushing cleanup item to synchronise during leave\n"));
       
    79 	CleanupStack::PushL(TCleanupItem(&Checkpoint, NULL));
       
    80 
       
    81     test.Printf(_L("Looking up leaving function.\n"));
       
    82 	TLibraryFunction leaving = library.Lookup(1);
       
    83 	test(leaving != NULL);
       
    84 
       
    85 	test.Printf(_L("Check-point whilst holding the open library handle.\n"));
       
    86 	Checkpoint(NULL);
       
    87 
       
    88 	test.Printf(_L("Calling leaving function.\n"));
       
    89 	(*leaving)();
       
    90 
       
    91 	test.Printf(_L("Cleaning up checkpoint operation.\n"));
       
    92 	CleanupStack::Pop(); // pause leave op
       
    93 
       
    94 	test.Printf(_L("Cleaning up DLL handle.\n"));
       
    95 	CleanupStack::PopAndDestroy();
       
    96 
       
    97 	test.Printf(_L("Cleaning up checkpoint operation.\n"));
       
    98 	CleanupStack::Pop(); // pause leave op
       
    99 	}