os/ossrv/genericopenlibs/openenvcore/ewsd/inc/pls.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 * Name        : pls.h
    16 * Part of     : Client API for ewsd library
    17 * Contains the client API for using the emulator WSD library
    18 *
    19 */
    20 
    21 
    22  
    23 #ifndef __PLS_H__
    24 #define __PLS_H__
    25 
    26 #ifdef __WINSCW__
    27  
    28 #include <ewsd.h>
    29 
    30 // Panic strings
    31 _LIT(KVirtualAllocFailure, "WSD VirtualAlloc() failed");
    32 _LIT(KPLSInitFailed, "WSD PLS init failed");
    33 _LIT(KWsdArrayFull, "WSD process or lib array full");	
    34 
    35 /**  
    36 A templated function that is used by a library (DLL) that requires to use
    37 WSD on the emulator.
    38 The function returns the PLS (Process Local Storage) object of the specified library, 
    39 for the current process. If the PLS object doesn't yet exist then it is allocated, 
    40 initialised, stored and returned.
    41 The template type T is the type of the PLS object, and is supplied by the caller.
    42 
    43 Takes as a parameter the TUid of the library DLL whose PLS is to be returned for the 
    44 current process. It also takes as a parameter a pointer to a (non-leaving, non-panicing) 
    45 initialisation function defined by the caller which takes a pointer to T (i.e. the 
    46 PLS object) as a parameter and returns one of the system wide error codes as a TInt.
    47 This parameter is optional but it should be used when necessary to ensure that if Pls() 
    48 requires to create a PLS object then the object is completely initialised on its return. 
    49 The initialisation function is called after the PLS object has been allocated and its 
    50 constructor called if it is an instance of a class - neither the constructor nor the 
    51 initialisation function should call Pls().
    52 
    53 Returns a pointer to the PLS object					
    54 */
    55 template <typename T>
    56 T* Pls(const TUid& aLibraryUid, TInt (*aInitFunc)(T*) = 0)
    57 	{
    58 	// Fetch the PLS, if it has been set
    59 	T* p = (T*) CheckPls(aLibraryUid);
    60 	if (p)
    61 		{
    62 		return p;
    63 		}
    64 	
    65 	// Obtain ownership of the mutex
    66 	TAny* mutexHandle = ObtainPlsMutex(); 
    67 			
    68 	// Check we haven't obtained the mutex from 
    69 	// another thread that has just set the same PLS!
    70 	p = (T*) CheckPls(aLibraryUid);
    71 	if (p) 
    72 		{
    73 		ReleasePlsMutex(mutexHandle);				
    74 		return p;
    75 		}
    76 	
    77 	// Allocate the memory for the PLS object
    78 	p = (T*) AllocatePls(sizeof(T));
    79 	if (!p)
    80 		{
    81 		ReleasePlsMutex(mutexHandle);
    82 		User::Panic(KVirtualAllocFailure, KErrNoMemory);
    83 		}
    84 			
    85 	// Do a placement new to construct the PLS object in the allocated memory
    86 	p = new (p) T;
    87 	
    88 	// Call the initialisation function (if one is provided)
    89 	// to complete initialisation of the PLS object
    90 	if (aInitFunc)
    91 		{
    92 		 if (((*aInitFunc)(p)) != KErrNone) 
    93 		 	{
    94 		 	 FreePls(p);			
    95 			 ReleasePlsMutex(mutexHandle);
    96 			 User::Panic(KPLSInitFailed, KErrGeneral);
    97 		 	}
    98 		}
    99 		
   100 	// Finally, call SetPls() to store the PLS object.
   101 	// NOTE: This step is last to ensure that a PLS object returned by 
   102 	// CheckPls() is completely constructed/initialised. This is important 
   103 	// to handle the scenario in which the thread that is creating the PLS 
   104 	// object is interrupted by another call to Pls() by another thread
   105 	if (SetPls(p, aLibraryUid) != KErrNone)
   106 		{
   107 		// SetPls() failed due to a size limit being reached in the wsdArray
   108 		FreePls(p);
   109 		ReleasePlsMutex(mutexHandle);
   110 		User::Panic(KWsdArrayFull, KErrNoMemory);				
   111 		}
   112 
   113 	ReleasePlsMutex(mutexHandle);		
   114 	return p;
   115 	}
   116 
   117 #endif // __WINSCW__
   118 
   119 #endif // __PLS_H__
   120