1 #include <iostream>
2 
3 #include "refcount.h"
4 #include "debug.h"
5 
6 using namespace std;
7 
RefCount()8 RefCount::RefCount() : refcount(1)
9 {
10 }
11 
~RefCount()12 RefCount::~RefCount()
13 {
14 	Debug[TRACE] << "In RefCount destructor" << endl;
15 }
16 
ObtainRefMutex()17 void RefCount::ObtainRefMutex()
18 {
19 	Debug[TRACE] << "In ObtainRefMutex" << endl;
20 	refmutex.ObtainMutex();
21 	Debug[TRACE] << "Obtained" << endl;
22 }
23 
ReleaseRefMutex()24 void RefCount::ReleaseRefMutex()
25 {
26 	refmutex.ReleaseMutex();
27 }
28 
Ref()29 void RefCount::Ref()
30 {
31 	ObtainRefMutex();
32 	++refcount;
33 	Debug[TRACE] << "Ref: count is " << refcount << endl;
34 	ReleaseRefMutex();
35 }
36 
UnRef()37 void RefCount::UnRef()
38 {
39 	ObtainRefMutex();
40 	--refcount;
41 
42 	Debug[TRACE] << "UnRef: count is " << refcount << endl;
43 
44 	ReleaseRefMutex();
45 
46 	if(refcount==0)
47 	{
48 		Debug[TRACE] << "UnRef: deleting object" << endl;
49 		delete this;
50 	}
51 	Debug[TRACE] << "UnRef complete" << endl;
52 }
53 
54 PTMutex RefCount::refmutex;
55 
56 
57