LibreOffice LibreOffice 5.3 SDK C/C++ API Reference

thread.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #ifndef INCLUDED_OSL_THREAD_HXX
21 #define INCLUDED_OSL_THREAD_HXX
22
23 #include <sal/config.h>
24
25 #include <cassert>
26 #include <cstddef>
27
28 #include <osl/time.h>
29 #include <osl/thread.h>
30 #include <rtl/alloc.h>
31
32 namespace osl
33 {
39 extern "C" inline void SAL_CALL threadFunc( void* param);
40
48 class Thread
49 {
50 Thread( const Thread& ) SAL_DELETED_FUNCTION;
51 Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
52 public:
53 // these are here to force memory de/allocation to sal lib.
54 inline static void * SAL_CALL operator new( size_t nSize )
55 { return ::rtl_allocateMemory( nSize ); }
56 inline static void SAL_CALL operator delete( void * pMem )
57 { ::rtl_freeMemory( pMem ); }
58 inline static void * SAL_CALL operator new( size_t, void * pMem )
59 { return pMem; }
60 inline static void SAL_CALL operator delete( void *, void * )
61 {}
62
63 Thread(): m_hThread(NULL){}
64
65 virtual ~Thread()
66 {
67 osl_destroyThread( m_hThread);
68 }
69
70 bool SAL_CALL create()
71 {
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)
75 {
76 return false;
77 }
78 osl_resumeThread(m_hThread);
79 return true;
80 }
81
82 bool SAL_CALL createSuspended()
83 {
84 assert(m_hThread == NULL); // only one running thread per instance
85 if( m_hThread)
86 return false;
87 m_hThread= osl_createSuspendedThread( threadFunc,
88 static_cast<void*>(this));
89 return m_hThread != NULL;
90 }
91
92 virtual void SAL_CALL suspend()
93 {
94 if( m_hThread )
95 osl_suspendThread(m_hThread);
96 }
97
98 virtual void SAL_CALL resume()
99 {
100 if( m_hThread )
101 osl_resumeThread(m_hThread);
102 }
103
104 virtual void SAL_CALL terminate()
105 {
106 if( m_hThread )
107 osl_terminateThread(m_hThread);
108 }
109
110 virtual void SAL_CALL join()
111 {
112 osl_joinWithThread(m_hThread);
113 }
114
115 bool SAL_CALL isRunning() const
116 {
117 return osl_isThreadRunning(m_hThread);
118 }
119
120 void SAL_CALL setPriority( oslThreadPriority Priority)
121 {
122 if( m_hThread )
123 osl_setThreadPriority(m_hThread, Priority);
124 }
125
126 oslThreadPriority SAL_CALL getPriority() const
127 {
128 return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
129 }
130
131 oslThreadIdentifier SAL_CALL getIdentifier() const
132 {
133 return osl_getThreadIdentifier(m_hThread);
134 }
135
136 static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
137 {
138 return osl_getThreadIdentifier(NULL);
139 }
140
141 static void SAL_CALL wait(const TimeValue& Delay)
142 {
143 osl_waitThread(&Delay);
144 }
145
146 static void SAL_CALL yield()
147 {
148 osl_yieldThread();
149 }
150
151 static inline void setName(char const * name) throw () {
152 osl_setThreadName(name);
153 }
154
155 virtual bool SAL_CALL schedule()
156 {
157 return m_hThread && osl_scheduleThread(m_hThread);
158 }
159
160 SAL_CALL operator oslThread() const
161 {
162 return m_hThread;
163 }
164
165 protected:
166
170 friend void SAL_CALL threadFunc( void* param);
171
172 virtual void SAL_CALL run() = 0;
173
174 virtual void SAL_CALL onTerminated()
175 {
176 }
177
178 private:
179 oslThread m_hThread;
180 };
181
182 extern "C" inline void SAL_CALL threadFunc( void* param)
183 {
184 Thread* pObj= static_cast<Thread*>(param);
185 pObj->run();
186 pObj->onTerminated();
187 }
188
189 class ThreadData
190 {
191 ThreadData( const ThreadData& ) SAL_DELETED_FUNCTION;
192 ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
193 public:
195 ThreadData( oslThreadKeyCallbackFunction pCallback= NULL )
196 {
197 m_hKey = osl_createThreadKey( pCallback );
198 }
199
201 ~ThreadData()
202 {
203 osl_destroyThreadKey(m_hKey);
204 }
205
209 bool SAL_CALL setData(void *pData)
210 {
211 return (osl_setThreadKeyData(m_hKey, pData));
212 }
213
218 void* SAL_CALL getData()
219 {
220 return osl_getThreadKeyData(m_hKey);
221 }
222
223 operator oslThreadKey() const
224 {
225 return m_hKey;
226 }
227
228 private:
229 oslThreadKey m_hKey;
230 };
231
232 } // end namespace osl
233
234 #endif
235
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
osl::ThreadData::setData
bool setData(void *pData)
Set the data associated with the data key.
Definition: thread.hxx:209
osl::Thread::getPriority
oslThreadPriority getPriority() const
Definition: thread.hxx:126
oslThreadKey
void * oslThreadKey
Definition: thread.h:62
osl::Thread::wait
static void wait(const TimeValue &Delay)
Definition: thread.hxx:141
osl::Thread::threadFunc
friend void threadFunc(void *param)
The thread functions calls the protected functions run and onTerminated.
Definition: thread.hxx:182
osl_createSuspendedThread
SAL_DLLPUBLIC oslThread osl_createSuspendedThread(oslWorkerFunction pWorker, void *pThreadData)
Create the thread, using the function-ptr pWorker as its main (worker) function.
osl_setThreadName
SAL_DLLPUBLIC void osl_setThreadName(char const *name)
Attempts to set the name of the current thread.
osl::Thread
A thread abstraction.
Definition: thread.hxx:48
osl_destroyThreadKey
SAL_DLLPUBLIC void osl_destroyThreadKey(oslThreadKey Key)
Destroy a key to an associated thread local storage pointer.
TimeValue
Definition: time.h:70
oslThreadPriority
oslThreadPriority
levels of thread-priority Note that oslThreadPriorityUnknown might be returned by getPriorityOfThread...
Definition: thread.h:48
osl::Thread::createSuspended
bool createSuspended()
Definition: thread.hxx:82
osl_resumeThread
SAL_DLLPUBLIC void osl_resumeThread(oslThread Thread)
Wake-up a thread that was suspended with suspend() or createSuspended().
osl::ThreadData::ThreadData
ThreadData(oslThreadKeyCallbackFunction pCallback=NULL)
Create a thread specific local data key.
Definition: thread.hxx:195
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:410
osl_suspendThread
SAL_DLLPUBLIC void osl_suspendThread(oslThread Thread)
Suspend the execution of the thread.
osl_waitThread
SAL_DLLPUBLIC void osl_waitThread(const TimeValue *pDelay)
Suspends the execution of the calling thread for at least the given time.
oslThreadIdentifier
sal_uInt32 oslThreadIdentifier
Definition: thread.h:60
osl_scheduleThread
SAL_DLLPUBLIC sal_Bool osl_scheduleThread(oslThread Thread)
Offers the rest of the threads time-slice to the OS.
osl_terminateThread
SAL_DLLPUBLIC void osl_terminateThread(oslThread Thread)
The requested thread will get terminate the next time scheduleThread() is called. ...
osl_isThreadRunning
SAL_DLLPUBLIC sal_Bool osl_isThreadRunning(const oslThread Thread)
Returns True if the thread was created and has not terminated yet.
rtl_allocateMemory
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
osl_getThreadKeyData
SAL_DLLPUBLIC void * osl_getThreadKeyData(oslThreadKey Key)
Get to key associated thread specific data.
osl_setThreadPriority
SAL_DLLPUBLIC void osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority)
Changes the threads priority.
osl::Thread::yield
static void yield()
Definition: thread.hxx:146
osl::Thread::suspend
virtual void suspend()
Definition: thread.hxx:92
osl_Thread_PriorityUnknown
Definition: thread.h:55
oslThread
void * oslThread
Opaque data type for threads.
Definition: thread.h:37
osl::Thread::getCurrentIdentifier
static oslThreadIdentifier getCurrentIdentifier()
Definition: thread.hxx:136
osl
Definition: conditn.hxx:32
osl::Thread::setName
static void setName(char const *name)
Definition: thread.hxx:151
osl::Thread::schedule
virtual bool schedule()
Definition: thread.hxx:155
osl::Thread::run
virtual void run()=0
osl_joinWithThread
SAL_DLLPUBLIC void osl_joinWithThread(oslThread Thread)
Blocks the calling thread until Thread has terminated.
osl::Thread::isRunning
bool isRunning() const
Definition: thread.hxx:115
osl::Thread::terminate
virtual void terminate()
Definition: thread.hxx:104
osl_destroyThread
SAL_DLLPUBLIC void osl_destroyThread(oslThread Thread)
Release the thread handle.
osl::Thread::join
virtual void join()
Definition: thread.hxx:110
config.h
osl::threadFunc
void threadFunc(void *param)
threadFunc is the function which is executed by the threads created by the osl::Thread class...
Definition: thread.hxx:182
oslThreadKeyCallbackFunction
void(* oslThreadKeyCallbackFunction)(void *)
Definition: thread.h:168
thread.h
osl_getThreadPriority
SAL_DLLPUBLIC oslThreadPriority osl_getThreadPriority(const oslThread Thread)
Retrieves the threads priority.
osl::Thread::setPriority
void setPriority(oslThreadPriority Priority)
Definition: thread.hxx:120
osl_getThreadIdentifier
SAL_DLLPUBLIC oslThreadIdentifier osl_getThreadIdentifier(oslThread Thread)
Get the identifier for the specified thread or if parameter Thread is NULL of the current active thre...
osl_yieldThread
SAL_DLLPUBLIC void osl_yieldThread(void)
Offers the rest of the threads time-slice to the OS.
osl::Thread::~Thread
virtual ~Thread()
Definition: thread.hxx:65
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
osl::Thread::Thread
Thread()
Definition: thread.hxx:63
alloc.h
osl::ThreadData::~ThreadData
~ThreadData()
Destroy a thread specific local data key.
Definition: thread.hxx:201
osl::Thread::onTerminated
virtual void onTerminated()
Definition: thread.hxx:174
osl::Thread::getIdentifier
oslThreadIdentifier getIdentifier() const
Definition: thread.hxx:131
osl::ThreadData::getData
void * getData()
Get the data associated with the data key.
Definition: thread.hxx:218
osl::Thread::resume
virtual void resume()
Definition: thread.hxx:98
osl::Thread::create
bool create()
Definition: thread.hxx:70
osl_setThreadKeyData
SAL_DLLPUBLIC sal_Bool osl_setThreadKeyData(oslThreadKey Key, void *pData)
Set to key associated thread specific data.
osl_createThreadKey
SAL_DLLPUBLIC oslThreadKey osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
Create a key to an associated thread local storage pointer.
osl::ThreadData
Definition: thread.hxx:189
time.h