LibreOffice LibreOffice 4.1 SDK C/C++ API Reference

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 _THREAD_HXX_
21 #define _THREAD_HXX_
22
23 #include "sal/config.h"
24
25 #include <cassert>
26
27 #include <osl/time.h>
28
29
30 #include <osl/diagnose.h>
31 #include <osl/thread.h>
32 #include <rtl/alloc.h>
33
34 namespace osl
35 {
41 extern "C" inline void SAL_CALL threadFunc( void* param);
42
50 class Thread
51 {
52 Thread( const Thread& );
53 Thread& operator= ( const Thread& );
54 public:
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 (())
61 { return pMem; }
62 inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (())
63 {}
64
65 Thread(): m_hThread(0){}
66
67 virtual ~Thread()
68 {
69 osl_destroyThread( m_hThread);
70 }
71
72 sal_Bool SAL_CALL create()
73 {
74 assert(m_hThread == 0); // only one running thread per instance
75 m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
76 if (m_hThread == 0)
77 {
78 return false;
79 }
80 osl_resumeThread(m_hThread);
81 return true;
82 }
83
84 sal_Bool SAL_CALL createSuspended()
85 {
86 assert(m_hThread == 0); // only one running thread per instance
87 if( m_hThread)
88 return sal_False;
89 m_hThread= osl_createSuspendedThread( threadFunc,
90 (void*)this);
91 return m_hThread != 0;
92 }
93
94 virtual void SAL_CALL suspend()
95 {
96 if( m_hThread )
97 osl_suspendThread(m_hThread);
98 }
99
100 virtual void SAL_CALL resume()
101 {
102 if( m_hThread )
103 osl_resumeThread(m_hThread);
104 }
105
106 virtual void SAL_CALL terminate()
107 {
108 if( m_hThread )
109 osl_terminateThread(m_hThread);
110 }
111
112 virtual void SAL_CALL join()
113 {
114 osl_joinWithThread(m_hThread);
115 }
116
117 sal_Bool SAL_CALL isRunning() const
118 {
119 return osl_isThreadRunning(m_hThread);
120 }
121
122 void SAL_CALL setPriority( oslThreadPriority Priority)
123 {
124 if( m_hThread )
125 osl_setThreadPriority(m_hThread, Priority);
126 }
127
128 oslThreadPriority SAL_CALL getPriority() const
129 {
130 return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
131 }
132
133 oslThreadIdentifier SAL_CALL getIdentifier() const
134 {
135 return osl_getThreadIdentifier(m_hThread);
136 }
137
138 static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
139 {
140 return osl_getThreadIdentifier(0);
141 }
142
143 static void SAL_CALL wait(const TimeValue& Delay)
144 {
145 osl_waitThread(&Delay);
146 }
147
148 static void SAL_CALL yield()
149 {
150 osl_yieldThread();
151 }
152
153 static inline void setName(char const * name) throw () {
154 osl_setThreadName(name);
155 }
156
157 virtual sal_Bool SAL_CALL schedule()
158 {
159 return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
160 }
161
162 SAL_CALL operator oslThread() const
163 {
164 return m_hThread;
165 }
166
167 protected:
168
172 friend void SAL_CALL threadFunc( void* param);
173
174 virtual void SAL_CALL run() = 0;
175
176 virtual void SAL_CALL onTerminated()
177 {
178 }
179
180 private:
181 oslThread m_hThread;
182 };
183
184 extern "C" inline void SAL_CALL threadFunc( void* param)
185 {
186 Thread* pObj= (Thread*)param;
187 pObj->run();
188 pObj->onTerminated();
189 }
190
191 class ThreadData
192 {
193 ThreadData( const ThreadData& );
194 ThreadData& operator= (const ThreadData& );
195 public:
197 ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
198 {
199 m_hKey = osl_createThreadKey( pCallback );
200 }
201
203 ~ThreadData()
204 {
205 osl_destroyThreadKey(m_hKey);
206 }
207
211 sal_Bool SAL_CALL setData(void *pData)
212 {
213 return (osl_setThreadKeyData(m_hKey, pData));
214 }
215
220 void* SAL_CALL getData()
221 {
222 return osl_getThreadKeyData(m_hKey);
223 }
224
225 operator oslThreadKey() const
226 {
227 return m_hKey;
228 }
229
230 private:
231 oslThreadKey m_hKey;
232 };
233
234 } // end namespace osl
235
236 #endif
237
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
osl_setThreadName
SAL_DLLPUBLIC void osl_setThreadName(char const *name)
Attempts to set the name of the current thread.
osl_Thread_PriorityUnknown
Definition: thread.h:55
osl::Thread::resume
virtual void resume()
Definition: thread.hxx:100
thread.h
osl::Thread::createSuspended
sal_Bool createSuspended()
Definition: thread.hxx:84
osl::Thread::terminate
virtual void terminate()
Definition: thread.hxx:106
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::Thread::yield
static void yield()
Definition: thread.hxx:148
osl::Thread::join
virtual void join()
Definition: thread.hxx:112
TimeValue
Definition: time.h:42
osl::Thread::wait
static void wait(const TimeValue &Delay)
Definition: thread.hxx:143
osl::Thread::setPriority
void setPriority(oslThreadPriority Priority)
Definition: thread.hxx:122
SAL_THROW
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:356
osl::Thread::~Thread
virtual ~Thread()
Definition: thread.hxx:67
rtl_allocateMemory
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
osl_waitThread
SAL_DLLPUBLIC void osl_waitThread(const TimeValue *pDelay)
Blocks the calling thread at least for the given number of time.
osl_getThreadPriority
SAL_DLLPUBLIC oslThreadPriority osl_getThreadPriority(const oslThread Thread)
Retrieves the threads priority.
oslThreadIdentifier
sal_uInt32 oslThreadIdentifier
Definition: thread.h:60
oslThreadKeyCallbackFunction
void(* oslThreadKeyCallbackFunction)(void *)
Definition: thread.h:168
osl::ThreadData::getData
void * getData()
Get the data associated with the data key.
Definition: thread.hxx:220
osl::ThreadData::ThreadData
ThreadData(oslThreadKeyCallbackFunction pCallback=0)
Create a thread specific local data key.
Definition: thread.hxx:197
diagnose.h
osl_resumeThread
SAL_DLLPUBLIC void osl_resumeThread(oslThread Thread)
Wake-up a thread that was suspended with suspend() or createSuspended().
osl::Thread::suspend
virtual void suspend()
Definition: thread.hxx:94
osl::Thread::getIdentifier
oslThreadIdentifier getIdentifier() const
Definition: thread.hxx:133
osl_destroyThread
SAL_DLLPUBLIC void osl_destroyThread(oslThread Thread)
Release the thread handle.
osl_setThreadKeyData
SAL_DLLPUBLIC sal_Bool osl_setThreadKeyData(oslThreadKey Key, void *pData)
Set to key associated thread specific data.
osl::Thread::onTerminated
virtual void onTerminated()
Definition: thread.hxx:176
osl_terminateThread
SAL_DLLPUBLIC void osl_terminateThread(oslThread Thread)
The requested thread will get terminate the next time scheduleThread() is called. ...
osl::Thread::run
virtual void run()=0
osl_yieldThread
SAL_DLLPUBLIC void osl_yieldThread(void)
Offers the rest of the threads time-slice to the OS.
oslThread
void * oslThread
Opaque data type for threads.
Definition: thread.h:37
config.h
osl_suspendThread
SAL_DLLPUBLIC void osl_suspendThread(oslThread Thread)
Suspend the execution of the thread.
osl::Thread::isRunning
sal_Bool isRunning() const
Definition: thread.hxx:117
oslThreadPriority
oslThreadPriority
levels of thread-priority Note that oslThreadPriorityUnknown might be returned by getPriorityOfThread...
Definition: thread.h:48
osl_scheduleThread
SAL_DLLPUBLIC sal_Bool osl_scheduleThread(oslThread Thread)
Offers the rest of the threads time-slice to the OS.
osl_getThreadKeyData
SAL_DLLPUBLIC void * osl_getThreadKeyData(oslThreadKey Key)
Get to key associated thread specific data.
osl::ThreadData::setData
sal_Bool setData(void *pData)
Set the data associated with the data key.
Definition: thread.hxx:211
osl_setThreadPriority
SAL_DLLPUBLIC void osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority)
Changes the threads priority.
osl::Thread::threadFunc
friend void threadFunc(void *param)
The thread functions calls the protected functions run and onTerminated.
Definition: thread.hxx:184
osl::Thread::create
sal_Bool create()
Definition: thread.hxx:72
osl::ThreadData::~ThreadData
~ThreadData()
Destroy a thread specific local data key.
Definition: thread.hxx:203
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:184
osl::Thread::getPriority
oslThreadPriority getPriority() const
Definition: thread.hxx:128
osl::Thread::Thread
Thread()
Definition: thread.hxx:65
oslThreadKey
void * oslThreadKey
Definition: thread.h:62
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
osl::Thread::getCurrentIdentifier
static oslThreadIdentifier getCurrentIdentifier()
Definition: thread.hxx:138
osl::ThreadData
Definition: thread.hxx:191
time.h
osl::Thread::setName
static void setName(char const *name)
Definition: thread.hxx:153
osl_createSuspendedThread
SAL_DLLPUBLIC oslThread osl_createSuspendedThread(oslWorkerFunction pWorker, void *pThreadData)
Create the thread, using the function-ptr pWorker as its main (worker) function.
sal_Bool
unsigned char sal_Bool
Definition: types.h:37
osl_destroyThreadKey
SAL_DLLPUBLIC void osl_destroyThreadKey(oslThreadKey Key)
Destroy a key to an associated thread local storage pointer.
osl_createThreadKey
SAL_DLLPUBLIC oslThreadKey osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
Create a key to an associated thread local storage pointer.
sal_False
#define sal_False
Definition: types.h:38
osl_isThreadRunning
SAL_DLLPUBLIC sal_Bool osl_isThreadRunning(const oslThread Thread)
Returns True if the thread was created and has not terminated yet.
osl::Thread
A thread abstraction.
Definition: thread.hxx:50
osl_joinWithThread
SAL_DLLPUBLIC void osl_joinWithThread(oslThread Thread)
Blocks the calling thread until Thread has terminated.
osl::Thread::schedule
virtual sal_Bool schedule()
Definition: thread.hxx:157
alloc.h