LibreOffice
LibreOffice 5.0 SDK C/C++ API Reference
 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 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 {
50  Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
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;
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 
126  {
127  return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
128  }
129 
131  {
132  return osl_getThreadIdentifier(m_hThread);
133  }
134 
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= static_cast<Thread*>(param);
184  pObj->run();
185  pObj->onTerminated();
186 }
187 
189 {
191  ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
192 public:
195  {
196  m_hKey = osl_createThreadKey( pCallback );
197  }
198 
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: */
SAL_DLLPUBLIC void osl_destroyThreadKey(oslThreadKey Key)
Destroy a key to an associated thread local storage pointer.
SAL_DLLPUBLIC void osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority)
Changes the threads priority.
virtual void run()=0
static void yield()
Definition: thread.hxx:145
void(* oslThreadKeyCallbackFunction)(void *)
Definition: thread.h:168
void setPriority(oslThreadPriority Priority)
Definition: thread.hxx:119
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:404
SAL_DLLPUBLIC void osl_yieldThread(void)
Offers the rest of the threads time-slice to the OS.
SAL_DLLPUBLIC sal_Bool osl_setThreadKeyData(oslThreadKey Key, void *pData)
Set to key associated thread specific data.
SAL_DLLPUBLIC void osl_suspendThread(oslThread Thread)
Suspend the execution of the thread.
~ThreadData()
Destroy a thread specific local data key.
Definition: thread.hxx:200
SAL_DLLPUBLIC void osl_setThreadName(char const *name)
Attempts to set the name of the current thread.
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
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...
void * oslThread
Opaque data type for threads.
Definition: thread.h:37
virtual void onTerminated()
Definition: thread.hxx:173
static oslThreadIdentifier getCurrentIdentifier()
Definition: thread.hxx:135
SAL_DLLPUBLIC sal_Bool osl_scheduleThread(oslThread Thread)
Offers the rest of the threads time-slice to the OS.
bool createSuspended()
Definition: thread.hxx:81
virtual void join()
Definition: thread.hxx:109
sal_uInt32 oslThreadIdentifier
Definition: thread.h:60
virtual void resume()
Definition: thread.hxx:97
SAL_DLLPUBLIC void osl_resumeThread(oslThread Thread)
Wake-up a thread that was suspended with suspend() or createSuspended().
friend void threadFunc(void *param)
The thread functions calls the protected functions run and onTerminated.
SAL_DLLPUBLIC void osl_destroyThread(oslThread Thread)
Release the thread handle.
Definition: thread.hxx:188
SAL_DLLPUBLIC oslThreadKey osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
Create a key to an associated thread local storage pointer.
SAL_DLLPUBLIC oslThread osl_createSuspendedThread(oslWorkerFunction pWorker, void *pThreadData)
Create the thread, using the function-ptr pWorker as its main (worker) function.
virtual void terminate()
Definition: thread.hxx:103
void * oslThreadKey
Definition: thread.h:62
bool setData(void *pData)
Set the data associated with the data key.
Definition: thread.hxx:208
virtual ~Thread()
Definition: thread.hxx:64
SAL_DLLPUBLIC void osl_terminateThread(oslThread Thread)
The requested thread will get terminate the next time scheduleThread() is called. ...
SAL_DLLPUBLIC sal_Bool osl_isThreadRunning(const oslThread Thread)
Returns True if the thread was created and has not terminated yet.
bool isRunning() const
Definition: thread.hxx:114
static void wait(const TimeValue &Delay)
Definition: thread.hxx:140
oslThreadPriority
levels of thread-priority Note that oslThreadPriorityUnknown might be returned by getPriorityOfThread...
Definition: thread.h:48
Definition: thread.h:55
SAL_DLLPUBLIC void osl_joinWithThread(oslThread Thread)
Blocks the calling thread until Thread has terminated.
oslThreadIdentifier getIdentifier() const
Definition: thread.hxx:130
SAL_DLLPUBLIC oslThreadPriority osl_getThreadPriority(const oslThread Thread)
Retrieves the threads priority.
Definition: time.h:42
ThreadData(oslThreadKeyCallbackFunction pCallback=0)
Create a thread specific local data key.
Definition: thread.hxx:194
SAL_DLLPUBLIC void osl_waitThread(const TimeValue *pDelay)
Blocks the calling thread at least for the given number of time.
static void setName(char const *name)
Definition: thread.hxx:150
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
A thread abstraction.
Definition: thread.hxx:47
virtual void suspend()
Definition: thread.hxx:91
bool create()
Definition: thread.hxx:69
virtual bool schedule()
Definition: thread.hxx:154
Thread()
Definition: thread.hxx:62
void threadFunc(void *param)
threadFunc is the function which is executed by the threads created by the osl::Thread class...
Definition: thread.hxx:181
void * getData()
Get the data associated with the data key.
Definition: thread.hxx:217
oslThreadPriority getPriority() const
Definition: thread.hxx:125
SAL_DLLPUBLIC void * osl_getThreadKeyData(oslThreadKey Key)
Get to key associated thread specific data.