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>
29 #include <osl/thread.h>
30 #include <rtl/alloc.h>
34 /** threadFunc is the function which is executed by the threads
35 created by the osl::Thread class. The function's signature
36 matches the one of oslWorkerFunction which is declared in
39 extern "C" inline void SAL_CALL
threadFunc( void* param
);
44 @deprecated use ::salhelper::Thread instead. Only the static member
45 functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
46 ::osl::Thread::yield are not deprecated.
50 Thread( const Thread
& ) SAL_DELETED_FUNCTION
;
51 Thread
& operator= ( const Thread
& ) SAL_DELETED_FUNCTION
;
53 // these are here to force memory de/allocation to sal lib.
54 static void * SAL_CALL
operator new( size_t nSize
)
55 { return ::rtl_allocateMemory( nSize
); }
56 static void SAL_CALL
operator delete( void * pMem
)
57 { ::rtl_freeMemory( pMem
); }
58 static void * SAL_CALL
operator new( size_t, void * pMem
)
60 static void SAL_CALL
operator delete( void *, void * )
63 Thread(): m_hThread(NULL
){}
67 osl_destroyThread( m_hThread
);
70 bool SAL_CALL
create()
72 assert(m_hThread
== NULL
); // only one running thread per instance
73 m_hThread
= osl_createSuspendedThread( threadFunc
, static_cast<void*>(this));
74 if (m_hThread
== NULL
)
78 osl_resumeThread(m_hThread
);
82 bool SAL_CALL
createSuspended()
84 assert(m_hThread
== NULL
); // only one running thread per instance
87 m_hThread
= osl_createSuspendedThread( threadFunc
,
88 static_cast<void*>(this));
89 return m_hThread
!= NULL
;
92 virtual void SAL_CALL
suspend()
95 osl_suspendThread(m_hThread
);
98 virtual void SAL_CALL
resume()
101 osl_resumeThread(m_hThread
);
104 virtual void SAL_CALL
terminate()
107 osl_terminateThread(m_hThread
);
110 virtual void SAL_CALL
join()
112 osl_joinWithThread(m_hThread
);
115 bool SAL_CALL
isRunning() const
117 return osl_isThreadRunning(m_hThread
);
120 void SAL_CALL
setPriority( oslThreadPriority Priority
)
123 osl_setThreadPriority(m_hThread
, Priority
);
126 oslThreadPriority SAL_CALL
getPriority() const
128 return m_hThread
? osl_getThreadPriority(m_hThread
) : osl_Thread_PriorityUnknown
;
131 oslThreadIdentifier SAL_CALL
getIdentifier() const
133 return osl_getThreadIdentifier(m_hThread
);
136 static oslThreadIdentifier SAL_CALL
getCurrentIdentifier()
138 return osl_getThreadIdentifier(NULL
);
141 static void SAL_CALL
wait(const TimeValue
& Delay
)
143 osl_waitThread(&Delay
);
146 static void SAL_CALL
yield()
151 static void setName(char const * name
) throw () {
152 osl_setThreadName(name
);
155 virtual bool SAL_CALL
schedule()
157 return m_hThread
&& osl_scheduleThread(m_hThread
);
160 SAL_CALL
operator oslThread() const
167 /** The thread functions calls the protected functions
168 run and onTerminated.
170 friend void SAL_CALL
threadFunc( void* param
);
172 virtual void SAL_CALL
run() = 0;
174 virtual void SAL_CALL
onTerminated()
182 extern "C" inline void SAL_CALL
threadFunc( void* param
)
184 Thread
* pObj
= static_cast<Thread
*>(param
);
186 pObj
->onTerminated();
191 ThreadData( const ThreadData
& ) SAL_DELETED_FUNCTION
;
192 ThreadData
& operator= (const ThreadData
& ) SAL_DELETED_FUNCTION
;
194 /// Create a thread specific local data key
195 ThreadData( oslThreadKeyCallbackFunction pCallback
= NULL
)
197 m_hKey
= osl_createThreadKey( pCallback
);
200 /// Destroy a thread specific local data key
203 osl_destroyThreadKey(m_hKey
);
206 /** Set the data associated with the data key.
207 @returns True if operation was successful
209 bool SAL_CALL
setData(void *pData
)
211 return (osl_setThreadKeyData(m_hKey
, pData
));
214 /** Get the data associated with the data key.
215 @returns The data associated with the data key or
216 NULL if no data was set
218 void* SAL_CALL
getData()
220 return osl_getThreadKeyData(m_hKey
);
223 operator oslThreadKey() const
232 } // end namespace osl
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */