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