Update contrib.
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
16 * Part of : Client API for ewsd library
17 * Contains the client API for using the emulator WSD library
31 _LIT(KVirtualAllocFailure, "WSD VirtualAlloc() failed");
32 _LIT(KPLSInitFailed, "WSD PLS init failed");
33 _LIT(KWsdArrayFull, "WSD process or lib array full");
36 A templated function that is used by a library (DLL) that requires to use
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.
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().
53 Returns a pointer to the PLS object
56 T* Pls(const TUid& aLibraryUid, TInt (*aInitFunc)(T*) = 0)
58 // Fetch the PLS, if it has been set
59 T* p = (T*) CheckPls(aLibraryUid);
65 // Obtain ownership of the mutex
66 TAny* mutexHandle = ObtainPlsMutex();
68 // Check we haven't obtained the mutex from
69 // another thread that has just set the same PLS!
70 p = (T*) CheckPls(aLibraryUid);
73 ReleasePlsMutex(mutexHandle);
77 // Allocate the memory for the PLS object
78 p = (T*) AllocatePls(sizeof(T));
81 ReleasePlsMutex(mutexHandle);
82 User::Panic(KVirtualAllocFailure, KErrNoMemory);
85 // Do a placement new to construct the PLS object in the allocated memory
88 // Call the initialisation function (if one is provided)
89 // to complete initialisation of the PLS object
92 if (((*aInitFunc)(p)) != KErrNone)
95 ReleasePlsMutex(mutexHandle);
96 User::Panic(KPLSInitFailed, KErrGeneral);
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)
107 // SetPls() failed due to a size limit being reached in the wsdArray
109 ReleasePlsMutex(mutexHandle);
110 User::Panic(KWsdArrayFull, KErrNoMemory);
113 ReleasePlsMutex(mutexHandle);