xref: /AOO41X/main/sal/inc/osl/thread.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _THREAD_HXX_
29*cdf0e10cSrcweir #define _THREAD_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifdef __cplusplus
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <osl/time.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <osl/diagnose.h>
37*cdf0e10cSrcweir #include <osl/thread.h>
38*cdf0e10cSrcweir #include <rtl/alloc.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace osl
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir /** threadFunc is the function which is executed by the threads
43*cdf0e10cSrcweir     created by the osl::Thread class. The function's signature
44*cdf0e10cSrcweir     matches the one of oslWorkerFunction which is declared in
45*cdf0e10cSrcweir     osl/thread.h .
46*cdf0e10cSrcweir */
47*cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param);
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir class Thread
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     Thread( const Thread& );
52*cdf0e10cSrcweir     Thread& operator= ( const Thread& );
53*cdf0e10cSrcweir public:
54*cdf0e10cSrcweir 	// these are here to force memory de/allocation to sal lib.
55*cdf0e10cSrcweir 	inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (())
56*cdf0e10cSrcweir 		{ return ::rtl_allocateMemory( nSize ); }
57*cdf0e10cSrcweir 	inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (())
58*cdf0e10cSrcweir 		{ ::rtl_freeMemory( pMem ); }
59*cdf0e10cSrcweir 	inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW (())
60*cdf0e10cSrcweir 		{ return pMem; }
61*cdf0e10cSrcweir 	inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (())
62*cdf0e10cSrcweir 		{}
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     Thread(): m_hThread(0){}
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     virtual  ~Thread()
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         osl_destroyThread( m_hThread);
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     sal_Bool SAL_CALL create()
72*cdf0e10cSrcweir     {
73*cdf0e10cSrcweir 	    OSL_ASSERT(m_hThread == 0);	// only one running thread per instance
74*cdf0e10cSrcweir        	if (m_hThread)
75*cdf0e10cSrcweir 			return sal_False;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	    m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
78*cdf0e10cSrcweir 	    if ( m_hThread )
79*cdf0e10cSrcweir 		    osl_resumeThread(m_hThread);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	    return m_hThread != 0;
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     sal_Bool SAL_CALL createSuspended()
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir 	    OSL_ASSERT(m_hThread == 0);	// only one running thread per instance
87*cdf0e10cSrcweir         if( m_hThread)
88*cdf0e10cSrcweir             return sal_False;
89*cdf0e10cSrcweir 	    m_hThread= osl_createSuspendedThread( threadFunc,
90*cdf0e10cSrcweir 										     (void*)this);
91*cdf0e10cSrcweir 	    return m_hThread != 0;
92*cdf0e10cSrcweir     }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     virtual void SAL_CALL suspend()
95*cdf0e10cSrcweir     {
96*cdf0e10cSrcweir         if( m_hThread )
97*cdf0e10cSrcweir             osl_suspendThread(m_hThread);
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     virtual void SAL_CALL resume()
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         if( m_hThread )
103*cdf0e10cSrcweir             osl_resumeThread(m_hThread);
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     virtual void SAL_CALL terminate()
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         if( m_hThread )
109*cdf0e10cSrcweir             osl_terminateThread(m_hThread);
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     virtual void SAL_CALL join()
113*cdf0e10cSrcweir     {
114*cdf0e10cSrcweir         osl_joinWithThread(m_hThread);
115*cdf0e10cSrcweir     }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     sal_Bool SAL_CALL isRunning() const
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir 	    return osl_isThreadRunning(m_hThread);
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     void SAL_CALL setPriority( oslThreadPriority Priority)
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         if( m_hThread )
125*cdf0e10cSrcweir             osl_setThreadPriority(m_hThread, Priority);
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     oslThreadPriority SAL_CALL getPriority() const
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir 	    return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     oslThreadIdentifier SAL_CALL getIdentifier() const
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir 	    return osl_getThreadIdentifier(m_hThread);
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir 	    return osl_getThreadIdentifier(0);
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     static void SAL_CALL wait(const TimeValue& Delay)
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir 	    osl_waitThread(&Delay);
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     static void SAL_CALL yield()
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir 	    osl_yieldThread();
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     static inline void setName(char const * name) throw () {
154*cdf0e10cSrcweir         osl_setThreadName(name);
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL schedule()
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir 	    return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     SAL_CALL operator oslThread() const
163*cdf0e10cSrcweir     {
164*cdf0e10cSrcweir         return m_hThread;
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir protected:
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     /** The thread functions calls the protected functions
170*cdf0e10cSrcweir         run and onTerminated.
171*cdf0e10cSrcweir     */
172*cdf0e10cSrcweir     friend void SAL_CALL threadFunc( void* param);
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     virtual void SAL_CALL run() = 0;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     virtual void SAL_CALL onTerminated()
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir private:
181*cdf0e10cSrcweir     oslThread m_hThread;
182*cdf0e10cSrcweir };
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param)
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir         Thread* pObj= (Thread*)param;
187*cdf0e10cSrcweir         pObj->run();
188*cdf0e10cSrcweir 	    pObj->onTerminated();
189*cdf0e10cSrcweir }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir class ThreadData
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir     ThreadData( const ThreadData& );
194*cdf0e10cSrcweir     ThreadData& operator= (const ThreadData& );
195*cdf0e10cSrcweir public:
196*cdf0e10cSrcweir  	/// Create a thread specific local data key
197*cdf0e10cSrcweir     ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         m_hKey = osl_createThreadKey( pCallback );
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	/// Destroy a thread specific local data key
203*cdf0e10cSrcweir 	~ThreadData()
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir        	osl_destroyThreadKey(m_hKey);
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 	/** Set the data associated with the data key.
209*cdf0e10cSrcweir 		@returns True if operation was successfull
210*cdf0e10cSrcweir 	*/
211*cdf0e10cSrcweir 	sal_Bool SAL_CALL setData(void *pData)
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir        	return (osl_setThreadKeyData(m_hKey, pData));
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	/** Get the data associated with the data key.
217*cdf0e10cSrcweir 		@returns The data asscoitaed with the data key or
218*cdf0e10cSrcweir 		NULL if no data was set
219*cdf0e10cSrcweir 	*/
220*cdf0e10cSrcweir 	void* SAL_CALL getData()
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir        	return osl_getThreadKeyData(m_hKey);
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     operator oslThreadKey() const
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         return m_hKey;
228*cdf0e10cSrcweir     }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir private:
231*cdf0e10cSrcweir 	oslThreadKey m_hKey;
232*cdf0e10cSrcweir };
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir } // end namespace osl
235*cdf0e10cSrcweir #endif
236*cdf0e10cSrcweir #endif
237