LibreOffice
LibreOffice 7.1 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
32namespace osl
33{
39extern "C" inline void SAL_CALL threadFunc( void* param);
40
48class Thread
49{
51 Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
52public:
53 // these are here to force memory de/allocation to sal lib.
54 static void * SAL_CALL operator new( size_t nSize )
55 { return ::rtl_allocateMemory( nSize ); }
56 static void SAL_CALL operator delete( void * pMem )
57 { ::rtl_freeMemory( pMem ); }
58 static void * SAL_CALL operator new( size_t, void * pMem )
59 { return pMem; }
60 static void SAL_CALL operator delete( void *, void * )
61 {}
62
63 Thread(): m_hThread(NULL){}
64
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;
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
127 {
128 return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
129 }
130
132 {
133 return osl_getThreadIdentifier(m_hThread);
134 }
135
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 {
149 }
150
151 static 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
165protected:
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
178private:
179 oslThread m_hThread;
180};
181
182extern "C" inline void SAL_CALL threadFunc( void* param)
183{
184 Thread* pObj= static_cast<Thread*>(param);
185 pObj->run();
186 pObj->onTerminated();
187}
188
190{
192 ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
193public:
196 {
197 m_hKey = osl_createThreadKey( pCallback );
198 }
199
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
228private:
229 oslThreadKey m_hKey;
230};
231
232} // end namespace osl
233
234#endif
235
236/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:374
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won't throw in practice,...
Definition: types.h:333
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
SAL_DLLPUBLIC void osl_terminateThread(oslThread Thread)
The requested thread will get terminate the next time scheduleThread() is called.
SAL_DLLPUBLIC void osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority)
Changes the threads priority.
SAL_DLLPUBLIC void osl_resumeThread(oslThread Thread)
Wake-up a thread that was suspended with suspend() or createSuspended().
SAL_DLLPUBLIC oslThread osl_createSuspendedThread(oslWorkerFunction pWorker, void *pThreadData)
Create the thread, using the function-ptr pWorker as its main (worker) function.
void * oslThreadKey
Definition: thread.h:62
SAL_DLLPUBLIC sal_Bool osl_scheduleThread(oslThread Thread)
Schedules in thread to wait till after time slice of specified thread.
SAL_DLLPUBLIC void osl_setThreadName(char const *name)
Attempts to set the name of the current thread.
SAL_DLLPUBLIC void osl_yieldThread(void)
Offers the rest of the threads time-slice to the OS.
SAL_DLLPUBLIC void osl_destroyThread(oslThread Thread)
Release the thread handle.
void(* oslThreadKeyCallbackFunction)(void *)
Definition: thread.h:200
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...
oslThreadPriority
levels of thread-priority Note that oslThreadPriorityUnknown might be returned by getPriorityOfThread...
Definition: thread.h:49
@ osl_Thread_PriorityUnknown
Definition: thread.h:55
SAL_DLLPUBLIC void osl_joinWithThread(oslThread Thread)
Blocks the calling thread until Thread has terminated.
SAL_DLLPUBLIC void osl_destroyThreadKey(oslThreadKey Key)
Destroy a key to an associated thread local storage pointer.
SAL_DLLPUBLIC void * osl_getThreadKeyData(oslThreadKey Key)
Get to key associated thread specific data.
sal_uInt32 oslThreadIdentifier
Definition: thread.h:60
SAL_DLLPUBLIC sal_Bool osl_setThreadKeyData(oslThreadKey Key, void *pData)
Set to key associated thread specific data.
void * oslThread
Opaque data type for threads.
Definition: thread.h:37
SAL_DLLPUBLIC sal_Bool osl_isThreadRunning(const oslThread Thread)
Returns True if the thread was created and has not terminated yet.
SAL_DLLPUBLIC void osl_suspendThread(oslThread Thread)
Suspend the execution of the thread.
SAL_DLLPUBLIC oslThreadPriority osl_getThreadPriority(const oslThread Thread)
Retrieves the threads priority.
SAL_DLLPUBLIC oslThreadKey osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
Create a key to an associated thread local storage pointer.
SAL_DLLPUBLIC void osl_waitThread(const TimeValue *pDelay)
Suspends the execution of the calling thread for at least the given time.
Definition: component.hxx:30
void threadFunc(void *param)
threadFunc is the function which is executed by the threads created by the osl::Thread class.
Definition: thread.hxx:182
A thread abstraction.
Definition: thread.hxx:49
oslThreadIdentifier getIdentifier() const
Definition: thread.hxx:131
virtual void resume()
Definition: thread.hxx:98
bool isRunning() const
Definition: thread.hxx:115
Thread()
Definition: thread.hxx:63
static oslThreadIdentifier getCurrentIdentifier()
Definition: thread.hxx:136
virtual void run()=0
virtual ~Thread() COVERITY_NOEXCEPT_FALSE
Definition: thread.hxx:65
static void yield()
Definition: thread.hxx:146
static void setName(char const *name)
Definition: thread.hxx:151
bool create()
Definition: thread.hxx:70
virtual bool schedule()
Definition: thread.hxx:155
oslThreadPriority getPriority() const
Definition: thread.hxx:126
virtual void join()
Definition: thread.hxx:110
static void wait(const TimeValue &Delay)
Definition: thread.hxx:141
bool createSuspended()
Definition: thread.hxx:82
friend void threadFunc(void *param)
The thread functions calls the protected functions run and onTerminated.
Definition: thread.hxx:182
void setPriority(oslThreadPriority Priority)
Definition: thread.hxx:120
virtual void terminate()
Definition: thread.hxx:104
virtual void suspend()
Definition: thread.hxx:92
virtual void onTerminated()
Definition: thread.hxx:174
Definition: thread.hxx:190
~ThreadData()
Destroy a thread specific local data key.
Definition: thread.hxx:201
bool setData(void *pData)
Set the data associated with the data key.
Definition: thread.hxx:209
ThreadData(oslThreadKeyCallbackFunction pCallback=NULL)
Create a thread specific local data key.
Definition: thread.hxx:195
void * getData()
Get the data associated with the data key.
Definition: thread.hxx:218
Definition: time.h:66