Panda3D
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  */
41 class EXPCL_PANDA_PUTIL CopyOnWriteObject : public CachedTypedWritableReferenceCount {
42 public:
43  INLINE CopyOnWriteObject();
44  INLINE CopyOnWriteObject(const CopyOnWriteObject &copy);
45  INLINE void operator = (const CopyOnWriteObject &copy);
46 
47 PUBLISHED:
48 #ifdef COW_THREADED
49  virtual bool unref() const;
50  INLINE void cache_ref() const;
51  INLINE bool cache_unref() const;
52 
53 public:
54  void cache_ref_only() const;
55 #endif // COW_THREADED
56 
57 protected:
58  virtual PT(CopyOnWriteObject) make_cow_copy()=0;
59 
60 private:
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 
73 public:
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 
79 PUBLISHED:
80  static TypeHandle get_class_type() {
81  return _type_handle;
82  }
83 
84 public:
85  static void init_type() {
86  CachedTypedWritableReferenceCount::init_type();
87  register_type(_type_handle, "CopyOnWriteObject",
88  CachedTypedWritableReferenceCount::get_class_type());
89  }
90 
91 private:
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  */
101 template<class Base>
102 class CopyOnWriteObj : public CopyOnWriteObject, public Base {
103 public:
104  INLINE CopyOnWriteObj();
105  INLINE CopyOnWriteObj(const Base &copy);
106  INLINE CopyOnWriteObj(const CopyOnWriteObj<Base> &copy);
107  ALLOC_DELETED_CHAIN(CopyOnWriteObj<Base>);
108 
109 protected:
110  virtual PT(CopyOnWriteObject) make_cow_copy();
111 
112 public:
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 
118 PUBLISHED:
119  static TypeHandle get_class_type() {
120  return _type_handle;
121  }
122 
123 public:
124  static void init_type();
125 
126 private:
127  static TypeHandle _type_handle;
128 };
129 
130 /**
131  * For objects (e.g. pvectors) whose constructor takes a single parameter.
132  */
133 template<class Base, class Param1>
134 class CopyOnWriteObj1 : public CopyOnWriteObject, public Base {
135 public:
136  INLINE CopyOnWriteObj1(Param1 p1);
137  INLINE CopyOnWriteObj1(const Base &copy);
139 
141  ALLOC_DELETED_CHAIN(ThisClass)
142 
143 protected:
144  virtual PT(CopyOnWriteObject) make_cow_copy();
145 
146 public:
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 
152 PUBLISHED:
153  static TypeHandle get_class_type() {
154  return _type_handle;
155  }
156 
157 public:
158  static void init_type();
159 
160 private:
161  static TypeHandle _type_handle;
162 };
163 
164 // We can safely redefine this as a no-op.
165 template<>
166 INLINE void PointerToBase<CopyOnWriteObject>::update_type(To *ptr) {}
167 
168 #include "copyOnWriteObject.I"
169 
170 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
For objects (e.g.
This is a special extension to ReferenceCount that includes dual reference counts: the standard refer...
This is the base class for PointerTo and ConstPointerTo.
Definition: pointerToBase.h:29
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:38
A condition variable, usually used to communicate information about changing state to a thread that i...
Definition: conditionVar.h:41
void cache_ref_only() const
Decrements the cache reference count without affecting the normal reference count.
bool cache_unref() const
Explicitly decrements the cache reference count and the normal reference count simultaneously.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void cache_ref() const
Explicitly increments the cache reference count and the normal reference count simultaneously.
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 thread; that is, a lightweight process.
Definition: thread.h:46
This is similar to RefCountObj, but it implements a CopyOnWriteObject inheritance instead of a Refere...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual bool unref() const
Explicitly decrements the reference count.