epoc32/include/pls.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
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@4
     5
* under the terms of "Eclipse Public License v1.0"
williamr@2
     6
* which accompanies this distribution, and is available
williamr@4
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.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
*
williamr@2
    19
*/
williamr@2
    20
williamr@2
    21
williamr@2
    22
 
williamr@2
    23
#ifndef __PLS_H__
williamr@2
    24
#define __PLS_H__
williamr@2
    25
williamr@2
    26
#ifdef __WINSCW__
williamr@2
    27
 
williamr@2
    28
#include <ewsd.h>
williamr@2
    29
williamr@2
    30
// Panic strings
williamr@2
    31
_LIT(KVirtualAllocFailure, "WSD VirtualAlloc() failed");
williamr@2
    32
_LIT(KPLSInitFailed, "WSD PLS init failed");
williamr@2
    33
_LIT(KWsdArrayFull, "WSD process or lib array full");	
williamr@2
    34
williamr@2
    35
/**  
williamr@2
    36
A templated function that is used by a library (DLL) that requires to use
williamr@2
    37
WSD on the emulator.
williamr@2
    38
The function returns the PLS (Process Local Storage) object of the specified library, 
williamr@2
    39
for the current process. If the PLS object doesn't yet exist then it is allocated, 
williamr@2
    40
initialised, stored and returned.
williamr@2
    41
The template type T is the type of the PLS object, and is supplied by the caller.
williamr@2
    42
williamr@2
    43
Takes as a parameter the TUid of the library DLL whose PLS is to be returned for the 
williamr@2
    44
current process. It also takes as a parameter a pointer to a (non-leaving, non-panicing) 
williamr@2
    45
initialisation function defined by the caller which takes a pointer to T (i.e. the 
williamr@2
    46
PLS object) as a parameter and returns one of the system wide error codes as a TInt.
williamr@2
    47
This parameter is optional but it should be used when necessary to ensure that if Pls() 
williamr@2
    48
requires to create a PLS object then the object is completely initialised on its return. 
williamr@2
    49
The initialisation function is called after the PLS object has been allocated and its 
williamr@2
    50
constructor called if it is an instance of a class - neither the constructor nor the 
williamr@2
    51
initialisation function should call Pls().
williamr@2
    52
williamr@2
    53
Returns a pointer to the PLS object					
williamr@2
    54
*/
williamr@2
    55
template <typename T>
williamr@2
    56
T* Pls(const TUid& aLibraryUid, TInt (*aInitFunc)(T*) = 0)
williamr@2
    57
	{
williamr@2
    58
	// Fetch the PLS, if it has been set
williamr@2
    59
	T* p = (T*) CheckPls(aLibraryUid);
williamr@2
    60
	if (p)
williamr@2
    61
		{
williamr@2
    62
		return p;
williamr@2
    63
		}
williamr@2
    64
	
williamr@2
    65
	// Obtain ownership of the mutex
williamr@2
    66
	TAny* mutexHandle = ObtainPlsMutex(); 
williamr@2
    67
			
williamr@2
    68
	// Check we haven't obtained the mutex from 
williamr@2
    69
	// another thread that has just set the same PLS!
williamr@2
    70
	p = (T*) CheckPls(aLibraryUid);
williamr@2
    71
	if (p) 
williamr@2
    72
		{
williamr@2
    73
		ReleasePlsMutex(mutexHandle);				
williamr@2
    74
		return p;
williamr@2
    75
		}
williamr@2
    76
	
williamr@2
    77
	// Allocate the memory for the PLS object
williamr@2
    78
	p = (T*) AllocatePls(sizeof(T));
williamr@2
    79
	if (!p)
williamr@2
    80
		{
williamr@2
    81
		ReleasePlsMutex(mutexHandle);
williamr@2
    82
		User::Panic(KVirtualAllocFailure, KErrNoMemory);
williamr@2
    83
		}
williamr@2
    84
			
williamr@2
    85
	// Do a placement new to construct the PLS object in the allocated memory
williamr@2
    86
	p = new (p) T;
williamr@2
    87
	
williamr@2
    88
	// Call the initialisation function (if one is provided)
williamr@2
    89
	// to complete initialisation of the PLS object
williamr@2
    90
	if (aInitFunc)
williamr@2
    91
		{
williamr@2
    92
		 if (((*aInitFunc)(p)) != KErrNone) 
williamr@2
    93
		 	{
williamr@2
    94
		 	 FreePls(p);			
williamr@2
    95
			 ReleasePlsMutex(mutexHandle);
williamr@2
    96
			 User::Panic(KPLSInitFailed, KErrGeneral);
williamr@2
    97
		 	}
williamr@2
    98
		}
williamr@2
    99
		
williamr@2
   100
	// Finally, call SetPls() to store the PLS object.
williamr@2
   101
	// NOTE: This step is last to ensure that a PLS object returned by 
williamr@2
   102
	// CheckPls() is completely constructed/initialised. This is important 
williamr@2
   103
	// to handle the scenario in which the thread that is creating the PLS 
williamr@2
   104
	// object is interrupted by another call to Pls() by another thread
williamr@2
   105
	if (SetPls(p, aLibraryUid) != KErrNone)
williamr@2
   106
		{
williamr@2
   107
		// SetPls() failed due to a size limit being reached in the wsdArray
williamr@2
   108
		FreePls(p);
williamr@2
   109
		ReleasePlsMutex(mutexHandle);
williamr@2
   110
		User::Panic(KWsdArrayFull, KErrNoMemory);				
williamr@2
   111
		}
williamr@2
   112
williamr@2
   113
	ReleasePlsMutex(mutexHandle);		
williamr@2
   114
	return p;
williamr@2
   115
	}
williamr@2
   116
williamr@2
   117
#endif // __WINSCW__
williamr@2
   118
williamr@2
   119
#endif // __PLS_H__
williamr@2
   120