forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyOnWriteObject.h
More file actions
170 lines (139 loc) · 4.1 KB
/
copyOnWriteObject.h
File metadata and controls
170 lines (139 loc) · 4.1 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file copyOnWriteObject.h
* @author drose
* @date 2007-04-09
*/
#ifndef COPYONWRITEOBJECT_H
#define COPYONWRITEOBJECT_H
#include "pandabase.h"
#include "cachedTypedWritableReferenceCount.h"
#include "pmutex.h"
#include "conditionVar.h"
#include "mutexHolder.h"
// Should we implement full thread protection for CopyOnWritePointer? If we
// can be assured that no other thread will interrupt while a write pointer is
// held, we don't need thread protection.
// Nowadays, this is the same thing as asking if HAVE_THREADS is defined.
// Maybe we'll just replace COW_THREADED with HAVE_THREADS in the future.
#ifdef HAVE_THREADS
#define COW_THREADED 1
#else
#undef COW_THREADED
#endif
/**
* This base class provides basic reference counting, but also can be used
* with a CopyOnWritePointer to provide get_read_pointer() and
* get_write_pointer().
*/
class EXPCL_PANDA_PUTIL CopyOnWriteObject : public CachedTypedWritableReferenceCount {
public:
INLINE CopyOnWriteObject();
INLINE CopyOnWriteObject(const CopyOnWriteObject ©);
INLINE void operator = (const CopyOnWriteObject ©);
PUBLISHED:
#ifdef COW_THREADED
virtual bool unref() const;
INLINE void cache_ref() const;
INLINE bool cache_unref() const;
public:
void cache_ref_only() const;
#endif // COW_THREADED
protected:
virtual PT(CopyOnWriteObject) make_cow_copy()=0;
private:
#ifdef COW_THREADED
enum LockStatus {
LS_unlocked,
LS_locked_read,
LS_locked_write,
};
Mutex _lock_mutex;
ConditionVar _lock_cvar;
LockStatus _lock_status;
Thread *_locking_thread;
#endif // COW_THREADED
public:
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
PUBLISHED:
static TypeHandle get_class_type() {
return _type_handle;
}
public:
static void init_type() {
CachedTypedWritableReferenceCount::init_type();
register_type(_type_handle, "CopyOnWriteObject",
CachedTypedWritableReferenceCount::get_class_type());
}
private:
static TypeHandle _type_handle;
friend class CopyOnWritePointer;
};
/**
* This is similar to RefCountObj, but it implements a CopyOnWriteObject
* inheritance instead of a ReferenceCount inheritance.
*/
template<class Base>
class CopyOnWriteObj : public CopyOnWriteObject, public Base {
public:
INLINE CopyOnWriteObj();
INLINE CopyOnWriteObj(const Base ©);
INLINE CopyOnWriteObj(const CopyOnWriteObj<Base> ©);
ALLOC_DELETED_CHAIN(CopyOnWriteObj<Base>);
protected:
virtual PT(CopyOnWriteObject) make_cow_copy();
public:
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
PUBLISHED:
static TypeHandle get_class_type() {
return _type_handle;
}
public:
static void init_type();
private:
static TypeHandle _type_handle;
};
/**
* For objects (e.g. pvectors) whose constructor takes a single parameter.
*/
template<class Base, class Param1>
class CopyOnWriteObj1 : public CopyOnWriteObject, public Base {
public:
INLINE CopyOnWriteObj1(Param1 p1);
INLINE CopyOnWriteObj1(const Base ©);
INLINE CopyOnWriteObj1(const CopyOnWriteObj1<Base, Param1> ©);
typedef CopyOnWriteObj1<Base, Param1> ThisClass;
ALLOC_DELETED_CHAIN(ThisClass)
protected:
virtual PT(CopyOnWriteObject) make_cow_copy();
public:
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
PUBLISHED:
static TypeHandle get_class_type() {
return _type_handle;
}
public:
static void init_type();
private:
static TypeHandle _type_handle;
};
// We can safely redefine this as a no-op.
template<>
INLINE void PointerToBase<CopyOnWriteObject>::update_type(To *ptr) {}
#include "copyOnWriteObject.I"
#endif