Version 24.8.3.2, tag libreoffice-24.8.3.2
[LibreOffice.git] / include / osl / thread.hxx
blobdd3a4cbd318482cc4758160629f58609a9854de8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_OSL_THREAD_HXX
25 #define INCLUDED_OSL_THREAD_HXX
27 #include "sal/config.h"
29 #include <cassert>
30 #include <cstddef>
32 #include "osl/time.h"
33 #include "osl/thread.h"
34 #include "rtl/alloc.h"
36 namespace osl
38 /** threadFunc is the function which is executed by the threads
39 created by the osl::Thread class. The function's signature
40 matches the one of oslWorkerFunction which is declared in
41 osl/thread.h
43 extern "C" inline void SAL_CALL threadFunc( void* param);
45 /**
46 A thread abstraction.
48 @deprecated use ::salhelper::Thread instead. Only the static member
49 functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
50 ::osl::Thread::yield are not deprecated.
52 class Thread
54 Thread( const Thread& ) SAL_DELETED_FUNCTION;
55 Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
56 public:
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 * )
67 Thread(): m_hThread(NULL){}
69 virtual ~Thread() COVERITY_NOEXCEPT_FALSE
71 osl_destroyThread( m_hThread);
74 bool SAL_CALL create()
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)
80 return false;
82 osl_resumeThread(m_hThread);
83 return true;
86 bool SAL_CALL createSuspended()
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;
96 virtual void SAL_CALL suspend()
98 if( m_hThread )
99 osl_suspendThread(m_hThread);
102 virtual void SAL_CALL resume()
104 if( m_hThread )
105 osl_resumeThread(m_hThread);
108 virtual void SAL_CALL terminate()
110 if( m_hThread )
111 osl_terminateThread(m_hThread);
114 virtual void SAL_CALL join()
116 osl_joinWithThread(m_hThread);
119 bool SAL_CALL isRunning() const
121 return osl_isThreadRunning(m_hThread);
124 void SAL_CALL setPriority( oslThreadPriority Priority)
126 if( m_hThread )
127 osl_setThreadPriority(m_hThread, Priority);
130 oslThreadPriority SAL_CALL getPriority() const
132 return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
135 oslThreadIdentifier SAL_CALL getIdentifier() const
137 return osl_getThreadIdentifier(m_hThread);
140 static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
142 return osl_getThreadIdentifier(NULL);
145 static void SAL_CALL wait(const TimeValue& Delay)
147 osl_waitThread(&Delay);
150 static void SAL_CALL yield()
152 osl_yieldThread();
155 static void setName(char const * name) SAL_NOEXCEPT {
156 osl_setThreadName(name);
159 virtual bool SAL_CALL schedule()
161 return m_hThread && osl_scheduleThread(m_hThread);
164 SAL_CALL operator oslThread() const
166 return m_hThread;
169 protected:
171 /** The thread functions calls the protected functions
172 run and onTerminated.
174 friend void SAL_CALL threadFunc( void* param);
176 virtual void SAL_CALL run() = 0;
178 virtual void SAL_CALL onTerminated()
182 private:
183 oslThread m_hThread;
186 extern "C" inline void SAL_CALL threadFunc( void* param)
188 Thread* pObj= static_cast<Thread*>(param);
189 pObj->run();
190 pObj->onTerminated();
193 class ThreadData
195 ThreadData( const ThreadData& ) SAL_DELETED_FUNCTION;
196 ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
197 public:
198 /// Create a thread specific local data key
199 ThreadData( oslThreadKeyCallbackFunction pCallback= NULL )
201 m_hKey = osl_createThreadKey( pCallback );
204 /// Destroy a thread specific local data key
205 ~ThreadData()
207 osl_destroyThreadKey(m_hKey);
210 /** Set the data associated with the data key.
211 @returns True if operation was successful
213 bool SAL_CALL setData(void *pData)
215 return osl_setThreadKeyData(m_hKey, pData);
218 /** Get the data associated with the data key.
219 @returns The data associated with the data key or
220 NULL if no data was set
222 void* SAL_CALL getData()
224 return osl_getThreadKeyData(m_hKey);
227 operator oslThreadKey() const
229 return m_hKey;
232 private:
233 oslThreadKey m_hKey;
236 } // end namespace osl
238 #endif
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */