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 .
23 #include "sal/config.h"
30 #include <osl/diagnose.h>
31 #include <osl/thread.h>
32 #include <rtl/alloc.h>
36 /** threadFunc is the function which is executed by the threads
37 created by the osl::Thread class. The function's signature
38 matches the one of oslWorkerFunction which is declared in
41 extern "C" inline void SAL_CALL
threadFunc( void* param
);
46 @deprecated use ::salhelper::Thread instead. Only the static member
47 functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
48 ::osl::Thread::yield are not deprecated.
52 Thread( const Thread
& );
53 Thread
& operator= ( const Thread
& );
55 // these are here to force memory de/allocation to sal lib.
56 inline static void * SAL_CALL
operator new( size_t nSize
) SAL_THROW (())
57 { return ::rtl_allocateMemory( nSize
); }
58 inline static void SAL_CALL
operator delete( void * pMem
) SAL_THROW (())
59 { ::rtl_freeMemory( pMem
); }
60 inline static void * SAL_CALL
operator new( size_t, void * pMem
) SAL_THROW (())
62 inline static void SAL_CALL
operator delete( void *, void * ) SAL_THROW (())
65 Thread(): m_hThread(0){}
69 osl_destroyThread( m_hThread
);
72 sal_Bool SAL_CALL
create()
74 assert(m_hThread
== 0); // only one running thread per instance
75 m_hThread
= osl_createSuspendedThread( threadFunc
, (void*)this);
80 osl_resumeThread(m_hThread
);
84 sal_Bool SAL_CALL
createSuspended()
86 assert(m_hThread
== 0); // only one running thread per instance
89 m_hThread
= osl_createSuspendedThread( threadFunc
,
91 return m_hThread
!= 0;
94 virtual void SAL_CALL
suspend()
97 osl_suspendThread(m_hThread
);
100 virtual void SAL_CALL
resume()
103 osl_resumeThread(m_hThread
);
106 virtual void SAL_CALL
terminate()
109 osl_terminateThread(m_hThread
);
112 virtual void SAL_CALL
join()
114 osl_joinWithThread(m_hThread
);
117 sal_Bool SAL_CALL
isRunning() const
119 return osl_isThreadRunning(m_hThread
);
122 void SAL_CALL
setPriority( oslThreadPriority Priority
)
125 osl_setThreadPriority(m_hThread
, Priority
);
128 oslThreadPriority SAL_CALL
getPriority() const
130 return m_hThread
? osl_getThreadPriority(m_hThread
) : osl_Thread_PriorityUnknown
;
133 oslThreadIdentifier SAL_CALL
getIdentifier() const
135 return osl_getThreadIdentifier(m_hThread
);
138 static oslThreadIdentifier SAL_CALL
getCurrentIdentifier()
140 return osl_getThreadIdentifier(0);
143 static void SAL_CALL
wait(const TimeValue
& Delay
)
145 osl_waitThread(&Delay
);
148 static void SAL_CALL
yield()
153 static inline void setName(char const * name
) throw () {
154 osl_setThreadName(name
);
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 successful
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
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */