xref: /trunk/main/sal/inc/osl/thread.hxx (revision 6e8337c105d646591dfdf02e780094cc04f2ad6a)
1565d668cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
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
10cdf0e10cSrcweir  *
11565d668cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
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
39cdf0e10cSrcweir     created by the osl::Thread class. The function's signature
40cdf0e10cSrcweir     matches the one of oslWorkerFunction which is declared in
41cdf0e10cSrcweir     osl/thread.h .
42cdf0e10cSrcweir */
43cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param);
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class Thread
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     Thread( const Thread& );
48cdf0e10cSrcweir     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()60cdf0e10cSrcweir     Thread(): m_hThread(0){}
61cdf0e10cSrcweir 
~Thread()62cdf0e10cSrcweir     virtual ~Thread()
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir         osl_destroyThread( m_hThread);
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
create()67cdf0e10cSrcweir     sal_Bool SAL_CALL create()
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         OSL_ASSERT(m_hThread == 0); // only one running thread per instance
70cdf0e10cSrcweir         if( m_hThread )
71cdf0e10cSrcweir             return sal_False;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
74cdf0e10cSrcweir         if( m_hThread )
75cdf0e10cSrcweir             osl_resumeThread(m_hThread);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         return m_hThread != 0;
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
createSuspended()80cdf0e10cSrcweir     sal_Bool SAL_CALL createSuspended()
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         OSL_ASSERT(m_hThread == 0); // only one running thread per instance
83cdf0e10cSrcweir         if( m_hThread )
84cdf0e10cSrcweir             return sal_False;
85*6e8337c1Smseidel 
86*6e8337c1Smseidel         m_hThread= osl_createSuspendedThread( threadFunc, (void*)this);
87*6e8337c1Smseidel 
88cdf0e10cSrcweir         return m_hThread != 0;
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
suspend()91cdf0e10cSrcweir     virtual void SAL_CALL suspend()
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         if( m_hThread )
94cdf0e10cSrcweir             osl_suspendThread(m_hThread);
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
resume()97cdf0e10cSrcweir     virtual void SAL_CALL resume()
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         if( m_hThread )
100cdf0e10cSrcweir             osl_resumeThread(m_hThread);
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
terminate()103cdf0e10cSrcweir     virtual void SAL_CALL terminate()
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         if( m_hThread )
106cdf0e10cSrcweir             osl_terminateThread(m_hThread);
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
join()109cdf0e10cSrcweir     virtual void SAL_CALL join()
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         osl_joinWithThread(m_hThread);
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
isRunning() const114cdf0e10cSrcweir     sal_Bool SAL_CALL isRunning() const
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         return osl_isThreadRunning(m_hThread);
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
setPriority(oslThreadPriority Priority)119cdf0e10cSrcweir     void SAL_CALL setPriority( oslThreadPriority Priority)
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir         if( m_hThread )
122cdf0e10cSrcweir             osl_setThreadPriority(m_hThread, Priority);
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir 
getPriority() const125cdf0e10cSrcweir     oslThreadPriority SAL_CALL getPriority() const
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
getIdentifier() const130cdf0e10cSrcweir     oslThreadIdentifier SAL_CALL getIdentifier() const
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         return osl_getThreadIdentifier(m_hThread);
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
getCurrentIdentifier()135cdf0e10cSrcweir     static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         return osl_getThreadIdentifier(0);
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
wait(const TimeValue & Delay)140cdf0e10cSrcweir     static void SAL_CALL wait(const TimeValue& Delay)
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         osl_waitThread(&Delay);
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
yield()145cdf0e10cSrcweir     static void SAL_CALL yield()
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         osl_yieldThread();
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
setName(char const * name)150cdf0e10cSrcweir     static inline void setName(char const * name) throw () {
151cdf0e10cSrcweir         osl_setThreadName(name);
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
schedule()154cdf0e10cSrcweir     virtual sal_Bool SAL_CALL schedule()
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
operator oslThread() const159cdf0e10cSrcweir     SAL_CALL operator oslThread() const
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         return m_hThread;
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir protected:
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     /** The thread functions calls the protected functions
167cdf0e10cSrcweir         run and onTerminated.
168cdf0e10cSrcweir     */
169cdf0e10cSrcweir     friend void SAL_CALL threadFunc( void* param);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     virtual void SAL_CALL run() = 0;
172cdf0e10cSrcweir 
onTerminated()173cdf0e10cSrcweir     virtual void SAL_CALL onTerminated()
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir private:
178cdf0e10cSrcweir     oslThread m_hThread;
179cdf0e10cSrcweir };
180cdf0e10cSrcweir 
threadFunc(void * param)181cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param)
182cdf0e10cSrcweir {
183cdf0e10cSrcweir         Thread* pObj= (Thread*)param;
184cdf0e10cSrcweir         pObj->run();
185cdf0e10cSrcweir         pObj->onTerminated();
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir class ThreadData
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     ThreadData( const ThreadData& );
191cdf0e10cSrcweir     ThreadData& operator= (const ThreadData& );
192cdf0e10cSrcweir public:
193cdf0e10cSrcweir     /// Create a thread specific local data key
ThreadData(oslThreadKeyCallbackFunction pCallback=0)194cdf0e10cSrcweir     ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         m_hKey = osl_createThreadKey( pCallback );
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     /// Destroy a thread specific local data key
~ThreadData()200cdf0e10cSrcweir     ~ThreadData()
201cdf0e10cSrcweir     {
202cdf0e10cSrcweir         osl_destroyThreadKey(m_hKey);
203cdf0e10cSrcweir     }
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)
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         return (osl_setThreadKeyData(m_hKey, pData));
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     /** Get the data associated with the data key.
21464b67dd5Smseidel         @returns The data associated with the data key or
215cdf0e10cSrcweir         NULL if no data was set
216cdf0e10cSrcweir     */
getData()217cdf0e10cSrcweir     void* SAL_CALL getData()
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir         return osl_getThreadKeyData(m_hKey);
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir 
operator oslThreadKey() const222cdf0e10cSrcweir     operator oslThreadKey() const
223cdf0e10cSrcweir     {
224cdf0e10cSrcweir         return m_hKey;
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir private:
228cdf0e10cSrcweir     oslThreadKey m_hKey;
229cdf0e10cSrcweir };
230cdf0e10cSrcweir 
231cdf0e10cSrcweir } // end namespace osl
232cdf0e10cSrcweir #endif
233cdf0e10cSrcweir #endif
234