AlterOffice
AlterOffice 3.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
4 #ifndef INCLUDED_OSL_THREAD_HXX
5 #define INCLUDED_OSL_THREAD_HXX
6 
7 #include "sal/config.h"
8 
9 #include <cassert>
10 #include <cstddef>
11 
12 #include "osl/time.h"
13 #include "osl/thread.h"
14 #include "rtl/alloc.h"
15 
16 namespace osl
17 {
23 extern "C" inline void SAL_CALL threadFunc( void* param);
24 
32 class Thread
33 {
35  Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
36 public:
37  // these are here to force memory de/allocation to sal lib.
38  static void * SAL_CALL operator new( size_t nSize )
39  { return ::rtl_allocateMemory( nSize ); }
40  static void SAL_CALL operator delete( void * pMem )
41  { ::rtl_freeMemory( pMem ); }
42  static void * SAL_CALL operator new( size_t, void * pMem )
43  { return pMem; }
44  static void SAL_CALL operator delete( void *, void * )
45  {}
46 
47  Thread(): m_hThread(NULL){}
48 
50  {
51  osl_destroyThread( m_hThread);
52  }
53 
54  bool SAL_CALL create()
55  {
56  assert(m_hThread == NULL); // only one running thread per instance
57  m_hThread = osl_createSuspendedThread( threadFunc, static_cast<void*>(this));
58  if (m_hThread == NULL)
59  {
60  return false;
61  }
62  osl_resumeThread(m_hThread);
63  return true;
64  }
65 
66  bool SAL_CALL createSuspended()
67  {
68  assert(m_hThread == NULL); // only one running thread per instance
69  if( m_hThread)
70  return false;
72  static_cast<void*>(this));
73  return m_hThread != NULL;
74  }
75 
76  virtual void SAL_CALL suspend()
77  {
78  if( m_hThread )
79  osl_suspendThread(m_hThread);
80  }
81 
82  virtual void SAL_CALL resume()
83  {
84  if( m_hThread )
85  osl_resumeThread(m_hThread);
86  }
87 
88  virtual void SAL_CALL terminate()
89  {
90  if( m_hThread )
91  osl_terminateThread(m_hThread);
92  }
93 
94  virtual void SAL_CALL join()
95  {
96  osl_joinWithThread(m_hThread);
97  }
98 
99  bool SAL_CALL isRunning() const
100  {
101  return osl_isThreadRunning(m_hThread);
102  }
103 
104  void SAL_CALL setPriority( oslThreadPriority Priority)
105  {
106  if( m_hThread )
107  osl_setThreadPriority(m_hThread, Priority);
108  }
109 
111  {
112  return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
113  }
114 
116  {
117  return osl_getThreadIdentifier(m_hThread);
118  }
119 
121  {
122  return osl_getThreadIdentifier(NULL);
123  }
124 
125  static void SAL_CALL wait(const TimeValue& Delay)
126  {
127  osl_waitThread(&Delay);
128  }
129 
130  static void SAL_CALL yield()
131  {
132  osl_yieldThread();
133  }
134 
135  static void setName(char const * name) SAL_NOEXCEPT {
136  osl_setThreadName(name);
137  }
138 
139  virtual bool SAL_CALL schedule()
140  {
141  return m_hThread && osl_scheduleThread(m_hThread);
142  }
143 
144  SAL_CALL operator oslThread() const
145  {
146  return m_hThread;
147  }
148 
149 protected:
150 
154  friend void SAL_CALL threadFunc( void* param);
155 
156  virtual void SAL_CALL run() = 0;
157 
158  virtual void SAL_CALL onTerminated()
159  {
160  }
161 
162 private:
163  oslThread m_hThread;
164 };
165 
166 extern "C" inline void SAL_CALL threadFunc( void* param)
167 {
168  Thread* pObj= static_cast<Thread*>(param);
169  pObj->run();
170  pObj->onTerminated();
171 }
172 
174 {
176  ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
177 public:
180  {
181  m_hKey = osl_createThreadKey( pCallback );
182  }
183 
186  {
187  osl_destroyThreadKey(m_hKey);
188  }
189 
193  bool SAL_CALL setData(void *pData)
194  {
195  return osl_setThreadKeyData(m_hKey, pData);
196  }
197 
202  void* SAL_CALL getData()
203  {
204  return osl_getThreadKeyData(m_hKey);
205  }
206 
207  operator oslThreadKey() const
208  {
209  return m_hKey;
210  }
211 
212 private:
213  oslThreadKey m_hKey;
214 };
215 
216 } // end namespace osl
217 
218 #endif
219 
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC sal_Bool osl_scheduleThread(oslThread Thread)
Schedules in thread to wait till after time slice of specified thread.
oslThreadPriority
levels of thread-priority Note that oslThreadPriorityUnknown might be returned by getPriorityOfThread...
Definition: thread.h:32
friend void threadFunc(void *param)
The thread functions calls the protected functions run and onTerminated.
static void setName(char const *name) SAL_NOEXCEPT
Definition: thread.hxx:135
void(* oslThreadKeyCallbackFunction)(void *)
Definition: thread.h:184
SAL_DLLPUBLIC oslThread osl_createSuspendedThread(oslWorkerFunction pWorker, void *pThreadData)
Create the thread, using the function-ptr pWorker as its main (worker) function.
Time since Jan-01-1970.
Definition: time.h:56
SAL_DLLPUBLIC void osl_resumeThread(oslThread Thread)
Wake-up a thread that was suspended with suspend() or createSuspended().
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
sal_uInt32 oslThreadIdentifier
Definition: thread.h:44
oslThreadPriority getPriority() const
Definition: thread.hxx:110
virtual void join()
Definition: thread.hxx:94
virtual void resume()
Definition: thread.hxx:82
bool isRunning() const
Definition: thread.hxx:99
virtual void onTerminated()
Definition: thread.hxx:158
ThreadData(oslThreadKeyCallbackFunction pCallback=NULL)
Create a thread specific local data key.
Definition: thread.hxx:179
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:329
bool setData(void *pData)
Set the data associated with the data key.
Definition: thread.hxx:193
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.
~ThreadData()
Destroy a thread specific local data key.
Definition: thread.hxx:185
SAL_DLLPUBLIC oslThreadPriority osl_getThreadPriority(const oslThread Thread)
Retrieves the threads priority.
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...
Definition: thread.hxx:173
virtual void suspend()
Definition: thread.hxx:76
void threadFunc(void *param)
threadFunc is the function which is executed by the threads created by the osl::Thread class...
Definition: thread.hxx:166
Thread()
Definition: thread.hxx:47
SAL_DLLPUBLIC oslThreadKey osl_createThreadKey(oslThreadKeyCallbackFunction pCallback)
Create a key to an associated thread local storage pointer.
static oslThreadIdentifier getCurrentIdentifier()
Definition: thread.hxx:120
virtual void terminate()
Definition: thread.hxx:88
SAL_DLLPUBLIC void * osl_getThreadKeyData(oslThreadKey Key)
Get to key associated thread specific data.
virtual ~Thread() COVERITY_NOEXCEPT_FALSE
Definition: thread.hxx:49
void * getData()
Get the data associated with the data key.
Definition: thread.hxx:202
SAL_DLLPUBLIC sal_Bool osl_isThreadRunning(const oslThread Thread)
Returns True if the thread was created and has not terminated yet.
#define SAL_NOEXCEPT
Macro for C++11 &quot;noexcept&quot; vs.
Definition: types.h:396
virtual void run()=0
static void wait(const TimeValue &Delay)
Definition: thread.hxx:125
static void yield()
Definition: thread.hxx:130
virtual bool schedule()
Definition: thread.hxx:139
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.
void * oslThreadKey
Definition: thread.h:46
bool createSuspended()
Definition: thread.hxx:66
SAL_DLLPUBLIC void osl_terminateThread(oslThread Thread)
The requested thread will get terminate the next time scheduleThread() is called. ...
void setPriority(oslThreadPriority Priority)
Definition: thread.hxx:104
SAL_DLLPUBLIC void osl_setThreadName(char const *name)
Attempts to set the name of the current thread.
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
A thread abstraction.
Definition: thread.hxx:32
Definition: thread.h:39
oslThreadIdentifier getIdentifier() const
Definition: thread.hxx:115
SAL_DLLPUBLIC void osl_suspendThread(oslThread Thread)
Suspend the execution of the thread.
SAL_DLLPUBLIC void osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority)
Changes the threads priority.
bool create()
Definition: thread.hxx:54
SAL_DLLPUBLIC void osl_waitThread(const TimeValue *pDelay)
Suspends the execution of the calling thread for at least the given time.
void * oslThread
Opaque data type for threads.
Definition: thread.h:21
SAL_DLLPUBLIC void osl_destroyThread(oslThread Thread)
Release the thread handle.
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:358