1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2003 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 \*---------------------------------------------------------------------------*/
42 #include "OSGConfig.h"
46 #include "OSGSemaphore.h"
48 #include "OSGBaseFunctions.h"
50 #include "OSGThreadManager.h"
56 //---------------------------------------------------------------------------
58 //---------------------------------------------------------------------------
60 /*--------------------------- Constructors --------------------------------*/
62 SemaphoreCommonBase::SemaphoreCommonBase(void) :
63 Inherited (NULL
, true),
68 SemaphoreCommonBase::SemaphoreCommonBase(const Char8
*szName
,
77 /*---------------------------- Destructor ---------------------------------*/
79 SemaphoreCommonBase::~SemaphoreCommonBase(void)
85 #if defined (OSG_USE_PTHREADS)
87 //---------------------------------------------------------------------------
89 //---------------------------------------------------------------------------
91 /*--------------------------- Constructors --------------------------------*/
93 PThreadSemaphoreBase::PThreadSemaphoreBase(void):
99 PThreadSemaphoreBase::PThreadSemaphoreBase(const Char8
*szName
,
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);
122 /*--------------------------- Destruction ---------------------------------*/
124 void PThreadSemaphoreBase::shutdown(void)
126 sem_destroy(&(_pLowLevelSemaphore
));
130 #endif /* OSG_USE_PTHREADS */
135 #if defined (OSG_USE_SPROC)
137 //---------------------------------------------------------------------------
139 //---------------------------------------------------------------------------
141 /*--------------------------- Constructors --------------------------------*/
143 SprocSemaphoreBase::SprocSemaphoreBase(void):
149 SprocSemaphoreBase::SprocSemaphoreBase(const Char8
*szName
,
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
)
172 if(pThreadManager
->getArena() == NULL
)
175 _pLowLevelSema
= usnewsema(pThreadManager
->getArena(), 1);
177 if(_pLowLevelSema
== NULL
)
180 usinitsema(_pLowLevelSema
, 1);
181 usctlsema (_pLowLevelSema
, CS_RECURSIVEON
, NULL
);
186 /*--------------------------- Destruction ---------------------------------*/
188 void SprocSemaphoreBase::shutdown(void)
190 ThreadManager
*pThreadManager
= ThreadManager::the();
192 if(pThreadManager
== NULL
)
195 if(pThreadManager
->getArena() == NULL
)
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 //---------------------------------------------------------------------------
216 //---------------------------------------------------------------------------
218 /*--------------------------- Constructors --------------------------------*/
220 WinThreadSemaphoreBase::WinThreadSemaphoreBase(void) :
226 WinThreadSemaphoreBase::WinThreadSemaphoreBase(const Char8
*szName
,
229 Inherited (szName
, uiId
, bGlobal
),
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
247 _szName
); // name of mutex
249 if(_pSemaphore
== NULL
)
257 /*-------------------------- Destruction ----------------------------------*/
259 void WinThreadSemaphoreBase::shutdown(void)
261 if(_pSemaphore
!= NULL
)
263 CloseHandle(_pSemaphore
);
268 #endif /* OSG_USE_WINTHREADS */
272 //---------------------------------------------------------------------------
274 //---------------------------------------------------------------------------
276 MPSemaphoreType
Semaphore::_type( "OSGSemaphore",
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)
310 /*--------------------------- Constructors --------------------------------*/
312 Semaphore::Semaphore(void) :
317 Semaphore::Semaphore(const Char8
*szName
, UInt32 uiId
, bool bGlobal
) :
318 Inherited(szName
, uiId
, bGlobal
)
322 /*---------------------------- Destructor ---------------------------------*/
324 Semaphore::~Semaphore(void)
328 ThreadManager::the()->remove(this);