blob: 51705f7a655d55b37d32f7ea947bfe5e7bca9fdf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
#include <IceUtil/GCRecMutex.h>
#include <IceUtil/GCShared.h>
using namespace IceUtil;
IceUtil::GCObjectSet IceUtil::gcObjects;
IceUtil::GCShared::GCShared()
: _ref(0), _noDelete(false)
{
}
IceUtil::GCShared::~GCShared()
{
gcRecMutex._m->lock();
gcObjects.erase(this);
gcRecMutex._m->unlock();
}
void
IceUtil::GCShared::__incRef()
{
gcRecMutex._m->lock();
assert(_ref >= 0);
if(_ref == 0)
{
#ifdef NDEBUG // To avoid annoying warnings about variables that are not used...
gcObjects.insert(this);
#else
std::pair<GCObjectSet::iterator, bool> rc = gcObjects.insert(this);
assert(rc.second);
#endif
}
++_ref;
gcRecMutex._m->unlock();
}
void
IceUtil::GCShared::__decRef()
{
gcRecMutex._m->lock();
bool doDelete = false;
assert(_ref > 0);
if(--_ref == 0)
{
doDelete = !_noDelete;
_noDelete = true;
#ifdef NDEBUG // To avoid annoying warnings about variables that are not used...
gcObjects.erase(this);
#else
GCObjectSet::size_type num = gcObjects.erase(this);
assert(num == 1);
#endif
}
gcRecMutex._m->unlock();
if(doDelete)
{
delete this;
}
}
int
IceUtil::GCShared::__getRef() const
{
gcRecMutex._m->lock();
int ref = _ref;
gcRecMutex._m->unlock();
return ref;
}
void
IceUtil::GCShared::__setNoDelete(bool b)
{
gcRecMutex._m->lock();
_noDelete = b;
gcRecMutex._m->unlock();
}
void
IceUtil::GCShared::__decRefUnsafe()
{
--_ref;
}
void
IceUtil::GCShared::__addObject(GCObjectMultiSet& c, GCShared* p)
{
gcRecMutex._m->lock();
if(p)
{
c.insert(p);
}
gcRecMutex._m->unlock();
}
|