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: */