changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Threading / OSGSemaphore.cpp
blob3d5a4b414a767b6ca45cc1dc554521936a8562d5
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2003 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 <iostream>
46 #include "OSGSemaphore.h"
48 #include "OSGBaseFunctions.h"
50 #include "OSGThreadManager.h"
52 #include "OSGLog.h"
54 OSG_BEGIN_NAMESPACE
56 //---------------------------------------------------------------------------
57 // Class
58 //---------------------------------------------------------------------------
60 /*--------------------------- Constructors --------------------------------*/
62 SemaphoreCommonBase::SemaphoreCommonBase(void) :
63 Inherited (NULL, true),
64 _uiSemaphoreId(0 )
68 SemaphoreCommonBase::SemaphoreCommonBase(const Char8 *szName,
69 UInt32 uiId,
70 bool bGlobal) :
71 Inherited (szName,
72 bGlobal),
73 _uiSemaphoreId(uiId )
77 /*---------------------------- Destructor ---------------------------------*/
79 SemaphoreCommonBase::~SemaphoreCommonBase(void)
85 #if defined (OSG_USE_PTHREADS)
87 //---------------------------------------------------------------------------
88 // Class
89 //---------------------------------------------------------------------------
91 /*--------------------------- Constructors --------------------------------*/
93 PThreadSemaphoreBase::PThreadSemaphoreBase(void):
94 Inherited (),
95 _pLowLevelSemaphore()
99 PThreadSemaphoreBase::PThreadSemaphoreBase(const Char8 *szName,
100 UInt32 uiId,
101 bool bGlobal) :
102 Inherited (szName, uiId, bGlobal),
103 _pLowLevelSemaphore( )
107 /*---------------------------- Destructor ---------------------------------*/
109 PThreadSemaphoreBase::~PThreadSemaphoreBase(void)
113 /*--------------------------- Construction --------------------------------*/
115 bool PThreadSemaphoreBase::init(void)
117 sem_init(&(_pLowLevelSemaphore), 0, 0);
119 return true;
122 /*--------------------------- Destruction ---------------------------------*/
124 void PThreadSemaphoreBase::shutdown(void)
126 sem_destroy(&(_pLowLevelSemaphore));
130 #endif /* OSG_USE_PTHREADS */
135 #if defined (OSG_USE_SPROC)
137 //---------------------------------------------------------------------------
138 // Class
139 //---------------------------------------------------------------------------
141 /*--------------------------- Constructors --------------------------------*/
143 SprocSemaphoreBase::SprocSemaphoreBase(void):
144 Inherited ( ),
145 _pLowLevelSema(NULL)
149 SprocSemaphoreBase::SprocSemaphoreBase(const Char8 *szName,
150 UInt32 uiId,
151 bool bGlobal) :
152 Inherited (szName, uiId, bGlobal),
153 _pLowLevelSema(NULL )
157 /*---------------------------- Destructor ---------------------------------*/
159 SprocSemaphoreBase::~SprocSemaphoreBase(void)
163 /*--------------------------- Construction --------------------------------*/
165 bool SprocSemaphoreBase::init(void)
167 ThreadManager *pThreadManager = ThreadManager::the();
169 if(pThreadManager == NULL)
170 return false;
172 if(pThreadManager->getArena() == NULL)
173 return false;
175 _pLowLevelSema = usnewsema(pThreadManager->getArena(), 1);
177 if(_pLowLevelSema == NULL)
178 return false;
180 usinitsema(_pLowLevelSema, 1);
181 usctlsema (_pLowLevelSema, CS_RECURSIVEON, NULL);
183 return true;
186 /*--------------------------- Destruction ---------------------------------*/
188 void SprocSemaphoreBase::shutdown(void)
190 ThreadManager *pThreadManager = ThreadManager::the();
192 if(pThreadManager == NULL)
193 return;
195 if(pThreadManager->getArena() == NULL)
196 return;
198 if(_pLowLevelSema != NULL)
200 usfreesema(_pLowLevelSema, pThreadManager->getArena());
202 _pLowLevelSema = NULL;
206 /*----------------------------- Semaphore ---------------------------------*/
208 #endif /* OSG_USE_SPROC */
212 #if defined (OSG_USE_WINTHREADS)
214 //---------------------------------------------------------------------------
215 // Class
216 //---------------------------------------------------------------------------
218 /*--------------------------- Constructors --------------------------------*/
220 WinThreadSemaphoreBase::WinThreadSemaphoreBase(void) :
221 Inherited ( ),
222 _pSemaphore(NULL)
226 WinThreadSemaphoreBase::WinThreadSemaphoreBase(const Char8 *szName,
227 UInt32 uiId,
228 bool bGlobal) :
229 Inherited (szName, uiId, bGlobal),
230 _pSemaphore(NULL )
234 /*---------------------------- Destructor ---------------------------------*/
236 WinThreadSemaphoreBase::~WinThreadSemaphoreBase(void)
240 /*-------------------------- Construction ---------------------------------*/
242 bool WinThreadSemaphoreBase::init(void)
244 _pSemaphore = CreateSemaphore( NULL, // no security attributes
245 0, // initially not owned
246 1024,
247 _szName); // name of mutex
249 if(_pSemaphore == NULL)
251 return false;
254 return true;
257 /*-------------------------- Destruction ----------------------------------*/
259 void WinThreadSemaphoreBase::shutdown(void)
261 if(_pSemaphore != NULL)
263 CloseHandle(_pSemaphore);
264 _pSemaphore = NULL;
268 #endif /* OSG_USE_WINTHREADS */
272 //---------------------------------------------------------------------------
273 // Class
274 //---------------------------------------------------------------------------
276 MPSemaphoreType Semaphore::_type( "OSGSemaphore",
277 "OSGMPBase",
278 &Semaphore::create);
280 /*------------------------------- Get -------------------------------------*/
282 Semaphore::ObjTransitPtr Semaphore::get(const Char8 *szName, bool bGlobal)
284 return ThreadManager::the()->getSemaphore(szName, bGlobal, "OSGSemaphore");
287 Semaphore *Semaphore::find(const Char8 *szName)
289 return ThreadManager::the()->findSemaphore(szName);
293 /*------------------------------ Create -----------------------------------*/
295 Semaphore *Semaphore::create(const Char8 *szName, UInt32 uiId, bool bGlobal)
297 Semaphore *returnValue = NULL;
299 returnValue = new Semaphore(szName, uiId, bGlobal);
301 if(returnValue->init() == false)
303 delete returnValue;
304 returnValue = NULL;
307 return returnValue;
310 /*--------------------------- Constructors --------------------------------*/
312 Semaphore::Semaphore(void) :
313 Inherited()
317 Semaphore::Semaphore(const Char8 *szName, UInt32 uiId, bool bGlobal) :
318 Inherited(szName, uiId, bGlobal)
322 /*---------------------------- Destructor ---------------------------------*/
324 Semaphore::~Semaphore(void)
326 _bGlobal = false;
328 ThreadManager::the()->remove(this);
330 shutdown();
334 OSG_END_NAMESPACE