1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #include "OSGConfig.h"
47 #include "OSGBarrier.h"
48 #include "OSGBaseFunctions.h"
50 #include "OSGThreadManager.h"
54 //---------------------------------------------------------------------------
56 //---------------------------------------------------------------------------
59 /*--------------------------- Constructors --------------------------------*/
61 BarrierCommonBase::BarrierCommonBase(const Char8
*szName
,
71 /*---------------------------- Destructor ---------------------------------*/
73 BarrierCommonBase::~BarrierCommonBase(void)
80 #if defined (OSG_USE_PTHREADS)
82 //---------------------------------------------------------------------------
84 //---------------------------------------------------------------------------
86 /*--------------------------- Constructors --------------------------------*/
88 PThreadBarrierBase::PThreadBarrierBase(const Char8
*szName
,
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
);
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 //---------------------------------------------------------------------------
138 //---------------------------------------------------------------------------
140 /*--------------------------- Constructors --------------------------------*/
142 SprocBarrierBase::SprocBarrierBase(const Char8
*szName
,
144 Inherited(szName
, uiId
),
150 /*---------------------------- Destructor ---------------------------------*/
152 SprocBarrierBase::~SprocBarrierBase(void)
156 /*--------------------------- Construction --------------------------------*/
158 bool SprocBarrierBase::init(void)
160 ThreadManager
*pThreadManager
= ThreadManager::the();
162 if(pThreadManager
== NULL
)
165 if(pThreadManager
->getArena() == NULL
)
168 _pBarrier
= new_barrier(pThreadManager
->getArena());
170 if(_pBarrier
== NULL
)
173 init_barrier(_pBarrier
);
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 //---------------------------------------------------------------------------
196 //---------------------------------------------------------------------------
198 /*--------------------------- Constructors --------------------------------*/
200 WinThreadBarrierBase::WinThreadBarrierBase(const Char8
*szName
,
203 Inherited (szName
, uiId
, bGlobal
),
210 _pBarrierSema
[0] = NULL
;
211 _pBarrierSema
[1] = NULL
;
214 /*---------------------------- Destructor ---------------------------------*/
216 WinThreadBarrierBase::~WinThreadBarrierBase(void)
220 /*--------------------------- Construction --------------------------------*/
222 bool WinThreadBarrierBase::init(void)
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
239 fprintf(stderr
, "Create mutex1 failed\n");
245 sprintf(pTmp
, "%sS1", _szName
);
248 _pBarrierSema
[0] = CreateSemaphore(NULL
, 0, 42, pTmp
);
250 if(_pBarrierSema
[0] == NULL
)
252 CloseHandle(_pMutex1
);
256 fprintf(stderr
, "Create semaphore 1 failed\n");
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]);
273 _pBarrierSema
[0] = NULL
;
275 fprintf(stderr
, "Create semaphore 2 failed\n");
284 /*---------------------------- Destruction --------------------------------*/
286 void WinThreadBarrierBase::shutdown(void)
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 //---------------------------------------------------------------------------
305 //---------------------------------------------------------------------------
307 MPBarrierType
Barrier::_type("OSGBarrier", "OSGMPBase", &Barrier::create
);
310 /*--------------------------- Constructors --------------------------------*/
312 Barrier::Barrier(const Char8
*szName
,
315 Inherited(szName
, uiId
, bGlobal
)
319 /*---------------------------- Destructor ---------------------------------*/
321 Barrier::~Barrier(void)
325 ThreadManager::the()->remove(this);
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
,
349 Barrier
*returnValue
= NULL
;
351 returnValue
= new Barrier(szName
, uiId
, bGlobal
);
353 if(returnValue
->init() == false)