1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_OSL_THREAD_HXX
21 #define INCLUDED_OSL_THREAD_HXX
23 #include <sal/config.h>
28 #include <osl/thread.h>
29 #include <rtl/alloc.h>
33 /** threadFunc is the function which is executed by the threads
34 created by the osl::Thread class. The function's signature
35 matches the one of oslWorkerFunction which is declared in
38 extern "C" inline void SAL_CALL
threadFunc( void* param
);
43 @deprecated use ::salhelper::Thread instead. Only the static member
44 functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
45 ::osl::Thread::yield are not deprecated.
49 Thread( const Thread
& ) SAL_DELETED_FUNCTION
;
50 Thread
& operator= ( const Thread
& ) SAL_DELETED_FUNCTION
;
52 // these are here to force memory de/allocation to sal lib.
53 inline static void * SAL_CALL
operator new( size_t nSize
)
54 { return ::rtl_allocateMemory( nSize
); }
55 inline static void SAL_CALL
operator delete( void * pMem
)
56 { ::rtl_freeMemory( pMem
); }
57 inline static void * SAL_CALL
operator new( size_t, void * pMem
)
59 inline static void SAL_CALL
operator delete( void *, void * )
62 Thread(): m_hThread(0){}
66 osl_destroyThread( m_hThread
);
69 bool SAL_CALL
create()
71 assert(m_hThread
== 0); // only one running thread per instance
72 m_hThread
= osl_createSuspendedThread( threadFunc
, (void*)this);
77 osl_resumeThread(m_hThread
);
81 bool SAL_CALL
createSuspended()
83 assert(m_hThread
== 0); // only one running thread per instance
86 m_hThread
= osl_createSuspendedThread( threadFunc
,
88 return m_hThread
!= 0;
91 virtual void SAL_CALL
suspend()
94 osl_suspendThread(m_hThread
);
97 virtual void SAL_CALL
resume()
100 osl_resumeThread(m_hThread
);
103 virtual void SAL_CALL
terminate()
106 osl_terminateThread(m_hThread
);
109 virtual void SAL_CALL
join()
111 osl_joinWithThread(m_hThread
);
114 bool SAL_CALL
isRunning() const
116 return osl_isThreadRunning(m_hThread
);
119 void SAL_CALL
setPriority( oslThreadPriority Priority
)
122 osl_setThreadPriority(m_hThread
, Priority
);
125 oslThreadPriority SAL_CALL
getPriority() const
127 return m_hThread
? osl_getThreadPriority(m_hThread
) : osl_Thread_PriorityUnknown
;
130 oslThreadIdentifier SAL_CALL
getIdentifier() const
132 return osl_getThreadIdentifier(m_hThread
);
135 static oslThreadIdentifier SAL_CALL
getCurrentIdentifier()
137 return osl_getThreadIdentifier(0);
140 static void SAL_CALL
wait(const TimeValue
& Delay
)
142 osl_waitThread(&Delay
);
145 static void SAL_CALL
yield()
150 static inline void setName(char const * name
) throw () {
151 osl_setThreadName(name
);
154 virtual bool SAL_CALL
schedule()
156 return m_hThread
&& osl_scheduleThread(m_hThread
);
159 SAL_CALL
operator oslThread() const
166 /** The thread functions calls the protected functions
167 run and onTerminated.
169 friend void SAL_CALL
threadFunc( void* param
);
171 virtual void SAL_CALL
run() = 0;
173 virtual void SAL_CALL
onTerminated()
181 extern "C" inline void SAL_CALL
threadFunc( void* param
)
183 Thread
* pObj
= static_cast<Thread
*>(param
);
185 pObj
->onTerminated();
190 ThreadData( const ThreadData
& ) SAL_DELETED_FUNCTION
;
191 ThreadData
& operator= (const ThreadData
& ) SAL_DELETED_FUNCTION
;
193 /// Create a thread specific local data key
194 ThreadData( oslThreadKeyCallbackFunction pCallback
= 0 )
196 m_hKey
= osl_createThreadKey( pCallback
);
199 /// Destroy a thread specific local data key
202 osl_destroyThreadKey(m_hKey
);
205 /** Set the data associated with the data key.
206 @returns True if operation was successful
208 bool SAL_CALL
setData(void *pData
)
210 return (osl_setThreadKeyData(m_hKey
, pData
));
213 /** Get the data associated with the data key.
214 @returns The data asscoitaed with the data key or
215 NULL if no data was set
217 void* SAL_CALL
getData()
219 return osl_getThreadKeyData(m_hKey
);
222 operator oslThreadKey() const
231 } // end namespace osl
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */