os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/mrefcntobj.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 @file
sl@0
    18
 @internalComponent
sl@0
    19
*/
sl@0
    20
sl@0
    21
#ifndef MREFCNTOBJ_H
sl@0
    22
#define MREFCNTOBJ_H
sl@0
    23
sl@0
    24
#include <klib.h>
sl@0
    25
sl@0
    26
/**
sl@0
    27
Base class for creating reference counted objects.
sl@0
    28
sl@0
    29
This provide basic reference counting and asynchronous cleanup
sl@0
    30
for derived classes. An object whose reference count reaches zero
sl@0
    31
will be destroyed.
sl@0
    32
*/
sl@0
    33
class DReferenceCountedObject : public DBase
sl@0
    34
	{
sl@0
    35
public:
sl@0
    36
	/**
sl@0
    37
	Constructs this object with an initial reference count of one.
sl@0
    38
	*/
sl@0
    39
	DReferenceCountedObject();
sl@0
    40
sl@0
    41
	/**
sl@0
    42
	Atomically increment this object's reference count.
sl@0
    43
	A fault is raised if the count was initially <= 0.
sl@0
    44
sl@0
    45
	@pre Calling thread must be in a critical section
sl@0
    46
	@pre #iReferenceCount>0
sl@0
    47
	*/
sl@0
    48
	void Open();
sl@0
    49
sl@0
    50
	/**
sl@0
    51
	Atomically increment this object's reference count if it 
sl@0
    52
	was initially > 0.
sl@0
    53
sl@0
    54
	This is for use in situations where a reference counted object 
sl@0
    55
	is found by looking in a list, and the list may contain
sl@0
    56
	objects which have had their reference counts decremented
sl@0
    57
	to zero and are in the process of being cleaned up. A failure
sl@0
    58
	to open a reference on these objects should be taken as an
sl@0
    59
	indication that the object should be ignored, and treated as
sl@0
    60
	though it were never found.
sl@0
    61
	
sl@0
    62
	@return True if the count was incremented.
sl@0
    63
			False if it wasn't: because it was initially <= 0.
sl@0
    64
sl@0
    65
	@pre Calling thread must be in a critical section
sl@0
    66
	*/
sl@0
    67
	TBool TryOpen();
sl@0
    68
sl@0
    69
	/**
sl@0
    70
	Decrement this object's reference count and if it reaches zero,
sl@0
    71
	delete this object.
sl@0
    72
sl@0
    73
	@pre Calling thread must be in a critical section.
sl@0
    74
	@pre No fast mutex can be held.
sl@0
    75
	@pre No mutexes with order greater less than KMutexOrdKernelHeap can be held.
sl@0
    76
	*/
sl@0
    77
	void Close();
sl@0
    78
sl@0
    79
	/**
sl@0
    80
	Decrement this object's reference count and if this reaches zero,
sl@0
    81
	queue this object for asynchronous deletion.
sl@0
    82
sl@0
    83
	@pre Calling thread must be in a critical section.
sl@0
    84
	@pre No fast mutex can be held.
sl@0
    85
	*/
sl@0
    86
	void AsyncClose();
sl@0
    87
sl@0
    88
protected:
sl@0
    89
	/**
sl@0
    90
	Destructor which asserts in debug builds that the object's reference count is zero.
sl@0
    91
	*/
sl@0
    92
	virtual ~DReferenceCountedObject();
sl@0
    93
sl@0
    94
	/**
sl@0
    95
	Return true if the preconditions for #Close are met.
sl@0
    96
	This is for use by derived classes which overload the #Close method.
sl@0
    97
	*/
sl@0
    98
	TBool CheckCloseIsSafe();
sl@0
    99
sl@0
   100
	/**
sl@0
   101
	Return true if the preconditions for #AsyncClose are met.
sl@0
   102
	This is for use by derived classes which overload the #AsyncClose method.
sl@0
   103
	*/
sl@0
   104
	TBool CheckAsyncCloseIsSafe();
sl@0
   105
sl@0
   106
protected:
sl@0
   107
sl@0
   108
	/**
sl@0
   109
	This object's reference count. Must always be >= 0.
sl@0
   110
	*/
sl@0
   111
	TInt iReferenceCount;
sl@0
   112
	};
sl@0
   113
sl@0
   114
sl@0
   115
FORCE_INLINE DReferenceCountedObject::DReferenceCountedObject()
sl@0
   116
	: iReferenceCount(1)
sl@0
   117
	{
sl@0
   118
	}
sl@0
   119
sl@0
   120
#endif