os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/mrefcntobj.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/mrefcntobj.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,120 @@
     1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +/**
    1.20 + @file
    1.21 + @internalComponent
    1.22 +*/
    1.23 +
    1.24 +#ifndef MREFCNTOBJ_H
    1.25 +#define MREFCNTOBJ_H
    1.26 +
    1.27 +#include <klib.h>
    1.28 +
    1.29 +/**
    1.30 +Base class for creating reference counted objects.
    1.31 +
    1.32 +This provide basic reference counting and asynchronous cleanup
    1.33 +for derived classes. An object whose reference count reaches zero
    1.34 +will be destroyed.
    1.35 +*/
    1.36 +class DReferenceCountedObject : public DBase
    1.37 +	{
    1.38 +public:
    1.39 +	/**
    1.40 +	Constructs this object with an initial reference count of one.
    1.41 +	*/
    1.42 +	DReferenceCountedObject();
    1.43 +
    1.44 +	/**
    1.45 +	Atomically increment this object's reference count.
    1.46 +	A fault is raised if the count was initially <= 0.
    1.47 +
    1.48 +	@pre Calling thread must be in a critical section
    1.49 +	@pre #iReferenceCount>0
    1.50 +	*/
    1.51 +	void Open();
    1.52 +
    1.53 +	/**
    1.54 +	Atomically increment this object's reference count if it 
    1.55 +	was initially > 0.
    1.56 +
    1.57 +	This is for use in situations where a reference counted object 
    1.58 +	is found by looking in a list, and the list may contain
    1.59 +	objects which have had their reference counts decremented
    1.60 +	to zero and are in the process of being cleaned up. A failure
    1.61 +	to open a reference on these objects should be taken as an
    1.62 +	indication that the object should be ignored, and treated as
    1.63 +	though it were never found.
    1.64 +	
    1.65 +	@return True if the count was incremented.
    1.66 +			False if it wasn't: because it was initially <= 0.
    1.67 +
    1.68 +	@pre Calling thread must be in a critical section
    1.69 +	*/
    1.70 +	TBool TryOpen();
    1.71 +
    1.72 +	/**
    1.73 +	Decrement this object's reference count and if it reaches zero,
    1.74 +	delete this object.
    1.75 +
    1.76 +	@pre Calling thread must be in a critical section.
    1.77 +	@pre No fast mutex can be held.
    1.78 +	@pre No mutexes with order greater less than KMutexOrdKernelHeap can be held.
    1.79 +	*/
    1.80 +	void Close();
    1.81 +
    1.82 +	/**
    1.83 +	Decrement this object's reference count and if this reaches zero,
    1.84 +	queue this object for asynchronous deletion.
    1.85 +
    1.86 +	@pre Calling thread must be in a critical section.
    1.87 +	@pre No fast mutex can be held.
    1.88 +	*/
    1.89 +	void AsyncClose();
    1.90 +
    1.91 +protected:
    1.92 +	/**
    1.93 +	Destructor which asserts in debug builds that the object's reference count is zero.
    1.94 +	*/
    1.95 +	virtual ~DReferenceCountedObject();
    1.96 +
    1.97 +	/**
    1.98 +	Return true if the preconditions for #Close are met.
    1.99 +	This is for use by derived classes which overload the #Close method.
   1.100 +	*/
   1.101 +	TBool CheckCloseIsSafe();
   1.102 +
   1.103 +	/**
   1.104 +	Return true if the preconditions for #AsyncClose are met.
   1.105 +	This is for use by derived classes which overload the #AsyncClose method.
   1.106 +	*/
   1.107 +	TBool CheckAsyncCloseIsSafe();
   1.108 +
   1.109 +protected:
   1.110 +
   1.111 +	/**
   1.112 +	This object's reference count. Must always be >= 0.
   1.113 +	*/
   1.114 +	TInt iReferenceCount;
   1.115 +	};
   1.116 +
   1.117 +
   1.118 +FORCE_INLINE DReferenceCountedObject::DReferenceCountedObject()
   1.119 +	: iReferenceCount(1)
   1.120 +	{
   1.121 +	}
   1.122 +
   1.123 +#endif