update dev300-m58
[ooovba.git] / sal / inc / osl / thread.hxx
blob1fb3bef6b62128635bb9dfd600ceb5a892911a48
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.hxx,v $
10 * $Revision: 1.11 $
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 #ifndef _THREAD_HXX_
32 #define _THREAD_HXX_
34 #ifdef __cplusplus
36 #include <osl/time.h>
39 #include <osl/diagnose.h>
40 #include <osl/thread.h>
41 #include <rtl/alloc.h>
43 namespace osl
45 /** threadFunc is the function which is executed by the threads
46 created by the osl::Thread class. The function's signature
47 matches the one of oslWorkerFunction which is declared in
48 osl/thread.h .
50 extern "C" inline void SAL_CALL threadFunc( void* param);
52 class Thread
54 Thread( const Thread& );
55 Thread& operator= ( const Thread& );
56 public:
57 // these are here to force memory de/allocation to sal lib.
58 inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (())
59 { return ::rtl_allocateMemory( nSize ); }
60 inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (())
61 { ::rtl_freeMemory( pMem ); }
62 inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW (())
63 { return pMem; }
64 inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (())
67 Thread(): m_hThread(0){}
69 virtual ~Thread()
71 osl_destroyThread( m_hThread);
74 sal_Bool SAL_CALL create()
76 OSL_ASSERT(m_hThread == 0); // only one running thread per instance
77 if (m_hThread)
78 return sal_False;
80 m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
81 if ( m_hThread )
82 osl_resumeThread(m_hThread);
84 return m_hThread != 0;
87 sal_Bool SAL_CALL createSuspended()
89 OSL_ASSERT(m_hThread == 0); // only one running thread per instance
90 if( m_hThread)
91 return sal_False;
92 m_hThread= osl_createSuspendedThread( threadFunc,
93 (void*)this);
94 return m_hThread != 0;
97 virtual void SAL_CALL suspend()
99 if( m_hThread )
100 osl_suspendThread(m_hThread);
103 virtual void SAL_CALL resume()
105 if( m_hThread )
106 osl_resumeThread(m_hThread);
109 virtual void SAL_CALL terminate()
111 if( m_hThread )
112 osl_terminateThread(m_hThread);
115 virtual void SAL_CALL join()
117 osl_joinWithThread(m_hThread);
120 sal_Bool SAL_CALL isRunning()
122 return osl_isThreadRunning(m_hThread);
125 void SAL_CALL setPriority( oslThreadPriority Priority)
127 if( m_hThread )
128 osl_setThreadPriority(m_hThread, Priority);
131 oslThreadPriority SAL_CALL getPriority()
133 return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
136 oslThreadIdentifier SAL_CALL getIdentifier() const
138 return osl_getThreadIdentifier(m_hThread);
141 static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
143 return osl_getThreadIdentifier(0);
146 static void SAL_CALL wait(const TimeValue& Delay)
148 osl_waitThread(&Delay);
151 static void SAL_CALL yield()
153 osl_yieldThread();
157 virtual sal_Bool SAL_CALL schedule()
159 return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
162 SAL_CALL operator oslThread() const
164 return m_hThread;
167 protected:
169 /** The thread functions calls the protected functions
170 run and onTerminated.
172 friend void SAL_CALL threadFunc( void* param);
174 virtual void SAL_CALL run() = 0;
176 virtual void SAL_CALL onTerminated()
180 private:
181 oslThread m_hThread;
184 extern "C" inline void SAL_CALL threadFunc( void* param)
186 Thread* pObj= (Thread*)param;
187 pObj->run();
188 pObj->onTerminated();
191 class ThreadData
193 ThreadData( const ThreadData& );
194 ThreadData& operator= (const ThreadData& );
195 public:
196 /// Create a thread specific local data key
197 ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
199 m_hKey = osl_createThreadKey( pCallback );
202 /// Destroy a thread specific local data key
203 ~ThreadData()
205 osl_destroyThreadKey(m_hKey);
208 /** Set the data associated with the data key.
209 @returns True if operation was successfull
211 sal_Bool SAL_CALL setData(void *pData)
213 return (osl_setThreadKeyData(m_hKey, pData));
216 /** Get the data associated with the data key.
217 @returns The data asscoitaed with the data key or
218 NULL if no data was set
220 void* SAL_CALL getData()
222 return osl_getThreadKeyData(m_hKey);
225 operator oslThreadKey() const
227 return m_hKey;
230 private:
231 oslThreadKey m_hKey;
234 } // end namespace osl
235 #endif
236 #endif