My Project
UDK 3.2.7 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  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _THREAD_HXX_
30 #define _THREAD_HXX_
31 
32 #include "sal/config.h"
33 
34 #include <cassert>
35 
36 #include <osl/time.h>
37 
38 
39 #include <osl/diagnose.h>
40 #include <osl/thread.h>
41 #include <rtl/alloc.h>
42 
43 namespace osl
44 {
50 extern "C" inline void SAL_CALL threadFunc( void* param);
51 
59 class Thread
60 {
61  Thread( const Thread& );
62  Thread& operator= ( const Thread& );
63 public:
64  // these are here to force memory de/allocation to sal lib.
65  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (())
66  { return ::rtl_allocateMemory( nSize ); }
67  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (())
68  { ::rtl_freeMemory( pMem ); }
69  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW (())
70  { return pMem; }
71  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (())
72  {}
73 
74  Thread(): m_hThread(0){}
75 
76  virtual ~Thread()
77  {
78  osl_destroyThread( m_hThread);
79  }
80 
81  sal_Bool SAL_CALL create()
82  {
83  assert(m_hThread == 0); // only one running thread per instance
84  m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
85  if (m_hThread == 0)
86  {
87  return false;
88  }
89  osl_resumeThread(m_hThread);
90  return true;
91  }
92 
94  {
95  assert(m_hThread == 0); // only one running thread per instance
96  if( m_hThread)
97  return sal_False;
99  (void*)this);
100  return m_hThread != 0;
101  }
102 
103  virtual void SAL_CALL suspend()
104  {
105  if( m_hThread )
106  osl_suspendThread(m_hThread);
107  }
108 
109  virtual void SAL_CALL resume()
110  {
111  if( m_hThread )
112  osl_resumeThread(m_hThread);
113  }
114 
115  virtual void SAL_CALL terminate()
116  {
117  if( m_hThread )
118  osl_terminateThread(m_hThread);
119  }
120 
121  virtual void SAL_CALL join()
122  {
123  osl_joinWithThread(m_hThread);
124  }
125 
126  sal_Bool SAL_CALL isRunning() const
127  {
128  return osl_isThreadRunning(m_hThread);
129  }
130 
131  void SAL_CALL setPriority( oslThreadPriority Priority)
132  {
133  if( m_hThread )
134  osl_setThreadPriority(m_hThread, Priority);
135  }
136 
138  {
139  return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
140  }
141 
143  {
144  return osl_getThreadIdentifier(m_hThread);
145  }
146 
148  {
149  return osl_getThreadIdentifier(0);
150  }
151 
152  static void SAL_CALL wait(const TimeValue& Delay)
153  {
154  osl_waitThread(&Delay);
155  }
156 
157  static void SAL_CALL yield()
158  {
159  osl_yieldThread();
160  }
161 
162  static inline void setName(char const * name) throw () {
163  osl_setThreadName(name);
164  }
165 
166  virtual sal_Bool SAL_CALL schedule()
167  {
168  return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
169  }
170 
171  SAL_CALL operator oslThread() const
172  {
173  return m_hThread;
174  }
175 
176 protected:
177 
181  friend void SAL_CALL threadFunc( void* param);
182 
183  virtual void SAL_CALL run() = 0;
184 
185  virtual void SAL_CALL onTerminated()
186  {
187  }
188 
189 private:
190  oslThread m_hThread;
191 };
192 
193 extern "C" inline void SAL_CALL threadFunc( void* param)
194 {
195  Thread* pObj= (Thread*)param;
196  pObj->run();
197  pObj->onTerminated();
198 }
199 
201 {
202  ThreadData( const ThreadData& );
203  ThreadData& operator= (const ThreadData& );
204 public:
207  {
208  m_hKey = osl_createThreadKey( pCallback );
209  }
210 
213  {
214  osl_destroyThreadKey(m_hKey);
215  }
216 
220  sal_Bool SAL_CALL setData(void *pData)
221  {
222  return (osl_setThreadKeyData(m_hKey, pData));
223  }
224 
229  void* SAL_CALL getData()
230  {
231  return osl_getThreadKeyData(m_hKey);
232  }
233 
234  operator oslThreadKey() const
235  {
236  return m_hKey;
237  }
238 
239 private:
240  oslThreadKey m_hKey;
241 };
242 
243 } // end namespace osl
244 
245 #endif
246 
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */