LibreOffice LibreOffice 4.4 SDK C/C++ API Reference

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

- File List
- File Members

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
27 #include <osl/time.h>
28 #include <osl/thread.h>
29 #include <rtl/alloc.h>
30
31 namespace osl
32 {
38 extern "C" inline void SAL_CALL threadFunc( void* param);
39
47 class Thread
48 {
49 Thread( const Thread& );
50 Thread& operator= ( const Thread& );
51 public:
52 // these are here to force memory de/allocation to sal lib.
53 inline static void * SAL_CALL operator new( size_t nSize )
54 { return ::rtl_allocateMemory( nSize ); }
55 inline static void SAL_CALL operator delete( void * pMem )
56 { ::rtl_freeMemory( pMem ); }
57 inline static void * SAL_CALL operator new( size_t, void * pMem )
58 { return pMem; }
59 inline static void SAL_CALL operator delete( void *, void * )
60 {}
61
62 Thread(): m_hThread(0){}
63
64 virtual ~Thread()
65 {
66 osl_destroyThread( m_hThread);
67 }
68
69 bool SAL_CALL create()
70 {
71 assert(m_hThread == 0); // only one running thread per instance
72 m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
73 if (m_hThread == 0)
74 {
75 return false;
76 }
77 osl_resumeThread(m_hThread);
78 return true;
79 }
80
81 bool SAL_CALL createSuspended()
82 {
83 assert(m_hThread == 0); // only one running thread per instance
84 if( m_hThread)
85 return false;
86 m_hThread= osl_createSuspendedThread( threadFunc,
87 (void*)this);
88 return m_hThread != 0;
89 }
90
91 virtual void SAL_CALL suspend()
92 {
93 if( m_hThread )
94 osl_suspendThread(m_hThread);
95 }
96
97 virtual void SAL_CALL resume()
98 {
99 if( m_hThread )
100 osl_resumeThread(m_hThread);
101 }
102
103 virtual void SAL_CALL terminate()
104 {
105 if( m_hThread )
106 osl_terminateThread(m_hThread);
107 }
108
109 virtual void SAL_CALL join()
110 {
111 osl_joinWithThread(m_hThread);
112 }
113
114 bool SAL_CALL isRunning() const
115 {
116 return osl_isThreadRunning(m_hThread);
117 }
118
119 void SAL_CALL setPriority( oslThreadPriority Priority)
120 {
121 if( m_hThread )
122 osl_setThreadPriority(m_hThread, Priority);
123 }
124
125 oslThreadPriority SAL_CALL getPriority() const
126 {
127 return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
128 }
129
130 oslThreadIdentifier SAL_CALL getIdentifier() const
131 {
132 return osl_getThreadIdentifier(m_hThread);
133 }
134
135 static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
136 {
137 return osl_getThreadIdentifier(0);
138 }
139
140 static void SAL_CALL wait(const TimeValue& Delay)
141 {
142 osl_waitThread(&Delay);
143 }
144
145 static void SAL_CALL yield()
146 {
147 osl_yieldThread();
148 }
149
150 static inline void setName(char const * name) throw () {
151 osl_setThreadName(name);
152 }
153
154 virtual bool SAL_CALL schedule()
155 {
156 return m_hThread && osl_scheduleThread(m_hThread);
157 }
158
159 SAL_CALL operator oslThread() const
160 {
161 return m_hThread;
162 }
163
164 protected:
165
169 friend void SAL_CALL threadFunc( void* param);
170
171 virtual void SAL_CALL run() = 0;
172
173 virtual void SAL_CALL onTerminated()
174 {
175 }
176
177 private:
178 oslThread m_hThread;
179 };
180
181 extern "C" inline void SAL_CALL threadFunc( void* param)
182 {
183 Thread* pObj= (Thread*)param;
184 pObj->run();
185 pObj->onTerminated();
186 }
187
188 class ThreadData
189 {
190 ThreadData( const ThreadData& );
191 ThreadData& operator= (const ThreadData& );
192 public:
194 ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
195 {
196 m_hKey = osl_createThreadKey( pCallback );
197 }
198
200 ~ThreadData()
201 {
202 osl_destroyThreadKey(m_hKey);
203 }
204
208 bool SAL_CALL setData(void *pData)
209 {
210 return (osl_setThreadKeyData(m_hKey, pData));
211 }
212
217 void* SAL_CALL getData()
218 {
219 return osl_getThreadKeyData(m_hKey);
220 }
221
222 operator oslThreadKey() const
223 {
224 return m_hKey;
225 }
226
227 private:
228 oslThreadKey m_hKey;
229 };
230
231 } // end namespace osl
232
233 #endif
234
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
osl_destroyThreadKey
SAL_DLLPUBLIC void osl_destroyThreadKey(oslThreadKey Key)
Destroy a key to an associated thread local storage pointer.
osl_setThreadPriority
SAL_DLLPUBLIC void osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority)
Changes the threads priority.
osl::Thread::run
virtual void run()=0
osl::Thread::yield
static void yield()
Definition: thread.hxx:145
osl::Thread::setPriority
void setPriority(oslThreadPriority Priority)
Definition: thread.hxx:119
osl_yieldThread
SAL_DLLPUBLIC void osl_yieldThread(void)
Offers the rest of the threads time-slice to the OS.
alloc.h
osl_setThreadKeyData
SAL_DLLPUBLIC sal_Bool osl_setThreadKeyData(oslThreadKey Key, void *pData)
Set to key associated thread specific data.
osl_suspendThread
SAL_DLLPUBLIC void osl_suspendThread(oslThread Thread)
Suspend the execution of the thread.
thread.h
osl::ThreadData::~ThreadData
~ThreadData()
Destroy a thread specific local data key.
Definition: thread.hxx:200
osl_setThreadName
SAL_DLLPUBLIC void osl_setThreadName(char const *name)
Attempts to set the name of the current thread.
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
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...
oslThread
void * oslThread
Opaque data type for threads.
Definition: thread.h:37
osl::Thread::onTerminated
virtual void onTerminated()
Definition: thread.hxx:173
osl::Thread::getCurrentIdentifier
static oslThreadIdentifier getCurrentIdentifier()
Definition: thread.hxx:135
osl_scheduleThread
SAL_DLLPUBLIC sal_Bool osl_scheduleThread(oslThread Thread)
Offers the rest of the threads time-slice to the OS.
osl::Thread::createSuspended
bool createSuspended()
Definition: thread.hxx:81
osl::Thread::join
virtual void join()
Definition: thread.hxx:109
oslThreadIdentifier
sal_uInt32 oslThreadIdentifier
Definition: thread.h:60
osl::Thread::resume
virtual void resume()
Definition: thread.hxx:97
time.h
osl_resumeThread
SAL_DLLPUBLIC void osl_resumeThread(oslThread Thread)
Wake-up a thread that was suspended with suspend() or createSuspended().
osl::Thread::threadFunc
friend void threadFunc(void *param)
The thread functions calls the protected functions run and onTerminated.
Definition: thread.hxx:181
osl_destroyThread
SAL_DLLPUBLIC void osl_destroyThread(oslThread Thread)
Release the thread handle.
osl::ThreadData
Definition: thread.hxx:188
osl_createThreadKey
SAL_DLLPUBLIC oslThreadKey osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
Create a key to an associated thread local storage pointer.
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::Thread::terminate
virtual void terminate()
Definition: thread.hxx:103
oslThreadKey
void * oslThreadKey
Definition: thread.h:62
osl::ThreadData::setData
bool setData(void *pData)
Set the data associated with the data key.
Definition: thread.hxx:208
osl::Thread::~Thread
virtual ~Thread()
Definition: thread.hxx:64
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.
osl::Thread::isRunning
bool isRunning() const
Definition: thread.hxx:114
osl
Definition: conditn.hxx:28
osl::Thread::wait
static void wait(const TimeValue &Delay)
Definition: thread.hxx:140
oslThreadPriority
oslThreadPriority
levels of thread-priority Note that oslThreadPriorityUnknown might be returned by getPriorityOfThread...
Definition: thread.h:48
osl_Thread_PriorityUnknown
Definition: thread.h:55
osl_joinWithThread
SAL_DLLPUBLIC void osl_joinWithThread(oslThread Thread)
Blocks the calling thread until Thread has terminated.
osl::Thread::getIdentifier
oslThreadIdentifier getIdentifier() const
Definition: thread.hxx:130
osl_getThreadPriority
SAL_DLLPUBLIC oslThreadPriority osl_getThreadPriority(const oslThread Thread)
Retrieves the threads priority.
TimeValue
Definition: time.h:42
osl::ThreadData::ThreadData
ThreadData(oslThreadKeyCallbackFunction pCallback=0)
Create a thread specific local data key.
Definition: thread.hxx:194
osl_waitThread
SAL_DLLPUBLIC void osl_waitThread(const TimeValue *pDelay)
Blocks the calling thread at least for the given number of time.
osl::Thread::setName
static void setName(char const *name)
Definition: thread.hxx:150
rtl_allocateMemory
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
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:181
osl::Thread
A thread abstraction.
Definition: thread.hxx:47
osl::Thread::suspend
virtual void suspend()
Definition: thread.hxx:91
osl::Thread::create
bool create()
Definition: thread.hxx:69
osl::Thread::schedule
virtual bool schedule()
Definition: thread.hxx:154
osl::Thread::Thread
Thread()
Definition: thread.hxx:62
osl::ThreadData::getData
void * getData()
Get the data associated with the data key.
Definition: thread.hxx:217
osl::Thread::getPriority
oslThreadPriority getPriority() const
Definition: thread.hxx:125
config.h
oslThreadKeyCallbackFunction
void(* oslThreadKeyCallbackFunction)(void *)
Definition: thread.h:168
osl_getThreadKeyData
SAL_DLLPUBLIC void * osl_getThreadKeyData(oslThreadKey Key)
Get to key associated thread specific data.