LibreOffice: osl/thread.hxx Source File

LibreOffice LibreOffice 7.5 SDK C/C++ API Reference

Loading...
Searching...
No Matches
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/*
21 * This file is part of LibreOffice published API.
22 */
23
24#ifndef INCLUDED_OSL_THREAD_HXX
25#define INCLUDED_OSL_THREAD_HXX
26
27#include "sal/config.h"
28
29#include <cassert>
30#include <cstddef>
31
32#include "osl/time.h"
33#include "osl/thread.h"
34#include "rtl/alloc.h"
35
36namespace osl
37{
43extern "C" inline void SAL_CALL threadFunc( void* param);
44
52 class Thread
53{
54 Thread( const Thread& ) SAL_DELETED_FUNCTION;
55 Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
56public:
57 // these are here to force memory de/allocation to sal lib.
58 static void * SAL_CALL operator new( size_t nSize )
59 { return ::rtl_allocateMemory( nSize ); }
60 static void SAL_CALL operator delete( void * pMem )
61 { ::rtl_freeMemory( pMem ); }
62 static void * SAL_CALL operator new( size_t, void * pMem )
63 { return pMem; }
64 static void SAL_CALL operator delete( void *, void * )
65 {}
66
67 Thread(): m_hThread(NULL){}
68
69 virtual ~Thread() COVERITY_NOEXCEPT_FALSE
70 {
71 osl_destroyThread( m_hThread);
72 }
73
74 bool SAL_CALL create()
75 {
76 assert(m_hThread == NULL); // only one running thread per instance
77 m_hThread = osl_createSuspendedThread( threadFunc, static_cast<void*>(this));
78 if (m_hThread == NULL)
79 {
80 return false;
81 }
82 osl_resumeThread(m_hThread);
83 return true;
84 }
85
86 bool SAL_CALL createSuspended()
87 {
88 assert(m_hThread == NULL); // only one running thread per instance
89 if( m_hThread)
90 return false;
91 m_hThread= osl_createSuspendedThread( threadFunc,
92 static_cast<void*>(this));
93 return m_hThread != NULL;
94 }
95
96 virtual void SAL_CALL suspend()
97 {
98 if( m_hThread )
99 osl_suspendThread(m_hThread);
100 }
101
102 virtual void SAL_CALL resume()
103 {
104 if( m_hThread )
105 osl_resumeThread(m_hThread);
106 }
107
108 virtual void SAL_CALL terminate()
109 {
110 if( m_hThread )
111 osl_terminateThread(m_hThread);
112 }
113
114 virtual void SAL_CALL join()
115 {
116 osl_joinWithThread(m_hThread);
117 }
118
119 bool SAL_CALL isRunning() const
120 {
121 return osl_isThreadRunning(m_hThread);
122 }
123
124 void SAL_CALL setPriority( oslThreadPriority Priority)
125 {
126 if( m_hThread )
127 osl_setThreadPriority(m_hThread, Priority);
128 }
129
130 oslThreadPriority SAL_CALL getPriority() const
131 {
132 return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
133 }
134
135 oslThreadIdentifier SAL_CALL getIdentifier() const
136 {
137 return osl_getThreadIdentifier(m_hThread);
138 }
139
140 static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
141 {
142 return osl_getThreadIdentifier(NULL);
143 }
144
145 static void SAL_CALL wait(const TimeValue& Delay)
146 {
147 osl_waitThread(&Delay);
148 }
149
150 static void SAL_CALL yield()
151 {
152 osl_yieldThread();
153 }
154
155 static void setName(char const * name) SAL_NOEXCEPT {
156 osl_setThreadName(name);
157 }
158
159 virtual bool SAL_CALL schedule()
160 {
161 return m_hThread && osl_scheduleThread(m_hThread);
162 }
163
164 SAL_CALL operator oslThread() const
165 {
166 return m_hThread;
167 }
168
169protected:
170
174 friend void SAL_CALL threadFunc( void* param);
175
176 virtual void SAL_CALL run() = 0;
177
178 virtual void SAL_CALL onTerminated()
179 {
180 }
181
182private:
183 oslThread m_hThread;
184};
185
186 extern "C" inline void SAL_CALL threadFunc( void* param)
187{
188 Thread* pObj= static_cast<Thread*>(param);
189 pObj->run();
190 pObj->onTerminated();
191}
192
193 class ThreadData
194{
195 ThreadData( const ThreadData& ) SAL_DELETED_FUNCTION;
196 ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
197public:
199 ThreadData( oslThreadKeyCallbackFunction pCallback= NULL )
200 {
201 m_hKey = osl_createThreadKey( pCallback );
202 }
203
205 ~ThreadData()
206 {
207 osl_destroyThreadKey(m_hKey);
208 }
209
213 bool SAL_CALL setData(void *pData)
214 {
215 return osl_setThreadKeyData(m_hKey, pData);
216 }
217
222 void* SAL_CALL getData()
223 {
224 return osl_getThreadKeyData(m_hKey);
225 }
226
227 operator oslThreadKey() const
228 {
229 return m_hKey;
230 }
231
232private:
233 oslThreadKey m_hKey;
234};
235
236} // end namespace osl
237
238#endif
239
240/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
config.h
SAL_NOEXCEPT
#define SAL_NOEXCEPT
Macro for C++11 "noexcept" vs.
Definition: types.h:416
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
COVERITY_NOEXCEPT_FALSE
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won't throw in practice,...
Definition: types.h:349
alloc.h
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
time.h
thread.h
osl_terminateThread
SAL_DLLPUBLIC void osl_terminateThread(oslThread Thread)
The requested thread will get terminate the next time scheduleThread() is called.
osl_setThreadPriority
SAL_DLLPUBLIC void osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority)
Changes the threads priority.
osl_resumeThread
SAL_DLLPUBLIC void osl_resumeThread(oslThread Thread)
Wake-up a thread that was suspended with suspend() or createSuspended().
osl_createSuspendedThread
SAL_DLLPUBLIC oslThread osl_createSuspendedThread(oslWorkerFunction pWorker, void *pThreadData)
Create the thread, using the function-ptr pWorker as its main (worker) function.
oslThreadKey
void * oslThreadKey
Definition: thread.h:66
osl_scheduleThread
SAL_DLLPUBLIC sal_Bool osl_scheduleThread(oslThread Thread)
Schedules in thread to wait till after time slice of specified thread.
osl_setThreadName
SAL_DLLPUBLIC void osl_setThreadName(char const *name)
Attempts to set the name of the current thread.
osl_yieldThread
SAL_DLLPUBLIC void osl_yieldThread(void)
Offers the rest of the threads time-slice to the OS.
osl_destroyThread
SAL_DLLPUBLIC void osl_destroyThread(oslThread Thread)
Release the thread handle.
oslThreadKeyCallbackFunction
void(* oslThreadKeyCallbackFunction)(void *)
Definition: thread.h:204
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...
oslThreadPriority
oslThreadPriority
levels of thread-priority Note that oslThreadPriorityUnknown might be returned by getPriorityOfThread...
Definition: thread.h:53
osl_Thread_PriorityUnknown
@ osl_Thread_PriorityUnknown
Definition: thread.h:59
osl_joinWithThread
SAL_DLLPUBLIC void osl_joinWithThread(oslThread Thread)
Blocks the calling thread until Thread has terminated.
osl_destroyThreadKey
SAL_DLLPUBLIC void osl_destroyThreadKey(oslThreadKey Key)
Destroy a key to an associated thread local storage pointer.
osl_getThreadKeyData
SAL_DLLPUBLIC void * osl_getThreadKeyData(oslThreadKey Key)
Get to key associated thread specific data.
oslThreadIdentifier
sal_uInt32 oslThreadIdentifier
Definition: thread.h:64
osl_setThreadKeyData
SAL_DLLPUBLIC sal_Bool osl_setThreadKeyData(oslThreadKey Key, void *pData)
Set to key associated thread specific data.
oslThread
void * oslThread
Opaque data type for threads.
Definition: thread.h:41
osl_isThreadRunning
SAL_DLLPUBLIC sal_Bool osl_isThreadRunning(const oslThread Thread)
Returns True if the thread was created and has not terminated yet.
osl_suspendThread
SAL_DLLPUBLIC void osl_suspendThread(oslThread Thread)
Suspend the execution of the thread.
osl_getThreadPriority
SAL_DLLPUBLIC oslThreadPriority osl_getThreadPriority(const oslThread Thread)
Retrieves the threads priority.
osl_createThreadKey
SAL_DLLPUBLIC oslThreadKey osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
Create a key to an associated thread local storage pointer.
osl_waitThread
SAL_DLLPUBLIC void osl_waitThread(const TimeValue *pDelay)
Suspends the execution of the calling thread for at least the given time.
osl
Definition: condition.hxx:31
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:186
osl::Thread
A thread abstraction.
Definition: thread.hxx:53
osl::Thread::getIdentifier
oslThreadIdentifier getIdentifier() const
Definition: thread.hxx:135
osl::Thread::resume
virtual void resume()
Definition: thread.hxx:102
osl::Thread::isRunning
bool isRunning() const
Definition: thread.hxx:119
osl::Thread::Thread
Thread()
Definition: thread.hxx:67
osl::Thread::getCurrentIdentifier
static oslThreadIdentifier getCurrentIdentifier()
Definition: thread.hxx:140
osl::Thread::run
virtual void run()=0
osl::Thread::~Thread
virtual ~Thread() COVERITY_NOEXCEPT_FALSE
Definition: thread.hxx:69
osl::Thread::yield
static void yield()
Definition: thread.hxx:150
osl::Thread::create
bool create()
Definition: thread.hxx:74
osl::Thread::schedule
virtual bool schedule()
Definition: thread.hxx:159
osl::Thread::getPriority
oslThreadPriority getPriority() const
Definition: thread.hxx:130
osl::Thread::join
virtual void join()
Definition: thread.hxx:114
osl::Thread::setName
static void setName(char const *name) SAL_NOEXCEPT
Definition: thread.hxx:155
osl::Thread::wait
static void wait(const TimeValue &Delay)
Definition: thread.hxx:145
osl::Thread::createSuspended
bool createSuspended()
Definition: thread.hxx:86
osl::Thread::threadFunc
friend void threadFunc(void *param)
The thread functions calls the protected functions run and onTerminated.
Definition: thread.hxx:186
osl::Thread::setPriority
void setPriority(oslThreadPriority Priority)
Definition: thread.hxx:124
osl::Thread::terminate
virtual void terminate()
Definition: thread.hxx:108
osl::Thread::suspend
virtual void suspend()
Definition: thread.hxx:96
osl::Thread::onTerminated
virtual void onTerminated()
Definition: thread.hxx:178
osl::ThreadData
Definition: thread.hxx:194
osl::ThreadData::~ThreadData
~ThreadData()
Destroy a thread specific local data key.
Definition: thread.hxx:205
osl::ThreadData::setData
bool setData(void *pData)
Set the data associated with the data key.
Definition: thread.hxx:213
osl::ThreadData::ThreadData
ThreadData(oslThreadKeyCallbackFunction pCallback=NULL)
Create a thread specific local data key.
Definition: thread.hxx:199
osl::ThreadData::getData
void * getData()
Get the data associated with the data key.
Definition: thread.hxx:222
TimeValue
Time since Jan-01-1970.
Definition: time.h:76