Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGBarrier.inl

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *           Copyright (C) 2003 by the OpenSG Forum                          *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 OSG_BEGIN_NAMESPACE
00040 
00041 inline
00042 void BarrierCommonBase::setNumWaitFor(UInt32 uiNumWaitFor)
00043 {
00044     _uiNumWaitFor = uiNumWaitFor;
00045 }
00046 
00047 
00048 #if defined (OSG_USE_PTHREADS)
00049 
00050 /*------------------------------- Enter -----------------------------------*/
00051 
00052 inline
00053 void PThreadBarrierBase::enter(void)
00054 {
00055     if(_uiNumWaitFor <= 1)
00056         return;
00057 
00058     pthread_mutex_lock(&(_pLockOne));
00059 
00060     _uiCount++;
00061 
00062     if(_uiCount < _uiNumWaitFor)
00063     {
00064         /* not enough threads are waiting => wait */
00065 
00066         pthread_cond_wait(&(_pWakeupCondition[_uiCurrentCond]), &(_pLockOne));
00067     }
00068     else
00069     {
00070         /* ok, enough threads are waiting
00071            => wake up all waiting threads
00072         */
00073 
00074         pthread_cond_broadcast(&(_pWakeupCondition[_uiCurrentCond]));
00075 
00076         _uiCount       = 0;
00077         _uiCurrentCond = 1 - _uiCurrentCond;
00078     }
00079 
00080     pthread_mutex_unlock(&(_pLockOne));
00081 }
00082 
00083 inline
00084 void PThreadBarrierBase::enter(UInt32 uiNumWaitFor)
00085 {
00086     _uiNumWaitFor = uiNumWaitFor;
00087 
00088     enter();
00089 }
00090 
00091 #endif /* OSG_USE_PTHREADS */
00092 
00093 
00094 
00095 #if defined (OSG_USE_SPROC)
00096 
00097 /*------------------------------ Enter ------------------------------------*/
00098 
00099 inline
00100 void SprocBarrierBase::enter(void)
00101 {
00102     if(_pBarrier != NULL)
00103         barrier(_pBarrier, _uiNumWaitFor);
00104 }
00105 
00106 inline
00107 void SprocBarrierBase::enter(UInt32 uiNumWaitFor)
00108 {
00109     _uiNumWaitFor = uiNumWaitFor;
00110 
00111     enter();
00112 }
00113 
00114 #endif /* OSG_USE_SPROC */
00115 
00116 
00117 
00118 #if defined (OSG_USE_WINTHREADS)
00119 
00120 /*------------------------------ Enter ------------------------------------*/
00121 
00122 inline
00123 void WinThreadBarrierBase::enter(void)
00124 {
00125     WaitForSingleObject(_pMutex1, INFINITE);
00126 
00127     ++_uiNumWaiters;
00128 
00129     if(_uiNumWaiters == _uiNumWaitFor) 
00130     {
00131         --_uiNumWaiters;
00132 
00133         ReleaseMutex(_pMutex1);
00134 
00135         ReleaseSemaphore(_pBarrierSema, _uiNumWaitFor - 1, NULL);
00136 
00137         return;
00138     }
00139     else
00140     {
00141         ReleaseMutex(_pMutex1);
00142 
00143         WaitForSingleObject(_pBarrierSema, INFINITE);
00144         
00145         WaitForSingleObject(_pMutex1, INFINITE);
00146 
00147         --_uiNumWaiters;
00148 
00149         ReleaseMutex(_pMutex1);
00150     }
00151 }
00152 
00153 inline
00154 void WinThreadBarrierBase::enter(UInt32 uiNumWaitFor)
00155 {
00156     _uiNumWaitFor = uiNumWaitFor;
00157 
00158     enter();
00159 }
00160 
00161 #endif /* OSG_USE_WINTHREADS */
00162 
00163 inline
00164 Barrier *Barrier::create(void)
00165 {
00166     return Barrier::get(NULL);
00167 }
00168 
00169 inline
00170 const MPBarrierType &Barrier::getClassType(void)
00171 {
00172     return _type;
00173 }
00174 
00175 /*------------------------------- Enter -----------------------------------*/
00176 
00177 
00178 inline
00179 void Barrier::enter(void)
00180 {
00181     Inherited::enter();
00182 }
00183 
00184 inline
00185 void Barrier::enter(UInt32 uiNumWaitFor)
00186 {
00187     Inherited::enter(uiNumWaitFor);
00188 }
00189 
00190 
00191 OSG_END_NAMESPACE
00192 
00193 #define OSGBARRIER_INLINE_CVSID "@(#)$Id: $"
00194 

Generated on Thu Aug 25 04:01:09 2005 for OpenSG by  doxygen 1.4.3