williamr@2: /* williamr@2: * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). williamr@2: * All rights reserved. williamr@2: * This component and the accompanying materials are made available williamr@4: * under the terms of "Eclipse Public License v1.0" williamr@2: * which accompanies this distribution, and is available williamr@4: * at the URL "http://www.eclipse.org/legal/epl-v10.html". williamr@2: * williamr@2: * Initial Contributors: williamr@2: * Nokia Corporation - initial contribution. williamr@2: * williamr@2: * Contributors: williamr@2: * williamr@2: * Description: williamr@2: * Name : pls.h williamr@2: * Part of : Client API for ewsd library williamr@2: * Contains the client API for using the emulator WSD library williamr@2: * williamr@2: */ williamr@2: williamr@2: williamr@2: williamr@2: #ifndef __PLS_H__ williamr@2: #define __PLS_H__ williamr@2: williamr@2: #ifdef __WINSCW__ williamr@2: williamr@2: #include williamr@2: williamr@2: // Panic strings williamr@2: _LIT(KVirtualAllocFailure, "WSD VirtualAlloc() failed"); williamr@2: _LIT(KPLSInitFailed, "WSD PLS init failed"); williamr@2: _LIT(KWsdArrayFull, "WSD process or lib array full"); williamr@2: williamr@2: /** williamr@2: A templated function that is used by a library (DLL) that requires to use williamr@2: WSD on the emulator. williamr@2: The function returns the PLS (Process Local Storage) object of the specified library, williamr@2: for the current process. If the PLS object doesn't yet exist then it is allocated, williamr@2: initialised, stored and returned. williamr@2: The template type T is the type of the PLS object, and is supplied by the caller. williamr@2: williamr@2: Takes as a parameter the TUid of the library DLL whose PLS is to be returned for the williamr@2: current process. It also takes as a parameter a pointer to a (non-leaving, non-panicing) williamr@2: initialisation function defined by the caller which takes a pointer to T (i.e. the williamr@2: PLS object) as a parameter and returns one of the system wide error codes as a TInt. williamr@2: This parameter is optional but it should be used when necessary to ensure that if Pls() williamr@2: requires to create a PLS object then the object is completely initialised on its return. williamr@2: The initialisation function is called after the PLS object has been allocated and its williamr@2: constructor called if it is an instance of a class - neither the constructor nor the williamr@2: initialisation function should call Pls(). williamr@2: williamr@2: Returns a pointer to the PLS object williamr@2: */ williamr@2: template williamr@2: T* Pls(const TUid& aLibraryUid, TInt (*aInitFunc)(T*) = 0) williamr@2: { williamr@2: // Fetch the PLS, if it has been set williamr@2: T* p = (T*) CheckPls(aLibraryUid); williamr@2: if (p) williamr@2: { williamr@2: return p; williamr@2: } williamr@2: williamr@2: // Obtain ownership of the mutex williamr@2: TAny* mutexHandle = ObtainPlsMutex(); williamr@2: williamr@2: // Check we haven't obtained the mutex from williamr@2: // another thread that has just set the same PLS! williamr@2: p = (T*) CheckPls(aLibraryUid); williamr@2: if (p) williamr@2: { williamr@2: ReleasePlsMutex(mutexHandle); williamr@2: return p; williamr@2: } williamr@2: williamr@2: // Allocate the memory for the PLS object williamr@2: p = (T*) AllocatePls(sizeof(T)); williamr@2: if (!p) williamr@2: { williamr@2: ReleasePlsMutex(mutexHandle); williamr@2: User::Panic(KVirtualAllocFailure, KErrNoMemory); williamr@2: } williamr@2: williamr@2: // Do a placement new to construct the PLS object in the allocated memory williamr@2: p = new (p) T; williamr@2: williamr@2: // Call the initialisation function (if one is provided) williamr@2: // to complete initialisation of the PLS object williamr@2: if (aInitFunc) williamr@2: { williamr@2: if (((*aInitFunc)(p)) != KErrNone) williamr@2: { williamr@2: FreePls(p); williamr@2: ReleasePlsMutex(mutexHandle); williamr@2: User::Panic(KPLSInitFailed, KErrGeneral); williamr@2: } williamr@2: } williamr@2: williamr@2: // Finally, call SetPls() to store the PLS object. williamr@2: // NOTE: This step is last to ensure that a PLS object returned by williamr@2: // CheckPls() is completely constructed/initialised. This is important williamr@2: // to handle the scenario in which the thread that is creating the PLS williamr@2: // object is interrupted by another call to Pls() by another thread williamr@2: if (SetPls(p, aLibraryUid) != KErrNone) williamr@2: { williamr@2: // SetPls() failed due to a size limit being reached in the wsdArray williamr@2: FreePls(p); williamr@2: ReleasePlsMutex(mutexHandle); williamr@2: User::Panic(KWsdArrayFull, KErrNoMemory); williamr@2: } williamr@2: williamr@2: ReleasePlsMutex(mutexHandle); williamr@2: return p; williamr@2: } williamr@2: williamr@2: #endif // __WINSCW__ williamr@2: williamr@2: #endif // __PLS_H__ williamr@2: