Panda3D
Loading...
Searching...
No Matches
copyOnWriteObject.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file copyOnWriteObject.h
10 * @author drose
11 * @date 2007-04-09
12 */
13
14#ifndef COPYONWRITEOBJECT_H
15#define COPYONWRITEOBJECT_H
16
17#include "pandabase.h"
18
20#include "pmutex.h"
21#include "conditionVar.h"
22#include "mutexHolder.h"
23
24// Should we implement full thread protection for CopyOnWritePointer? If we
25// can be assured that no other thread will interrupt while a write pointer is
26// held, we don't need thread protection.
27
28// Nowadays, this is the same thing as asking if HAVE_THREADS is defined.
29// Maybe we'll just replace COW_THREADED with HAVE_THREADS in the future.
30#ifdef HAVE_THREADS
31 #define COW_THREADED 1
32#else
33 #undef COW_THREADED
34#endif
35
36/**
37 * This base class provides basic reference counting, but also can be used
38 * with a CopyOnWritePointer to provide get_read_pointer() and
39 * get_write_pointer().
40 */
41class EXPCL_PANDA_PUTIL CopyOnWriteObject : public CachedTypedWritableReferenceCount {
42public:
43 INLINE CopyOnWriteObject();
44 INLINE CopyOnWriteObject(const CopyOnWriteObject &copy);
45 INLINE void operator = (const CopyOnWriteObject &copy);
46
47PUBLISHED:
48#ifdef COW_THREADED
49 virtual bool unref() const;
50 INLINE void cache_ref() const;
51 INLINE bool cache_unref() const;
52
53public:
54 void cache_ref_only() const;
55#endif // COW_THREADED
56
57protected:
58 virtual PT(CopyOnWriteObject) make_cow_copy()=0;
59
60private:
61#ifdef COW_THREADED
62 enum LockStatus {
63 LS_unlocked,
64 LS_locked_read,
65 LS_locked_write,
66 };
67 Mutex _lock_mutex;
68 ConditionVar _lock_cvar;
69 LockStatus _lock_status;
70 Thread *_locking_thread;
71#endif // COW_THREADED
72
73public:
74 virtual TypeHandle get_type() const {
75 return get_class_type();
76 }
77 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
78
79PUBLISHED:
80 static TypeHandle get_class_type() {
81 return _type_handle;
82 }
83
84public:
85 static void init_type() {
86 CachedTypedWritableReferenceCount::init_type();
87 register_type(_type_handle, "CopyOnWriteObject",
88 CachedTypedWritableReferenceCount::get_class_type());
89 }
90
91private:
92 static TypeHandle _type_handle;
93
94 friend class CopyOnWritePointer;
95};
96
97/**
98 * This is similar to RefCountObj, but it implements a CopyOnWriteObject
99 * inheritance instead of a ReferenceCount inheritance.
100 */
101template<class Base>
102class CopyOnWriteObj : public CopyOnWriteObject, public Base {
103public:
104 INLINE CopyOnWriteObj();
105 INLINE CopyOnWriteObj(const Base &copy);
106 INLINE CopyOnWriteObj(const CopyOnWriteObj<Base> &copy);
107 ALLOC_DELETED_CHAIN(CopyOnWriteObj<Base>);
108
109protected:
110 virtual PT(CopyOnWriteObject) make_cow_copy();
111
112public:
113 virtual TypeHandle get_type() const {
114 return get_class_type();
115 }
116 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
117
118PUBLISHED:
119 static TypeHandle get_class_type() {
120 return _type_handle;
121 }
122
123public:
124 static void init_type();
125
126private:
127 static TypeHandle _type_handle;
128};
129
130/**
131 * For objects (e.g. pvectors) whose constructor takes a single parameter.
132 */
133template<class Base, class Param1>
134class CopyOnWriteObj1 : public CopyOnWriteObject, public Base {
135public:
136 INLINE CopyOnWriteObj1(Param1 p1);
137 INLINE CopyOnWriteObj1(const Base &copy);
139
141 ALLOC_DELETED_CHAIN(ThisClass)
142
143protected:
144 virtual PT(CopyOnWriteObject) make_cow_copy();
145
146public:
147 virtual TypeHandle get_type() const {
148 return get_class_type();
149 }
150 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
151
152PUBLISHED:
153 static TypeHandle get_class_type() {
154 return _type_handle;
155 }
156
157public:
158 static void init_type();
159
160private:
161 static TypeHandle _type_handle;
162};
163
164// We can safely redefine this as a no-op.
165template<>
167
168#include "copyOnWriteObject.I"
169
170#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a special extension to ReferenceCount that includes dual reference counts: the standard refer...
void cache_ref() const
Explicitly increments the cache reference count and the normal reference count simultaneously.
bool cache_unref() const
Explicitly decrements the cache reference count and the normal reference count simultaneously.
void cache_ref_only() const
Decrements the cache reference count without affecting the normal reference count.
A condition variable, usually used to communicate information about changing state to a thread that i...
For objects (e.g.
This is similar to RefCountObj, but it implements a CopyOnWriteObject inheritance instead of a Refere...
This base class provides basic reference counting, but also can be used with a CopyOnWritePointer to ...
This safely stores the primary, owned pointer to a CopyOnWriteObject.
A standard mutex, or mutual exclusion lock.
Definition pmutex.h:40
This is the base class for PointerTo and ConstPointerTo.
virtual bool unref() const
Explicitly decrements the reference count.
A thread; that is, a lightweight process.
Definition thread.h:46
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...