changed: gcc8 base update
[opensg.git] / Source / Base / FieldContainer / Threading / OSGHardwareContextTask.cpp
blobd7bb5e72f10a03f72e22f0c427ce1ec7e8e461c3
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #include <cstdlib>
40 #include <cstdio>
42 #include "OSGConfig.h"
44 #include "OSGHardwareContextTask.h"
45 #include "OSGFieldContainer.h"
47 #include "OSGMField.ins"
49 OSG_BEGIN_NAMESPACE
51 /*! \class OSG::HardwareContextTask
52 \ingroup GrpSystemRenderingBackend
55 /*-------------------------------------------------------------------------*/
56 /* Constructors */
58 HardwareContextTask::HardwareContextTask(UInt32 uiTaskType) :
59 Inherited ( ),
60 _uiTypeTask(uiTaskType)
64 /*-------------------------------------------------------------------------*/
65 /* Destructor */
67 HardwareContextTask::~HardwareContextTask(void)
72 //virtual void execute(DrawEnv *pEnv);
74 /*! \class OSG::BlockingTask
75 \ingroup GrpSystemRenderingBackend
78 /*-------------------------------------------------------------------------*/
79 /* Constructors */
81 BlockingTask::BlockingTask(UInt32 uiTaskType) :
82 Inherited (uiTaskType),
83 _bBarrierActive(false ),
84 _pBarrier (NULL )
88 /*-------------------------------------------------------------------------*/
89 /* Destructor */
91 BlockingTask::~BlockingTask(void)
93 _pBarrier = NULL;
96 void BlockingTask::activateBarrier(bool bVal)
98 _bBarrierActive = bVal;
100 if(_bBarrierActive == true && _pBarrier == NULL)
102 _pBarrier = Barrier::get(NULL, false);
103 _pBarrier->setNumWaitFor(2);
107 void BlockingTask::setNumWaitFor(UInt32 uiWaitees)
109 if(_pBarrier != NULL && uiWaitees != 0)
111 _pBarrier->setNumWaitFor(uiWaitees);
115 void BlockingTask::waitForBarrier(void)
117 if(_bBarrierActive == false)
119 return;
122 OSG_ASSERT(_pBarrier != NULL);
124 _pBarrier->enter();
128 /*! \class OSG::CallbackTask
129 \ingroup GrpSystemRenderingBackend
132 CallbackTask::CallbackTask(UInt32 uiType) :
133 Inherited(uiType),
134 _fCallback( )
138 CallbackTask::CallbackTask(const CallbackFunctor &fCallback,
139 UInt32 uiType ) :
140 Inherited (uiType ),
141 _fCallback (fCallback)
145 CallbackTask::~CallbackTask(void)
149 void CallbackTask::execute(HardwareContext *pContext, DrawEnv *pEnv)
151 OSG_ASSERT(_fCallback );
153 switch(_uiTypeTask)
155 case Callback:
157 _fCallback(pContext, pEnv);
159 if(_bBarrierActive == true)
160 _pBarrier->enter();
162 break;
164 default:
165 break;
169 void CallbackTask::setCallback(const CallbackFunctor &fCallback)
171 _fCallback = fCallback;
175 void CallbackTask::dump(UInt32 uiIndent)
177 for(UInt32 i = 0; i < uiIndent; ++i) { fprintf(stderr, " "); }
178 fprintf(stderr, "CallbackTask : ");
180 switch(_uiTypeTask)
182 case Callback:
184 fprintf(stderr, "Callback, barrier active : %d\n",
185 UInt32(_bBarrierActive));
187 break;
189 default:
191 fprintf(stderr, "Unknown\n");
193 break;
200 /*! \class OSG::HardwareContextTask
201 \ingroup GrpSystemRenderingBackend
204 /*-------------------------------------------------------------------------*/
205 /* Constructors */
207 HardwareContextTaskQueue::HardwareContextTaskQueue(void) :
208 _qTaskStore( ),
209 _pStoreLock(NULL),
210 _pStoreSema(NULL)
212 _pStoreLock = Lock ::get(NULL, false);
213 _pStoreSema = Semaphore::get(NULL, false);
216 /*-------------------------------------------------------------------------*/
217 /* Destructor */
219 HardwareContextTaskQueue::~HardwareContextTaskQueue(void)
221 _qTaskStore.clear();
223 _pStoreLock = NULL;
224 _pStoreSema = NULL;
227 void HardwareContextTaskQueue::queueTask(HardwareContextTask *pTask)
229 if(pTask != NULL)
231 _pStoreLock->acquire();
233 _qTaskStore.push_back(pTask);
235 _pStoreLock->release();
237 _pStoreSema->post();
243 void HardwareContextTaskQueue::queueTaskFront(HardwareContextTask *pTask)
245 if(pTask != NULL)
247 _pStoreLock->acquire();
249 _qTaskStore.push_front(pTask);
251 _pStoreLock->release();
253 _pStoreSema->post();
259 HardwareContextTaskTransitPtr HardwareContextTaskQueue::popTask(void)
261 _pStoreSema->wait();
263 _pStoreLock->acquire();
265 OSG_ASSERT(_qTaskStore.size() != 0);
267 HardwareContextTaskTransitPtr returnValue(_qTaskStore.front());
269 _qTaskStore.pop_front();
271 _pStoreLock->release();
273 return returnValue;
276 void HardwareContextTaskQueue::dumpQueue(void)
278 TaskStore::const_iterator qIt = _qTaskStore.begin();
279 TaskStore::const_iterator qEnd = _qTaskStore.end ();
281 fprintf(stderr, "Dumping task queue\n");
282 fprintf(stderr, "------------------\n");
284 for(; qIt != qEnd; ++qIt)
286 (*qIt)->dump(4);
290 void HardwareContextTaskQueue::runAndClearQueue(HardwareContext *pContext,
291 DrawEnv *pEnv )
293 TaskStore::const_iterator qIt = _qTaskStore.begin();
294 TaskStore::const_iterator qEnd = _qTaskStore.end ();
296 for(; qIt != qEnd; ++qIt)
298 (*qIt)->execute(pContext, pEnv);
301 _qTaskStore.clear();
304 DataType FieldTraits<
305 HardwareContextTaskRefPtr>::_type("HardwareContextTaskRefPtr",
306 NULL);
308 OSG_FIELDTRAITS_GETTYPE(HardwareContextTaskRefPtr)
310 OSG_FIELD_DLLEXPORT_DEF1(MField, HardwareContextTaskRefPtr)
313 OSG_END_NAMESPACE