epoc32/include/pls.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 0 061f57f2323e
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
     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 the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.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 * Redistribution and use in source and binary forms, with or without 
    19 * modification, are permitted provided that the following conditions are met:
    20 * Redistributions of source code must retain the above copyright notice, this 
    21 * list of conditions and the following disclaimer. 
    22 * Redistributions in binary form must reproduce the above copyright notice, 
    23 * this list of conditions and the following disclaimer in the documentation 
    24 * and/or other materials provided with the distribution. 
    25 * Neither the name of the <ORGANIZATION> nor the names of its contributors 
    26 * may be used to endorse or promote products derived from this software 
    27 * without specific prior written permission. 
    28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    31 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
    32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
    33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
    34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
    35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    38 *
    39 */
    40 
    41 
    42  
    43 #ifndef __PLS_H__
    44 #define __PLS_H__
    45 
    46 #ifdef __WINSCW__
    47  
    48 #include <ewsd.h>
    49 
    50 // Panic strings
    51 _LIT(KVirtualAllocFailure, "WSD VirtualAlloc() failed");
    52 _LIT(KPLSInitFailed, "WSD PLS init failed");
    53 _LIT(KWsdArrayFull, "WSD process or lib array full");	
    54 
    55 /**  
    56 A templated function that is used by a library (DLL) that requires to use
    57 WSD on the emulator.
    58 The function returns the PLS (Process Local Storage) object of the specified library, 
    59 for the current process. If the PLS object doesn't yet exist then it is allocated, 
    60 initialised, stored and returned.
    61 The template type T is the type of the PLS object, and is supplied by the caller.
    62 
    63 Takes as a parameter the TUid of the library DLL whose PLS is to be returned for the 
    64 current process. It also takes as a parameter a pointer to a (non-leaving, non-panicing) 
    65 initialisation function defined by the caller which takes a pointer to T (i.e. the 
    66 PLS object) as a parameter and returns one of the system wide error codes as a TInt.
    67 This parameter is optional but it should be used when necessary to ensure that if Pls() 
    68 requires to create a PLS object then the object is completely initialised on its return. 
    69 The initialisation function is called after the PLS object has been allocated and its 
    70 constructor called if it is an instance of a class - neither the constructor nor the 
    71 initialisation function should call Pls().
    72 
    73 Returns a pointer to the PLS object					
    74 */
    75 template <typename T>
    76 T* Pls(const TUid& aLibraryUid, TInt (*aInitFunc)(T*) = 0)
    77 	{
    78 	// Fetch the PLS, if it has been set
    79 	T* p = (T*) CheckPls(aLibraryUid);
    80 	if (p)
    81 		{
    82 		return p;
    83 		}
    84 	
    85 	// Obtain ownership of the mutex
    86 	TAny* mutexHandle = ObtainPlsMutex(); 
    87 			
    88 	// Check we haven't obtained the mutex from 
    89 	// another thread that has just set the same PLS!
    90 	p = (T*) CheckPls(aLibraryUid);
    91 	if (p) 
    92 		{
    93 		ReleasePlsMutex(mutexHandle);				
    94 		return p;
    95 		}
    96 	
    97 	// Allocate the memory for the PLS object
    98 	p = (T*) AllocatePls(sizeof(T));
    99 	if (!p)
   100 		{
   101 		ReleasePlsMutex(mutexHandle);
   102 		User::Panic(KVirtualAllocFailure, KErrNoMemory);
   103 		}
   104 			
   105 	// Do a placement new to construct the PLS object in the allocated memory
   106 	p = new (p) T;
   107 	
   108 	// Call the initialisation function (if one is provided)
   109 	// to complete initialisation of the PLS object
   110 	if (aInitFunc)
   111 		{
   112 		 if (((*aInitFunc)(p)) != KErrNone) 
   113 		 	{
   114 		 	 FreePls(p);			
   115 			 ReleasePlsMutex(mutexHandle);
   116 			 User::Panic(KPLSInitFailed, KErrGeneral);
   117 		 	}
   118 		}
   119 		
   120 	// Finally, call SetPls() to store the PLS object.
   121 	// NOTE: This step is last to ensure that a PLS object returned by 
   122 	// CheckPls() is completely constructed/initialised. This is important 
   123 	// to handle the scenario in which the thread that is creating the PLS 
   124 	// object is interrupted by another call to Pls() by another thread
   125 	if (SetPls(p, aLibraryUid) != KErrNone)
   126 		{
   127 		// SetPls() failed due to a size limit being reached in the wsdArray
   128 		FreePls(p);
   129 		ReleasePlsMutex(mutexHandle);
   130 		User::Panic(KWsdArrayFull, KErrNoMemory);				
   131 		}
   132 
   133 	ReleasePlsMutex(mutexHandle);		
   134 	return p;
   135 	}
   136 
   137 #endif // __WINSCW__
   138 
   139 #endif // __PLS_H__
   140