merge the formfield patch from ooo-build
[ooovba.git] / vos / source / thread.cxx
blob4777913d3c2e504d74fb811a5f60840e57a8e643
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: thread.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/time.h>
32 #include <vos/diagnose.hxx>
33 #include <vos/object.hxx>
34 #include <vos/thread.hxx>
36 using namespace vos;
38 void vos::threadWorkerFunction_impl(void * pthis)
40 NAMESPACE_VOS(OThread)* pThis= (NAMESPACE_VOS(OThread)*)pthis;
42 // call Handler-Function of OThread-derived class
43 pThis->run();
45 // if not already terminating, by a kill do normal shutdown
46 if (! pThis->m_bTerminating)
48 pThis->m_bTerminating = sal_True;
50 pThis->onTerminated(); // could e.g. delete this
54 /////////////////////////////////////////////////////////////////////////////
56 // Thread class
59 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OThread, vos),
60 VOS_NAMESPACE(OThread, vos),
61 VOS_NAMESPACE(OObject, vos), 0);
63 OThread::OThread()
65 m_hThread = 0;
66 m_bTerminating = sal_False;
67 m_aCondition = osl_createCondition();
70 OThread::~OThread()
72 if (m_hThread != 0)
74 osl_destroyThread(m_hThread);
77 osl_destroyCondition( m_aCondition );
80 sal_Bool OThread::create()
82 VOS_ASSERT(m_hThread == 0); // only one running thread per instance
84 m_hThread = osl_createSuspendedThread(
85 threadWorkerFunction_impl, (void*)this);
86 if (m_hThread)
87 osl_resumeThread(m_hThread);
89 return m_hThread != 0;
92 sal_Bool OThread::createSuspended()
94 VOS_ASSERT(m_hThread == 0); // only one running thread per instance
96 m_hThread= osl_createSuspendedThread(threadWorkerFunction_impl, (void*)this);
97 return m_hThread != 0;
100 void OThread::suspend()
102 VOS_ASSERT(m_hThread != 0); // use only on running thread
104 osl_suspendThread(m_hThread);
107 void OThread::resume()
109 VOS_ASSERT(m_hThread != 0); // use only on running thread
111 osl_resumeThread(m_hThread);
114 sal_Bool OThread::isRunning()
116 return m_hThread != 0 && osl_isThreadRunning(m_hThread);
119 OThread::TThreadIdentifier OThread::getIdentifier() const
121 return (TThreadIdentifier)osl_getThreadIdentifier(m_hThread);
124 OThread::TThreadIdentifier OThread::getCurrentIdentifier()
126 return (TThreadIdentifier)osl_getThreadIdentifier(0);
129 void OThread::join()
131 if (m_hThread) {
132 VOS_ASSERT(getCurrentIdentifier() != getIdentifier());
133 osl_joinWithThread(m_hThread);
137 OThread::TThreadSleep OThread::sleep(const TimeValue& Delay)
139 TThreadSleep eRet;
141 switch( osl_waitCondition( m_aCondition, &Delay ) )
143 case osl_cond_result_ok:
144 eRet = TSleep_Normal;
145 break;
147 case osl_cond_result_timeout:
148 eRet = TSleep_Cancel;
149 break;
151 default:
152 eRet = TSleep_Error;
153 break;
156 return eRet;
159 void OThread::wait(const TimeValue& Delay) {
160 osl_waitThread(&Delay);
163 sal_Bool OThread::awake()
165 osl_setCondition( m_aCondition );
166 return osl_resetCondition( m_aCondition );
169 void OThread::terminate()
171 osl_terminateThread(m_hThread);
174 sal_Bool OThread::schedule() {
175 return osl_scheduleThread(m_hThread);
178 void OThread::kill()
180 if (osl_isThreadRunning(m_hThread))
182 // flag we are shutting down
183 m_bTerminating = sal_True;
185 terminate();
186 join();
190 void OThread::setPriority(OThread::TThreadPriority Priority)
192 osl_setThreadPriority(m_hThread, (oslThreadPriority)Priority);
195 OThread::TThreadPriority OThread::getPriority()
197 return (TThreadPriority)osl_getThreadPriority(m_hThread);
201 void OThread::yield()
203 osl_yieldThread();
206 void OThread::onTerminated()
210 /////////////////////////////////////////////////////////////////////////////
212 // ThreadData class
215 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OThreadData, vos),
216 VOS_NAMESPACE(OThreadData, vos),
217 VOS_NAMESPACE(OObject, vos), 0);
219 OThreadData::OThreadData( oslThreadKeyCallbackFunction pCallback )
221 m_hKey = osl_createThreadKey( pCallback );
222 VOS_VERIFY(m_hKey);
225 OThreadData::~OThreadData()
227 osl_destroyThreadKey(m_hKey);
230 sal_Bool OThreadData::setData(void *pData)
232 VOS_ASSERT(m_hKey != 0);
234 return (osl_setThreadKeyData(m_hKey, pData));
237 void *OThreadData::getData()
239 VOS_ASSERT(m_hKey != 0);
241 return (osl_getThreadKeyData(m_hKey));