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