Panda3D
cycleDataWriter.I
1 // Filename: cycleDataWriter.I
2 // Created by: drose (21Feb02)
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 CPPPARSER
16 
17 #ifdef DO_PIPELINING
18 // This is the implementation for full support of pipelining (as well
19 // as the sanity-check only implementation).
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: CycleDataWriter::Constructor (full)
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 template<class CycleDataType>
28 CycleDataWriter(PipelineCycler<CycleDataType> &cycler, Thread *current_thread) :
29  _cycler(&cycler),
30  _current_thread(current_thread)
31 {
32  _pointer = _cycler->write(_current_thread);
33  nassertv(_pointer != (CycleDataType *)NULL);
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: CycleDataWriter::Constructor (full)
38 // Access: Public
39 // Description: This two-parameter constructor, with a bool parameter
40 // for the second parameter, automatically propagates
41 // the CycleData pointer upstream from the current
42 // stage, either stopping at the first pointer
43 // encountered that's different, going or all the way to
44 // stage 0, according to force_to_0. See
45 // PipelineCycler::write_upstream().
46 ////////////////////////////////////////////////////////////////////
47 template<class CycleDataType>
49 CycleDataWriter(PipelineCycler<CycleDataType> &cycler, bool force_to_0,
50  Thread *current_thread) :
51  _cycler(&cycler),
52  _current_thread(current_thread)
53 {
54  _pointer = _cycler->write_upstream(force_to_0, _current_thread);
55  nassertv(_pointer != (CycleDataType *)NULL);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: CycleDataWriter::Constructor (full)
60 // Access: Public
61 // Description: This special constructor "steals" a reference count
62 // from the already-locked cdata object. It does not
63 // increment the lock count of this object, but will
64 // release it when the destructor is called.
65 //
66 // This is designed for special functions that return an
67 // already-locked cdata object and expect the caller to
68 // unlock it.
69 ////////////////////////////////////////////////////////////////////
70 template<class CycleDataType>
72 CycleDataWriter(PipelineCycler<CycleDataType> &cycler, CycleDataType *locked_cdata,
73  Thread *current_thread) :
74  _cycler(&cycler),
75  _current_thread(current_thread)
76 {
77  _pointer = locked_cdata;
78  nassertv(_pointer != (CycleDataType *)NULL);
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: CycleDataWriter::Copy Constructor (full)
83 // Access: Public
84 // Description:
85 ////////////////////////////////////////////////////////////////////
86 template<class CycleDataType>
89  _cycler(copy._cycler),
90  _current_thread(copy._current_thread),
91  _pointer(copy._pointer)
92 {
93  nassertv(_pointer != (CycleDataType *)NULL);
94  _cycler->increment_write(_pointer);
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: CycleDataWriter::Copy Assignment (full)
99 // Access: Public
100 // Description:
101 ////////////////////////////////////////////////////////////////////
102 template<class CycleDataType>
105  nassertv(_pointer == (CycleDataType *)NULL);
106  nassertv(_current_thread == copy._current_thread);
107 
108  _cycler = copy._cycler;
109  _pointer = copy._pointer;
110 
111  nassertv(_pointer != (CycleDataType *)NULL);
112  _cycler->increment_write(_pointer);
113 }
114 
115 ////////////////////////////////////////////////////////////////////
116 // Function: CycleDataWriter::Constructor (full)
117 // Access: Public
118 // Description: This flavor of the constructor elevates the pointer
119 // from the CycleDataLockedReader from a read to a write
120 // pointer (and invalidates the reader).
121 ////////////////////////////////////////////////////////////////////
122 template<class CycleDataType>
126  _cycler(&cycler),
127  _current_thread(take_from.get_current_thread())
128 {
129  _pointer = _cycler->elevate_read(take_from.take_pointer(), _current_thread);
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: CycleDataWriter::Constructor (full)
134 // Access: Public
135 // Description: This flavor of the constructor elevates the pointer
136 // from the CycleDataLockedReader from a read to a write
137 // pointer (and invalidates the reader). It also
138 // propagates the pointer back upstream; see
139 // PipelineCycler::write_upstream().
140 ////////////////////////////////////////////////////////////////////
141 template<class CycleDataType>
145  bool force_to_0) :
146  _cycler(&cycler),
147  _current_thread(take_from.get_current_thread())
148 {
149  _pointer = _cycler->elevate_read_upstream(take_from.take_pointer(),
150  force_to_0, _current_thread);
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: CycleDataWriter::Destructor (full)
155 // Access: Public
156 // Description:
157 ////////////////////////////////////////////////////////////////////
158 template<class CycleDataType>
161  if (_pointer != (CycleDataType *)NULL) {
162  _cycler->release_write(_pointer);
163  }
164 }
165 
166 ////////////////////////////////////////////////////////////////////
167 // Function: CycleDataWriter::operator -> (full)
168 // Access: Public
169 // Description: This provides an indirect member access to the actual
170 // CycleData data.
171 ////////////////////////////////////////////////////////////////////
172 template<class CycleDataType>
173 INLINE CycleDataType *CycleDataWriter<CycleDataType>::
174 operator -> () {
175  nassertr(_pointer != (CycleDataType *)NULL, _cycler->cheat());
176  return _pointer;
177 }
178 
179 ////////////////////////////////////////////////////////////////////
180 // Function: CycleDataWriter::operator -> (full)
181 // Access: Public
182 // Description: This provides an indirect member access to the actual
183 // CycleData data.
184 ////////////////////////////////////////////////////////////////////
185 template<class CycleDataType>
186 INLINE const CycleDataType *CycleDataWriter<CycleDataType>::
187 operator -> () const {
188  nassertr(_pointer != (CycleDataType *)NULL, _cycler->cheat());
189  return _pointer;
190 }
191 
192 ////////////////////////////////////////////////////////////////////
193 // Function: CycleDataWriter::Typecast pointer (full)
194 // Access: Public
195 // Description: This allows the CycleDataWriter to be passed to any
196 // function that expects a CycleDataType pointer.
197 ////////////////////////////////////////////////////////////////////
198 template<class CycleDataType>
200 operator CycleDataType * () {
201  nassertr(_pointer != (CycleDataType *)NULL, _cycler->cheat());
202  return _pointer;
203 }
204 
205 ////////////////////////////////////////////////////////////////////
206 // Function: CycleDataWriter::get_current_thread (full)
207 // Access: Public
208 // Description: Returns the Thread pointer of the currently-executing
209 // thread, as passed to the constructor of this object.
210 ////////////////////////////////////////////////////////////////////
211 template<class CycleDataType>
213 get_current_thread() const {
214  return _current_thread;
215 }
216 
217 #else // !DO_PIPELINING
218 // This is the trivial, do-nothing implementation.
219 
220 ////////////////////////////////////////////////////////////////////
221 // Function: CycleDataWriter::Constructor (trivial)
222 // Access: Public
223 // Description:
224 ////////////////////////////////////////////////////////////////////
225 template<class CycleDataType>
228  _pointer = cycler.cheat();
229 }
230 
231 ////////////////////////////////////////////////////////////////////
232 // Function: CycleDataWriter::Constructor (trivial)
233 // Access: Public
234 // Description:
235 ////////////////////////////////////////////////////////////////////
236 template<class CycleDataType>
239  _pointer = cycler.cheat();
240 }
241 
242 ////////////////////////////////////////////////////////////////////
243 // Function: CycleDataWriter::Constructor (trivial)
244 // Access: Public
245 // Description:
246 ////////////////////////////////////////////////////////////////////
247 template<class CycleDataType>
249 CycleDataWriter(PipelineCycler<CycleDataType> &, CycleDataType *locked_cdata,
250  Thread *) {
251  _pointer = locked_cdata;
252 }
253 
254 ////////////////////////////////////////////////////////////////////
255 // Function: CycleDataWriter::Copy Constructor (trivial)
256 // Access: Public
257 // Description:
258 ////////////////////////////////////////////////////////////////////
259 template<class CycleDataType>
262  _pointer(copy._pointer)
263 {
264 }
265 
266 ////////////////////////////////////////////////////////////////////
267 // Function: CycleDataWriter::Copy Assignment (trivial)
268 // Access: Public
269 // Description:
270 ////////////////////////////////////////////////////////////////////
271 template<class CycleDataType>
274  _pointer = copy._pointer;
275 }
276 
277 ////////////////////////////////////////////////////////////////////
278 // Function: CycleDataWriter::Constructor (trivial)
279 // Access: Public
280 // Description: This flavor of the constructor elevates the pointer
281 // from the CycleDataLockedReader from a read to a write
282 // pointer (and invalidates the reader).
283 ////////////////////////////////////////////////////////////////////
284 template<class CycleDataType>
288  _pointer((CycleDataType *)take_from.take_pointer())
289 {
290 }
291 
292 ////////////////////////////////////////////////////////////////////
293 // Function: CycleDataWriter::Constructor (trivial)
294 // Access: Public
295 // Description: This flavor of the constructor elevates the pointer
296 // from the CycleDataLockedReader from a read to a write
297 // pointer (and invalidates the reader). It also
298 // propagates the pointer back upstream; see
299 // PipelineCycler::write_upstream().
300 ////////////////////////////////////////////////////////////////////
301 template<class CycleDataType>
305  bool force_to_0) :
306  _pointer((CycleDataType *)take_from.take_pointer())
307 {
308 }
309 
310 ////////////////////////////////////////////////////////////////////
311 // Function: CycleDataWriter::Destructor (trivial)
312 // Access: Public
313 // Description:
314 ////////////////////////////////////////////////////////////////////
315 template<class CycleDataType>
318 }
319 
320 ////////////////////////////////////////////////////////////////////
321 // Function: CycleDataWriter::operator -> (trivial)
322 // Access: Public
323 // Description: This provides an indirect member access to the actual
324 // CycleData data.
325 ////////////////////////////////////////////////////////////////////
326 template<class CycleDataType>
327 INLINE CycleDataType *CycleDataWriter<CycleDataType>::
329  return _pointer;
330 }
331 
332 ////////////////////////////////////////////////////////////////////
333 // Function: CycleDataWriter::operator -> (trivial)
334 // Access: Public
335 // Description: This provides an indirect member access to the actual
336 // CycleData data.
337 ////////////////////////////////////////////////////////////////////
338 template<class CycleDataType>
339 INLINE const CycleDataType *CycleDataWriter<CycleDataType>::
340 operator -> () const {
341  return _pointer;
342 }
343 
344 ////////////////////////////////////////////////////////////////////
345 // Function: CycleDataWriter::Typecast pointer (trivial)
346 // Access: Public
347 // Description: This allows the CycleDataWriter to be passed to any
348 // function that expects a CycleDataType pointer.
349 ////////////////////////////////////////////////////////////////////
350 template<class CycleDataType>
352 operator CycleDataType * () {
353  return _pointer;
354 }
355 
356 ////////////////////////////////////////////////////////////////////
357 // Function: CycleDataWriter::get_current_thread (trivial)
358 // Access: Public
359 // Description: Returns the Thread pointer of the currently-executing
360 // thread, as passed to the constructor of this object.
361 ////////////////////////////////////////////////////////////////////
362 template<class CycleDataType>
366 }
367 
368 #endif // DO_PIPELINING
369 #endif // CPPPARSER
This class maintains different copies of a page of data between stages of the graphics pipeline (or a...
CycleDataType * operator->()
This provides an indirect member access to the actual CycleData data.
Thread * get_current_thread() const
Returns the Thread pointer of the currently-executing thread, as passed to the constructor of this ob...
CycleDataType * cheat() const
Returns a pointer without counting it.
static Thread * get_current_thread()
Returns a pointer to the currently-executing Thread object.
Definition: thread.I:145
const CycleDataType * take_pointer()
This is intended to be called only from CycleDataWriter when it elevates the pointer from read to wri...
This template class calls PipelineCycler::read() in the constructor and PipelineCycler::release_read(...
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
A thread; that is, a lightweight process.
Definition: thread.h:51