os/ossrv/genericopenlibs/openenvcore/ewsd/inc/pls.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
* Name        : pls.h
sl@0
    16
* Part of     : Client API for ewsd library
sl@0
    17
* Contains the client API for using the emulator WSD library
sl@0
    18
*
sl@0
    19
*/
sl@0
    20
sl@0
    21
sl@0
    22
 
sl@0
    23
#ifndef __PLS_H__
sl@0
    24
#define __PLS_H__
sl@0
    25
sl@0
    26
#ifdef __WINSCW__
sl@0
    27
 
sl@0
    28
#include <ewsd.h>
sl@0
    29
sl@0
    30
// Panic strings
sl@0
    31
_LIT(KVirtualAllocFailure, "WSD VirtualAlloc() failed");
sl@0
    32
_LIT(KPLSInitFailed, "WSD PLS init failed");
sl@0
    33
_LIT(KWsdArrayFull, "WSD process or lib array full");	
sl@0
    34
sl@0
    35
/**  
sl@0
    36
A templated function that is used by a library (DLL) that requires to use
sl@0
    37
WSD on the emulator.
sl@0
    38
The function returns the PLS (Process Local Storage) object of the specified library, 
sl@0
    39
for the current process. If the PLS object doesn't yet exist then it is allocated, 
sl@0
    40
initialised, stored and returned.
sl@0
    41
The template type T is the type of the PLS object, and is supplied by the caller.
sl@0
    42
sl@0
    43
Takes as a parameter the TUid of the library DLL whose PLS is to be returned for the 
sl@0
    44
current process. It also takes as a parameter a pointer to a (non-leaving, non-panicing) 
sl@0
    45
initialisation function defined by the caller which takes a pointer to T (i.e. the 
sl@0
    46
PLS object) as a parameter and returns one of the system wide error codes as a TInt.
sl@0
    47
This parameter is optional but it should be used when necessary to ensure that if Pls() 
sl@0
    48
requires to create a PLS object then the object is completely initialised on its return. 
sl@0
    49
The initialisation function is called after the PLS object has been allocated and its 
sl@0
    50
constructor called if it is an instance of a class - neither the constructor nor the 
sl@0
    51
initialisation function should call Pls().
sl@0
    52
sl@0
    53
Returns a pointer to the PLS object					
sl@0
    54
*/
sl@0
    55
template <typename T>
sl@0
    56
T* Pls(const TUid& aLibraryUid, TInt (*aInitFunc)(T*) = 0)
sl@0
    57
	{
sl@0
    58
	// Fetch the PLS, if it has been set
sl@0
    59
	T* p = (T*) CheckPls(aLibraryUid);
sl@0
    60
	if (p)
sl@0
    61
		{
sl@0
    62
		return p;
sl@0
    63
		}
sl@0
    64
	
sl@0
    65
	// Obtain ownership of the mutex
sl@0
    66
	TAny* mutexHandle = ObtainPlsMutex(); 
sl@0
    67
			
sl@0
    68
	// Check we haven't obtained the mutex from 
sl@0
    69
	// another thread that has just set the same PLS!
sl@0
    70
	p = (T*) CheckPls(aLibraryUid);
sl@0
    71
	if (p) 
sl@0
    72
		{
sl@0
    73
		ReleasePlsMutex(mutexHandle);				
sl@0
    74
		return p;
sl@0
    75
		}
sl@0
    76
	
sl@0
    77
	// Allocate the memory for the PLS object
sl@0
    78
	p = (T*) AllocatePls(sizeof(T));
sl@0
    79
	if (!p)
sl@0
    80
		{
sl@0
    81
		ReleasePlsMutex(mutexHandle);
sl@0
    82
		User::Panic(KVirtualAllocFailure, KErrNoMemory);
sl@0
    83
		}
sl@0
    84
			
sl@0
    85
	// Do a placement new to construct the PLS object in the allocated memory
sl@0
    86
	p = new (p) T;
sl@0
    87
	
sl@0
    88
	// Call the initialisation function (if one is provided)
sl@0
    89
	// to complete initialisation of the PLS object
sl@0
    90
	if (aInitFunc)
sl@0
    91
		{
sl@0
    92
		 if (((*aInitFunc)(p)) != KErrNone) 
sl@0
    93
		 	{
sl@0
    94
		 	 FreePls(p);			
sl@0
    95
			 ReleasePlsMutex(mutexHandle);
sl@0
    96
			 User::Panic(KPLSInitFailed, KErrGeneral);
sl@0
    97
		 	}
sl@0
    98
		}
sl@0
    99
		
sl@0
   100
	// Finally, call SetPls() to store the PLS object.
sl@0
   101
	// NOTE: This step is last to ensure that a PLS object returned by 
sl@0
   102
	// CheckPls() is completely constructed/initialised. This is important 
sl@0
   103
	// to handle the scenario in which the thread that is creating the PLS 
sl@0
   104
	// object is interrupted by another call to Pls() by another thread
sl@0
   105
	if (SetPls(p, aLibraryUid) != KErrNone)
sl@0
   106
		{
sl@0
   107
		// SetPls() failed due to a size limit being reached in the wsdArray
sl@0
   108
		FreePls(p);
sl@0
   109
		ReleasePlsMutex(mutexHandle);
sl@0
   110
		User::Panic(KWsdArrayFull, KErrNoMemory);				
sl@0
   111
		}
sl@0
   112
sl@0
   113
	ReleasePlsMutex(mutexHandle);		
sl@0
   114
	return p;
sl@0
   115
	}
sl@0
   116
sl@0
   117
#endif // __WINSCW__
sl@0
   118
sl@0
   119
#endif // __PLS_H__
sl@0
   120