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