xref: /aoo42x/main/sal/inc/osl/thread.hxx (revision 5f05c480)
1565d668cSAndrew Rist /**************************************************************
288e06183Smseidel  *
3565d668cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4565d668cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5565d668cSAndrew Rist  * distributed with this work for additional information
6565d668cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7565d668cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8565d668cSAndrew Rist  * "License"); you may not use this file except in compliance
9565d668cSAndrew Rist  * with the License.  You may obtain a copy of the License at
1088e06183Smseidel  *
11565d668cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1288e06183Smseidel  *
13565d668cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14565d668cSAndrew Rist  * software distributed under the License is distributed on an
15565d668cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16565d668cSAndrew Rist  * KIND, either express or implied.  See the License for the
17565d668cSAndrew Rist  * specific language governing permissions and limitations
18565d668cSAndrew Rist  * under the License.
1988e06183Smseidel  *
20565d668cSAndrew Rist  *************************************************************/
21565d668cSAndrew Rist 
22565d668cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _THREAD_HXX_
25cdf0e10cSrcweir #define _THREAD_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifdef __cplusplus
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <osl/time.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <osl/diagnose.h>
33cdf0e10cSrcweir #include <osl/thread.h>
34cdf0e10cSrcweir #include <rtl/alloc.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace osl
37cdf0e10cSrcweir {
38cdf0e10cSrcweir /** threadFunc is the function which is executed by the threads
3988e06183Smseidel 	created by the osl::Thread class. The function's signature
4088e06183Smseidel 	matches the one of oslWorkerFunction which is declared in
4188e06183Smseidel 	osl/thread.h .
42cdf0e10cSrcweir */
43cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param);
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class Thread
46cdf0e10cSrcweir {
4788e06183Smseidel 	Thread( const Thread& );
4888e06183Smseidel 	Thread& operator= ( const Thread& );
49cdf0e10cSrcweir public:
50cdf0e10cSrcweir 	// these are here to force memory de/allocation to sal lib.
operator new(size_t nSize)51cdf0e10cSrcweir 	inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (())
52cdf0e10cSrcweir 		{ return ::rtl_allocateMemory( nSize ); }
operator delete(void * pMem)53cdf0e10cSrcweir 	inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (())
54cdf0e10cSrcweir 		{ ::rtl_freeMemory( pMem ); }
operator new(size_t,void * pMem)55cdf0e10cSrcweir 	inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW (())
56cdf0e10cSrcweir 		{ return pMem; }
operator delete(void *,void *)57cdf0e10cSrcweir 	inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (())
58cdf0e10cSrcweir 		{}
59cdf0e10cSrcweir 
Thread()6088e06183Smseidel 	Thread(): m_hThread(0){}
61cdf0e10cSrcweir 
~Thread()6288e06183Smseidel 	virtual ~Thread()
6388e06183Smseidel 	{
6488e06183Smseidel 		osl_destroyThread( m_hThread);
6588e06183Smseidel 	}
66cdf0e10cSrcweir 
create()6788e06183Smseidel 	sal_Bool SAL_CALL create()
6888e06183Smseidel 	{
6988e06183Smseidel 		OSL_ASSERT(m_hThread == 0); // only one running thread per instance
70*5f05c480Smseidel 		if( m_hThread )
71cdf0e10cSrcweir 			return sal_False;
72cdf0e10cSrcweir 
7388e06183Smseidel 		m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
74*5f05c480Smseidel 		if( m_hThread )
7588e06183Smseidel 			osl_resumeThread(m_hThread);
7688e06183Smseidel 
7788e06183Smseidel 		return m_hThread != 0;
7888e06183Smseidel 	}
7988e06183Smseidel 
createSuspended()8088e06183Smseidel 	sal_Bool SAL_CALL createSuspended()
8188e06183Smseidel 	{
8288e06183Smseidel 		OSL_ASSERT(m_hThread == 0); // only one running thread per instance
83*5f05c480Smseidel 		if( m_hThread )
8488e06183Smseidel 			return sal_False;
85*5f05c480Smseidel 
86*5f05c480Smseidel 		m_hThread= osl_createSuspendedThread( threadFunc, (void*)this);
87*5f05c480Smseidel 
8888e06183Smseidel 		return m_hThread != 0;
8988e06183Smseidel 	}
9088e06183Smseidel 
suspend()9188e06183Smseidel 	virtual void SAL_CALL suspend()
9288e06183Smseidel 	{
9388e06183Smseidel 		if( m_hThread )
9488e06183Smseidel 			osl_suspendThread(m_hThread);
9588e06183Smseidel 	}
9688e06183Smseidel 
resume()9788e06183Smseidel 	virtual void SAL_CALL resume()
9888e06183Smseidel 	{
9988e06183Smseidel 		if( m_hThread )
10088e06183Smseidel 			osl_resumeThread(m_hThread);
10188e06183Smseidel 	}
10288e06183Smseidel 
terminate()10388e06183Smseidel 	virtual void SAL_CALL terminate()
10488e06183Smseidel 	{
10588e06183Smseidel 		if( m_hThread )
10688e06183Smseidel 			osl_terminateThread(m_hThread);
10788e06183Smseidel 	}
10888e06183Smseidel 
join()10988e06183Smseidel 	virtual void SAL_CALL join()
11088e06183Smseidel 	{
11188e06183Smseidel 		osl_joinWithThread(m_hThread);
11288e06183Smseidel 	}
11388e06183Smseidel 
isRunning() const11488e06183Smseidel 	sal_Bool SAL_CALL isRunning() const
11588e06183Smseidel 	{
11688e06183Smseidel 		return osl_isThreadRunning(m_hThread);
11788e06183Smseidel 	}
11888e06183Smseidel 
setPriority(oslThreadPriority Priority)11988e06183Smseidel 	void SAL_CALL setPriority( oslThreadPriority Priority)
12088e06183Smseidel 	{
12188e06183Smseidel 		if( m_hThread )
12288e06183Smseidel 			osl_setThreadPriority(m_hThread, Priority);
12388e06183Smseidel 	}
12488e06183Smseidel 
getPriority() const12588e06183Smseidel 	oslThreadPriority SAL_CALL getPriority() const
12688e06183Smseidel 	{
12788e06183Smseidel 		return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
12888e06183Smseidel 	}
12988e06183Smseidel 
getIdentifier() const13088e06183Smseidel 	oslThreadIdentifier SAL_CALL getIdentifier() const
13188e06183Smseidel 	{
13288e06183Smseidel 		return osl_getThreadIdentifier(m_hThread);
13388e06183Smseidel 	}
13488e06183Smseidel 
getCurrentIdentifier()13588e06183Smseidel 	static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
13688e06183Smseidel 	{
13788e06183Smseidel 		return osl_getThreadIdentifier(0);
13888e06183Smseidel 	}
13988e06183Smseidel 
wait(const TimeValue & Delay)14088e06183Smseidel 	static void SAL_CALL wait(const TimeValue& Delay)
14188e06183Smseidel 	{
14288e06183Smseidel 		osl_waitThread(&Delay);
14388e06183Smseidel 	}
14488e06183Smseidel 
yield()14588e06183Smseidel 	static void SAL_CALL yield()
14688e06183Smseidel 	{
14788e06183Smseidel 		osl_yieldThread();
14888e06183Smseidel 	}
14988e06183Smseidel 
setName(char const * name)15088e06183Smseidel 	static inline void setName(char const * name) throw () {
15188e06183Smseidel 		osl_setThreadName(name);
15288e06183Smseidel 	}
15388e06183Smseidel 
schedule()15488e06183Smseidel 	virtual sal_Bool SAL_CALL schedule()
15588e06183Smseidel 	{
15688e06183Smseidel 		return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
15788e06183Smseidel 	}
15888e06183Smseidel 
operator oslThread() const15988e06183Smseidel 	SAL_CALL operator oslThread() const
16088e06183Smseidel 	{
16188e06183Smseidel 		return m_hThread;
16288e06183Smseidel 	}
163cdf0e10cSrcweir 
164cdf0e10cSrcweir protected:
165cdf0e10cSrcweir 
16688e06183Smseidel 	/** The thread functions calls the protected functions
16788e06183Smseidel 		run and onTerminated.
16888e06183Smseidel 	*/
16988e06183Smseidel 	friend void SAL_CALL threadFunc( void* param);
170cdf0e10cSrcweir 
17188e06183Smseidel 	virtual void SAL_CALL run() = 0;
172cdf0e10cSrcweir 
onTerminated()17388e06183Smseidel 	virtual void SAL_CALL onTerminated()
17488e06183Smseidel 	{
17588e06183Smseidel 	}
176cdf0e10cSrcweir 
177cdf0e10cSrcweir private:
17888e06183Smseidel 	oslThread m_hThread;
179cdf0e10cSrcweir };
180cdf0e10cSrcweir 
threadFunc(void * param)181cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param)
182cdf0e10cSrcweir {
18388e06183Smseidel 		Thread* pObj= (Thread*)param;
18488e06183Smseidel 		pObj->run();
18588e06183Smseidel 		pObj->onTerminated();
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
18888e06183Smseidel class ThreadData
189cdf0e10cSrcweir {
19088e06183Smseidel 	ThreadData( const ThreadData& );
19188e06183Smseidel 	ThreadData& operator= (const ThreadData& );
192cdf0e10cSrcweir public:
19388e06183Smseidel  	/// Create a thread specific local data key
ThreadData(oslThreadKeyCallbackFunction pCallback=0)19488e06183Smseidel 	ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
19588e06183Smseidel 	{
19688e06183Smseidel 		m_hKey = osl_createThreadKey( pCallback );
19788e06183Smseidel 	}
198cdf0e10cSrcweir 
19988e06183Smseidel 	/// Destroy a thread specific local data key
~ThreadData()200cdf0e10cSrcweir 	~ThreadData()
20188e06183Smseidel 	{
20288e06183Smseidel 		osl_destroyThreadKey(m_hKey);
20388e06183Smseidel 	}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	/** Set the data associated with the data key.
20686e1cf34SPedro Giffuni 		@returns True if operation was successful
207cdf0e10cSrcweir 	*/
setData(void * pData)208cdf0e10cSrcweir 	sal_Bool SAL_CALL setData(void *pData)
20988e06183Smseidel 	{
21088e06183Smseidel 		return (osl_setThreadKeyData(m_hKey, pData));
21188e06183Smseidel 	}
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	/** Get the data associated with the data key.
21488e06183Smseidel 		@returns The data associated with the data key or
215cdf0e10cSrcweir 		NULL if no data was set
216cdf0e10cSrcweir 	*/
getData()217cdf0e10cSrcweir 	void* SAL_CALL getData()
21888e06183Smseidel 	{
21988e06183Smseidel 		return osl_getThreadKeyData(m_hKey);
22088e06183Smseidel 	}
22188e06183Smseidel 
operator oslThreadKey() const22288e06183Smseidel 	operator oslThreadKey() const
22388e06183Smseidel 	{
22488e06183Smseidel 		return m_hKey;
22588e06183Smseidel 	}
226cdf0e10cSrcweir 
227cdf0e10cSrcweir private:
228cdf0e10cSrcweir 	oslThreadKey m_hKey;
229cdf0e10cSrcweir };
230cdf0e10cSrcweir 
231cdf0e10cSrcweir } // end namespace osl
232cdf0e10cSrcweir #endif
233cdf0e10cSrcweir #endif
234