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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_OSL_THREAD_HXX
25 #define INCLUDED_OSL_THREAD_HXX
27 #include "sal/config.h"
33 #include "osl/thread.h"
34 #include "rtl/alloc.h"
38 /** threadFunc is the function which is executed by the threads
39 created by the osl::Thread class. The function's signature
40 matches the one of oslWorkerFunction which is declared in
43 extern "C" inline void SAL_CALL
threadFunc( void* param
);
48 @deprecated use ::salhelper::Thread instead. Only the static member
49 functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
50 ::osl::Thread::yield are not deprecated.
54 Thread( const Thread
& ) SAL_DELETED_FUNCTION
;
55 Thread
& operator= ( const Thread
& ) SAL_DELETED_FUNCTION
;
57 // these are here to force memory de/allocation to sal lib.
58 static void * SAL_CALL
operator new( size_t nSize
)
59 { return ::rtl_allocateMemory( nSize
); }
60 static void SAL_CALL
operator delete( void * pMem
)
61 { ::rtl_freeMemory( pMem
); }
62 static void * SAL_CALL
operator new( size_t, void * pMem
)
64 static void SAL_CALL
operator delete( void *, void * )
67 Thread(): m_hThread(NULL
){}
69 virtual ~Thread() COVERITY_NOEXCEPT_FALSE
71 osl_destroyThread( m_hThread
);
74 bool SAL_CALL
create()
76 assert(m_hThread
== NULL
); // only one running thread per instance
77 m_hThread
= osl_createSuspendedThread( threadFunc
, static_cast<void*>(this));
78 if (m_hThread
== NULL
)
82 osl_resumeThread(m_hThread
);
86 bool SAL_CALL
createSuspended()
88 assert(m_hThread
== NULL
); // only one running thread per instance
91 m_hThread
= osl_createSuspendedThread( threadFunc
,
92 static_cast<void*>(this));
93 return m_hThread
!= NULL
;
96 virtual void SAL_CALL
suspend()
99 osl_suspendThread(m_hThread
);
102 virtual void SAL_CALL
resume()
105 osl_resumeThread(m_hThread
);
108 virtual void SAL_CALL
terminate()
111 osl_terminateThread(m_hThread
);
114 virtual void SAL_CALL
join()
116 osl_joinWithThread(m_hThread
);
119 bool SAL_CALL
isRunning() const
121 return osl_isThreadRunning(m_hThread
);
124 void SAL_CALL
setPriority( oslThreadPriority Priority
)
127 osl_setThreadPriority(m_hThread
, Priority
);
130 oslThreadPriority SAL_CALL
getPriority() const
132 return m_hThread
? osl_getThreadPriority(m_hThread
) : osl_Thread_PriorityUnknown
;
135 oslThreadIdentifier SAL_CALL
getIdentifier() const
137 return osl_getThreadIdentifier(m_hThread
);
140 static oslThreadIdentifier SAL_CALL
getCurrentIdentifier()
142 return osl_getThreadIdentifier(NULL
);
145 static void SAL_CALL
wait(const TimeValue
& Delay
)
147 osl_waitThread(&Delay
);
150 static void SAL_CALL
yield()
155 static void setName(char const * name
) SAL_NOEXCEPT
{
156 osl_setThreadName(name
);
159 virtual bool SAL_CALL
schedule()
161 return m_hThread
&& osl_scheduleThread(m_hThread
);
164 SAL_CALL
operator oslThread() const
171 /** The thread functions calls the protected functions
172 run and onTerminated.
174 friend void SAL_CALL
threadFunc( void* param
);
176 virtual void SAL_CALL
run() = 0;
178 virtual void SAL_CALL
onTerminated()
186 extern "C" inline void SAL_CALL
threadFunc( void* param
)
188 Thread
* pObj
= static_cast<Thread
*>(param
);
190 pObj
->onTerminated();
195 ThreadData( const ThreadData
& ) SAL_DELETED_FUNCTION
;
196 ThreadData
& operator= (const ThreadData
& ) SAL_DELETED_FUNCTION
;
198 /// Create a thread specific local data key
199 ThreadData( oslThreadKeyCallbackFunction pCallback
= NULL
)
201 m_hKey
= osl_createThreadKey( pCallback
);
204 /// Destroy a thread specific local data key
207 osl_destroyThreadKey(m_hKey
);
210 /** Set the data associated with the data key.
211 @returns True if operation was successful
213 bool SAL_CALL
setData(void *pData
)
215 return osl_setThreadKeyData(m_hKey
, pData
);
218 /** Get the data associated with the data key.
219 @returns The data associated with the data key or
220 NULL if no data was set
222 void* SAL_CALL
getData()
224 return osl_getThreadKeyData(m_hKey
);
227 operator oslThreadKey() const
236 } // end namespace osl
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */