changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Threading / OSGBaseThread.inl
blob4d7ef177dbe13b7db3de5d34eb83ce30663a0129
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *           Copyright (C) 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 OSG_BEGIN_NAMESPACE
41 inline
42 bool BaseThreadCommonBase::isInitialized(void)
44     return _bInitialized;
48 #if defined (OSG_USE_PTHREADS)
50 /*---------------------------- Blocking -----------------------------------*/
52 inline
53 void BasePThreadBase::block(void)
55     pthread_mutex_lock  (_pBlockMutex);
56     pthread_cond_wait   (_pBlockCond, _pBlockMutex);
57     pthread_mutex_unlock(_pBlockMutex);
60 inline
61 void BasePThreadBase::unblock(void)
63     pthread_cond_broadcast(_pBlockCond);
66 /*------------------------------ Helper -----------------------------------*/
68 inline
69 bool BasePThreadBase::exists(void)
71     return true;
74 inline
75 void BasePThreadBase::terminate(void)
77     if(_pThreadDesc != NULL)
78         pthread_cancel(*_pThreadDesc);
81 inline
82 void BasePThreadBase::kill(void)
84     if(_pThreadDesc != NULL) 
85         pthread_cancel(*_pThreadDesc);
89 #endif /* OSG_USE_PTHREADS */
94 #if defined (OSG_USE_SPROC)
96 /*------------------------------- Get -------------------------------------*/
98 inline
99 BaseThread *BaseSprocBase::getCurrent(void)
101     return ((ProcessData *) PRDA->usr_prda.fill)->_pThread;
104 /*---------------------------- Blocking -----------------------------------*/
106 inline
107 void BaseSprocBase::block(void)
109     blockproc(_pid);
112 inline
113 void BaseSprocBase::unblock(void)
115     unblockproc(_pid);
118 /*------------------------------ Helper -----------------------------------*/
120 inline
121 bool BaseSprocBase::exists(void)
123     bool returnValue = false;
125     returnValue = (prctl(PR_ISBLOCKED, _pid) != -1);
127     return returnValue;
131 inline
132 void BaseSprocBase::terminate(void)
134     ::kill(_pid, SIGTERM);
137 inline
138 void BaseSprocBase::kill(void)
140     ::kill(_pid, SIGKILL);
143 #endif /* OSG_USE_SPROC */
148 #if defined (OSG_USE_WINTHREADS)
150 /*----------------------------- Blocking ----------------------------------*/
152 inline
153 void BaseWinThreadBase::block(void)
155     SuspendThread(_pThreadHandle);
158 inline
159 void BaseWinThreadBase::unblock(void)
161     ResumeThread(_pExternalHandle);
164 /*----------------------------- Helper ------------------------------------*/
166 inline
167 bool BaseWinThreadBase::exists(void)
169     bool  returnValue = false;
170     DWORD rc          = 0;
172     if(BaseWinThreadBase::getCurrentInternal() == this)
173     {
174         GetExitCodeThread( _pThreadHandle,
175                           &rc            );
176     }
177     else
178     {
179         GetExitCodeThread( _pExternalHandle,
180                           &rc              );
181     }
183     returnValue = (rc == STILL_ACTIVE);
185     return returnValue;
189 inline
190 void BaseWinThreadBase::terminate(void)
192     if(BaseWinThreadBase::getCurrentInternal() == this)
193     {
194         TerminateThread(_pThreadHandle,   0);
195     }
196     else
197     {
198         TerminateThread(_pExternalHandle, 0);
199     }
202 inline
203 void BaseWinThreadBase::kill(void)
205     if(BaseWinThreadBase::getCurrentInternal() == this)
206     {
207         TerminateThread(_pThreadHandle,   0);
208     }
209     else
210     {
211         TerminateThread(_pExternalHandle, 0);
212     }
215 #endif /* OSG_USE_WINTHREADS */
218 inline
219 BaseThread::ObjTransitPtr BaseThread::create(void)
221     return BaseThread::get(NULL, false);
224 inline
225 const MPThreadType &BaseThread::getClassType(void)
227     return _type;
230 /*------------------------------- Get -------------------------------------*/
232 inline
233 BaseThread *BaseThread::getCurrent(void)
235     return Inherited::getCurrent();
238 /*------------------------------ Join -------------------------------------*/
240 inline
241 void BaseThread::join(BaseThread *pThread)
243     Inherited::join(pThread);
246 /*------------------------------- Run -------------------------------------*/
249 inline
250 void BaseThread::run(void)
252     Inherited::runFunction(runWorkProc, this);
255 inline
256 bool BaseThread::runFunction(ThreadFuncF  fThreadFunc,
257                              void        *pThreadArg)
259     return Inherited::runFunction(fThreadFunc, pThreadArg);
262 /*----------------------------- Blocking ----------------------------------*/
264 inline
265 void BaseThread::block(void)
267     Inherited::block();
270 inline
271 void BaseThread::unblock(void)
273     Inherited::unblock();
276 /*----------------------------- Helper ------------------------------------*/
278 inline
279 bool BaseThread::exists(void)
281     return Inherited::exists();
284 inline
285 void BaseThread::terminate(void)
287     Inherited::terminate();
290 inline
291 void BaseThread::kill(void)
293     Inherited::kill();
296 OSG_END_NAMESPACE