1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: thread.hxx,v $
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 ************************************************************************/
39 #include <osl/diagnose.h>
40 #include <osl/thread.h>
41 #include <rtl/alloc.h>
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
50 extern "C" inline void SAL_CALL
threadFunc( void* param
);
54 Thread( const Thread
& );
55 Thread
& operator= ( const Thread
& );
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 (())
64 inline static void SAL_CALL
operator delete( void *, void * ) SAL_THROW (())
67 Thread(): m_hThread(0){}
71 osl_destroyThread( m_hThread
);
74 sal_Bool SAL_CALL
create()
76 OSL_ASSERT(m_hThread
== 0); // only one running thread per instance
80 m_hThread
= osl_createSuspendedThread( threadFunc
, (void*)this);
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
92 m_hThread
= osl_createSuspendedThread( threadFunc
,
94 return m_hThread
!= 0;
97 virtual void SAL_CALL
suspend()
100 osl_suspendThread(m_hThread
);
103 virtual void SAL_CALL
resume()
106 osl_resumeThread(m_hThread
);
109 virtual void SAL_CALL
terminate()
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
)
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()
157 virtual sal_Bool SAL_CALL
schedule()
159 return m_hThread
? osl_scheduleThread(m_hThread
) : sal_False
;
162 SAL_CALL
operator oslThread() const
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()
184 extern "C" inline void SAL_CALL
threadFunc( void* param
)
186 Thread
* pObj
= (Thread
*)param
;
188 pObj
->onTerminated();
193 ThreadData( const ThreadData
& );
194 ThreadData
& operator= (const ThreadData
& );
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
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
234 } // end namespace osl