changed: gcc8 base update
[opensg.git] / Source / Base / Threading / OSGBarrier.cpp
blobc5e7cf5d1e56e38dae86623f61c1373a4b2d4c3e
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 "OSGConfig.h"
41 #include <cstdlib>
42 #include <cstdio>
45 #include <iostream>
47 #include "OSGBarrier.h"
48 #include "OSGBaseFunctions.h"
50 #include "OSGThreadManager.h"
52 OSG_USING_NAMESPACE
54 //---------------------------------------------------------------------------
55 // Class
56 //---------------------------------------------------------------------------
59 /*--------------------------- Constructors --------------------------------*/
61 BarrierCommonBase::BarrierCommonBase(const Char8 *szName,
62 UInt32 uiId,
63 bool bGlobal) :
64 Inherited (szName,
65 bGlobal),
66 _uiBarrierId (uiId ),
67 _uiNumWaitFor( 1)
71 /*---------------------------- Destructor ---------------------------------*/
73 BarrierCommonBase::~BarrierCommonBase(void)
80 #if defined (OSG_USE_PTHREADS)
82 //---------------------------------------------------------------------------
83 // Class
84 //---------------------------------------------------------------------------
86 /*--------------------------- Constructors --------------------------------*/
88 PThreadBarrierBase::PThreadBarrierBase(const Char8 *szName,
89 UInt32 uiId,
90 bool bGlobal) :
91 Inherited (szName,
92 uiId,
93 bGlobal),
95 _pLockOne ( ),
96 _uiCount (0 ),
97 _uiCurrentCond (0 )
101 /*---------------------------- Destructor ---------------------------------*/
103 PThreadBarrierBase::~PThreadBarrierBase(void)
107 /*--------------------------- Construction --------------------------------*/
109 bool PThreadBarrierBase::init(void)
111 pthread_cond_init (&(_pWakeupCondition[0]), NULL);
112 pthread_cond_init (&(_pWakeupCondition[1]), NULL);
113 pthread_mutex_init(&(_pLockOne), NULL);
115 _uiCount = 0;
116 _uiCurrentCond = 0;
118 return true;
121 /*---------------------------- Destruction --------------------------------*/
123 void PThreadBarrierBase::shutdown(void)
125 pthread_cond_destroy (&(_pWakeupCondition[0]));
126 pthread_cond_destroy (&(_pWakeupCondition[1]));
127 pthread_mutex_destroy(&(_pLockOne));
130 #endif /* OSG_USE_PTHREADS */
134 #if defined (OSG_USE_SPROC)
136 //---------------------------------------------------------------------------
137 // Class
138 //---------------------------------------------------------------------------
140 /*--------------------------- Constructors --------------------------------*/
142 SprocBarrierBase::SprocBarrierBase(const Char8 *szName,
143 UInt32 uiId ) :
144 Inherited(szName, uiId),
146 _pBarrier (NULL)
150 /*---------------------------- Destructor ---------------------------------*/
152 SprocBarrierBase::~SprocBarrierBase(void)
156 /*--------------------------- Construction --------------------------------*/
158 bool SprocBarrierBase::init(void)
160 ThreadManager *pThreadManager = ThreadManager::the();
162 if(pThreadManager == NULL)
163 return false;
165 if(pThreadManager->getArena() == NULL)
166 return false;
168 _pBarrier = new_barrier(pThreadManager->getArena());
170 if(_pBarrier == NULL)
171 return false;
173 init_barrier(_pBarrier);
175 return true;
178 /*---------------------------- Destruction --------------------------------*/
180 void SprocBarrierBase::shutdown(void)
182 if(_pBarrier != NULL)
183 free_barrier(_pBarrier);
188 #endif /* OSG_USE_SPROC */
192 #if defined (OSG_USE_WINTHREADS)
194 //---------------------------------------------------------------------------
195 // Class
196 //---------------------------------------------------------------------------
198 /*--------------------------- Constructors --------------------------------*/
200 WinThreadBarrierBase::WinThreadBarrierBase(const Char8 *szName,
201 UInt32 uiId,
202 bool bGlobal) :
203 Inherited (szName, uiId, bGlobal),
205 _pMutex1 (NULL),
206 _uiCount (0 ),
207 _uiCurrentCond(0 )
210 _pBarrierSema[0] = NULL;
211 _pBarrierSema[1] = NULL;
214 /*---------------------------- Destructor ---------------------------------*/
216 WinThreadBarrierBase::~WinThreadBarrierBase(void)
220 /*--------------------------- Construction --------------------------------*/
222 bool WinThreadBarrierBase::init(void)
224 Char8 *pTmp = NULL;
226 if(_szName != NULL)
228 pTmp = new Char8[strlen(_szName) + 5];
230 sprintf(pTmp, "%sM1", _szName);
233 _pMutex1 = CreateMutex(NULL, // no security attributes
234 FALSE, // initially not owned
235 pTmp); // name of mutex
237 if(_pMutex1 == NULL)
239 fprintf(stderr, "Create mutex1 failed\n");
240 return false;
243 if(_szName != NULL)
245 sprintf(pTmp, "%sS1", _szName);
248 _pBarrierSema[0] = CreateSemaphore(NULL, 0, 42, pTmp);
250 if(_pBarrierSema[0] == NULL)
252 CloseHandle(_pMutex1);
254 _pMutex1 = NULL;
256 fprintf(stderr, "Create semaphore 1 failed\n");
257 return false;
260 if(_szName != NULL)
262 sprintf(pTmp, "%sS2", _szName);
265 _pBarrierSema[1] = CreateSemaphore(NULL, 0, 42, pTmp);
267 if(_pBarrierSema[1] == NULL)
269 CloseHandle(_pMutex1 );
270 CloseHandle(_pBarrierSema[0]);
272 _pMutex1 = NULL;
273 _pBarrierSema[0] = NULL;
275 fprintf(stderr, "Create semaphore 2 failed\n");
276 return false;
279 delete [] pTmp;
281 return true;
284 /*---------------------------- Destruction --------------------------------*/
286 void WinThreadBarrierBase::shutdown(void)
288 if(_pMutex1 != NULL)
289 CloseHandle(_pMutex1);
291 if(_pBarrierSema[0] != NULL)
292 CloseHandle(_pBarrierSema[0]);
294 if(_pBarrierSema[1] != NULL)
295 CloseHandle(_pBarrierSema[1]);
298 #endif /* OSG_USE_WINTHREADS */
303 //---------------------------------------------------------------------------
304 // Class
305 //---------------------------------------------------------------------------
307 MPBarrierType Barrier::_type("OSGBarrier", "OSGMPBase", &Barrier::create);
310 /*--------------------------- Constructors --------------------------------*/
312 Barrier::Barrier(const Char8 *szName,
313 UInt32 uiId,
314 bool bGlobal) :
315 Inherited(szName, uiId, bGlobal)
319 /*---------------------------- Destructor ---------------------------------*/
321 Barrier::~Barrier(void)
323 _bGlobal = false;
325 ThreadManager::the()->remove(this);
327 shutdown();
330 /*-------------------------------- Get ------------------------------------*/
332 Barrier::ObjTransitPtr Barrier::get(const Char8 *szName, bool bGlobal)
334 return ThreadManager::the()->getBarrier(szName, bGlobal, "OSGBarrier");
337 Barrier *Barrier::find(const Char8 *szName)
339 return ThreadManager::the()->findBarrier(szName);
343 /*------------------------------ Create -----------------------------------*/
345 Barrier *Barrier::create (const Char8 *szName,
346 UInt32 uiId,
347 bool bGlobal)
349 Barrier *returnValue = NULL;
351 returnValue = new Barrier(szName, uiId, bGlobal);
353 if(returnValue->init() == false)
355 delete returnValue;
356 returnValue = NULL;
359 return returnValue;