Branch data Line data Source code
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 : : {
45 : : /** threadFunc is the function which is executed by the threads
46 : : created by the osl::Thread class. The function's signature
47 : : matches the one of oslWorkerFunction which is declared in
48 : : osl/thread.h .
49 : : */
50 : : extern "C" inline void SAL_CALL threadFunc( void* param);
51 : :
52 : : /**
53 : : A thread abstraction.
54 : :
55 : : @deprecated use ::salhelper::Thread instead. Only the static member
56 : : functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
57 : : ::osl::Thread::yield are not deprecated.
58 : : */
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 : 429 : inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (())
66 : 429 : { return ::rtl_allocateMemory( nSize ); }
67 : 426 : inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (())
68 : 426 : { ::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 : 5934 : Thread(): m_hThread(0){}
75 : :
76 : 5926 : virtual ~Thread()
77 : 5926 : {
78 : 5926 : osl_destroyThread( m_hThread);
79 [ - + ]: 5926 : }
80 : :
81 : 5700 : sal_Bool SAL_CALL create()
82 : : {
83 : : assert(m_hThread == 0); // only one running thread per instance
84 : 5700 : m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
85 [ - + ]: 5700 : if (m_hThread == 0)
86 : : {
87 : 0 : return false;
88 : : }
89 : 5700 : osl_resumeThread(m_hThread);
90 : 5700 : return true;
91 : : }
92 : :
93 : 0 : sal_Bool SAL_CALL createSuspended()
94 : : {
95 : : assert(m_hThread == 0); // only one running thread per instance
96 [ # # ]: 0 : if( m_hThread)
97 : 0 : return sal_False;
98 : : m_hThread= osl_createSuspendedThread( threadFunc,
99 : 0 : (void*)this);
100 : 0 : return m_hThread != 0;
101 : : }
102 : :
103 : 0 : virtual void SAL_CALL suspend()
104 : : {
105 [ # # ]: 0 : if( m_hThread )
106 : 0 : osl_suspendThread(m_hThread);
107 : 0 : }
108 : :
109 : 0 : virtual void SAL_CALL resume()
110 : : {
111 [ # # ]: 0 : if( m_hThread )
112 : 0 : osl_resumeThread(m_hThread);
113 : 0 : }
114 : :
115 : 829 : virtual void SAL_CALL terminate()
116 : : {
117 [ + - ]: 829 : if( m_hThread )
118 : 829 : osl_terminateThread(m_hThread);
119 : 829 : }
120 : :
121 : 828 : virtual void SAL_CALL join()
122 : : {
123 : 828 : osl_joinWithThread(m_hThread);
124 : 828 : }
125 : :
126 : 90 : sal_Bool SAL_CALL isRunning() const
127 : : {
128 : 90 : return osl_isThreadRunning(m_hThread);
129 : : }
130 : :
131 : 8 : void SAL_CALL setPriority( oslThreadPriority Priority)
132 : : {
133 [ - + ]: 8 : if( m_hThread )
134 : 0 : osl_setThreadPriority(m_hThread, Priority);
135 : 8 : }
136 : :
137 : 0 : oslThreadPriority SAL_CALL getPriority() const
138 : : {
139 [ # # ]: 0 : return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
140 : : }
141 : :
142 : 220 : oslThreadIdentifier SAL_CALL getIdentifier() const
143 : : {
144 : 220 : return osl_getThreadIdentifier(m_hThread);
145 : : }
146 : :
147 : 28030486 : static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
148 : : {
149 : 28030486 : return osl_getThreadIdentifier(0);
150 : : }
151 : :
152 : 7 : static void SAL_CALL wait(const TimeValue& Delay)
153 : : {
154 : 7 : osl_waitThread(&Delay);
155 : 7 : }
156 : :
157 : 0 : static void SAL_CALL yield()
158 : : {
159 : 0 : osl_yieldThread();
160 : 0 : }
161 : :
162 : 3553 : static inline void setName(char const * name) throw () {
163 : 3553 : osl_setThreadName(name);
164 : 3553 : }
165 : :
166 : 11367 : virtual sal_Bool SAL_CALL schedule()
167 : : {
168 [ + - ]: 11367 : 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 : :
178 : : /** The thread functions calls the protected functions
179 : : run and onTerminated.
180 : : */
181 : : friend void SAL_CALL threadFunc( void* param);
182 : :
183 : : virtual void SAL_CALL run() = 0;
184 : :
185 : 94 : virtual void SAL_CALL onTerminated()
186 : : {
187 : 94 : }
188 : :
189 : : private:
190 : : oslThread m_hThread;
191 : : };
192 : :
193 : 5697 : extern "C" inline void SAL_CALL threadFunc( void* param)
194 : : {
195 : 5697 : Thread* pObj= (Thread*)param;
196 : 5697 : pObj->run();
197 : 5692 : pObj->onTerminated();
198 : 5693 : }
199 : :
200 : : class ThreadData
201 : : {
202 : : ThreadData( const ThreadData& );
203 : : ThreadData& operator= (const ThreadData& );
204 : : public:
205 : : /// Create a thread specific local data key
206 : 14 : ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
207 : : {
208 : 14 : m_hKey = osl_createThreadKey( pCallback );
209 : 14 : }
210 : :
211 : : /// Destroy a thread specific local data key
212 : 4 : ~ThreadData()
213 : : {
214 : 4 : osl_destroyThreadKey(m_hKey);
215 : 4 : }
216 : :
217 : : /** Set the data associated with the data key.
218 : : @returns True if operation was successfull
219 : : */
220 : 75 : sal_Bool SAL_CALL setData(void *pData)
221 : : {
222 : 75 : return (osl_setThreadKeyData(m_hKey, pData));
223 : : }
224 : :
225 : : /** Get the data associated with the data key.
226 : : @returns The data asscoitaed with the data key or
227 : : NULL if no data was set
228 : : */
229 : 70 : void* SAL_CALL getData()
230 : : {
231 : 70 : 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: */
|