1*565d668cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*565d668cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*565d668cSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*565d668cSAndrew Rist * distributed with this work for additional information 6*565d668cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*565d668cSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*565d668cSAndrew Rist * "License"); you may not use this file except in compliance 9*565d668cSAndrew Rist * with the License. You may obtain a copy of the License at 10*565d668cSAndrew Rist * 11*565d668cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*565d668cSAndrew Rist * 13*565d668cSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*565d668cSAndrew Rist * software distributed under the License is distributed on an 15*565d668cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*565d668cSAndrew Rist * KIND, either express or implied. See the License for the 17*565d668cSAndrew Rist * specific language governing permissions and limitations 18*565d668cSAndrew Rist * under the License. 19*565d668cSAndrew Rist * 20*565d668cSAndrew Rist *************************************************************/ 21*565d668cSAndrew Rist 22*565d668cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef _THREAD_HXX_ 25cdf0e10cSrcweir #define _THREAD_HXX_ 26cdf0e10cSrcweir 27cdf0e10cSrcweir #ifdef __cplusplus 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <osl/time.h> 30cdf0e10cSrcweir 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <osl/diagnose.h> 33cdf0e10cSrcweir #include <osl/thread.h> 34cdf0e10cSrcweir #include <rtl/alloc.h> 35cdf0e10cSrcweir 36cdf0e10cSrcweir namespace osl 37cdf0e10cSrcweir { 38cdf0e10cSrcweir /** threadFunc is the function which is executed by the threads 39cdf0e10cSrcweir created by the osl::Thread class. The function's signature 40cdf0e10cSrcweir matches the one of oslWorkerFunction which is declared in 41cdf0e10cSrcweir osl/thread.h . 42cdf0e10cSrcweir */ 43cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param); 44cdf0e10cSrcweir 45cdf0e10cSrcweir class Thread 46cdf0e10cSrcweir { 47cdf0e10cSrcweir Thread( const Thread& ); 48cdf0e10cSrcweir Thread& operator= ( const Thread& ); 49cdf0e10cSrcweir public: 50cdf0e10cSrcweir // these are here to force memory de/allocation to sal lib. 51cdf0e10cSrcweir inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (()) 52cdf0e10cSrcweir { return ::rtl_allocateMemory( nSize ); } 53cdf0e10cSrcweir inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (()) 54cdf0e10cSrcweir { ::rtl_freeMemory( pMem ); } 55cdf0e10cSrcweir inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW (()) 56cdf0e10cSrcweir { return pMem; } 57cdf0e10cSrcweir inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (()) 58cdf0e10cSrcweir {} 59cdf0e10cSrcweir 60cdf0e10cSrcweir Thread(): m_hThread(0){} 61cdf0e10cSrcweir 62cdf0e10cSrcweir virtual ~Thread() 63cdf0e10cSrcweir { 64cdf0e10cSrcweir osl_destroyThread( m_hThread); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir sal_Bool SAL_CALL create() 68cdf0e10cSrcweir { 69cdf0e10cSrcweir OSL_ASSERT(m_hThread == 0); // only one running thread per instance 70cdf0e10cSrcweir if (m_hThread) 71cdf0e10cSrcweir return sal_False; 72cdf0e10cSrcweir 73cdf0e10cSrcweir m_hThread = osl_createSuspendedThread( threadFunc, (void*)this); 74cdf0e10cSrcweir if ( m_hThread ) 75cdf0e10cSrcweir osl_resumeThread(m_hThread); 76cdf0e10cSrcweir 77cdf0e10cSrcweir return m_hThread != 0; 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir sal_Bool SAL_CALL createSuspended() 81cdf0e10cSrcweir { 82cdf0e10cSrcweir OSL_ASSERT(m_hThread == 0); // only one running thread per instance 83cdf0e10cSrcweir if( m_hThread) 84cdf0e10cSrcweir return sal_False; 85cdf0e10cSrcweir m_hThread= osl_createSuspendedThread( threadFunc, 86cdf0e10cSrcweir (void*)this); 87cdf0e10cSrcweir return m_hThread != 0; 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir virtual void SAL_CALL suspend() 91cdf0e10cSrcweir { 92cdf0e10cSrcweir if( m_hThread ) 93cdf0e10cSrcweir osl_suspendThread(m_hThread); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir 96cdf0e10cSrcweir virtual void SAL_CALL resume() 97cdf0e10cSrcweir { 98cdf0e10cSrcweir if( m_hThread ) 99cdf0e10cSrcweir osl_resumeThread(m_hThread); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir virtual void SAL_CALL terminate() 103cdf0e10cSrcweir { 104cdf0e10cSrcweir if( m_hThread ) 105cdf0e10cSrcweir osl_terminateThread(m_hThread); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir virtual void SAL_CALL join() 109cdf0e10cSrcweir { 110cdf0e10cSrcweir osl_joinWithThread(m_hThread); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir sal_Bool SAL_CALL isRunning() const 114cdf0e10cSrcweir { 115cdf0e10cSrcweir return osl_isThreadRunning(m_hThread); 116cdf0e10cSrcweir } 117cdf0e10cSrcweir 118cdf0e10cSrcweir void SAL_CALL setPriority( oslThreadPriority Priority) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir if( m_hThread ) 121cdf0e10cSrcweir osl_setThreadPriority(m_hThread, Priority); 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir oslThreadPriority SAL_CALL getPriority() const 125cdf0e10cSrcweir { 126cdf0e10cSrcweir return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown; 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir oslThreadIdentifier SAL_CALL getIdentifier() const 130cdf0e10cSrcweir { 131cdf0e10cSrcweir return osl_getThreadIdentifier(m_hThread); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir static oslThreadIdentifier SAL_CALL getCurrentIdentifier() 135cdf0e10cSrcweir { 136cdf0e10cSrcweir return osl_getThreadIdentifier(0); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir 139cdf0e10cSrcweir static void SAL_CALL wait(const TimeValue& Delay) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir osl_waitThread(&Delay); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir static void SAL_CALL yield() 145cdf0e10cSrcweir { 146cdf0e10cSrcweir osl_yieldThread(); 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir static inline void setName(char const * name) throw () { 150cdf0e10cSrcweir osl_setThreadName(name); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153cdf0e10cSrcweir virtual sal_Bool SAL_CALL schedule() 154cdf0e10cSrcweir { 155cdf0e10cSrcweir return m_hThread ? osl_scheduleThread(m_hThread) : sal_False; 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir SAL_CALL operator oslThread() const 159cdf0e10cSrcweir { 160cdf0e10cSrcweir return m_hThread; 161cdf0e10cSrcweir } 162cdf0e10cSrcweir 163cdf0e10cSrcweir protected: 164cdf0e10cSrcweir 165cdf0e10cSrcweir /** The thread functions calls the protected functions 166cdf0e10cSrcweir run and onTerminated. 167cdf0e10cSrcweir */ 168cdf0e10cSrcweir friend void SAL_CALL threadFunc( void* param); 169cdf0e10cSrcweir 170cdf0e10cSrcweir virtual void SAL_CALL run() = 0; 171cdf0e10cSrcweir 172cdf0e10cSrcweir virtual void SAL_CALL onTerminated() 173cdf0e10cSrcweir { 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir private: 177cdf0e10cSrcweir oslThread m_hThread; 178cdf0e10cSrcweir }; 179cdf0e10cSrcweir 180cdf0e10cSrcweir extern "C" inline void SAL_CALL threadFunc( void* param) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir Thread* pObj= (Thread*)param; 183cdf0e10cSrcweir pObj->run(); 184cdf0e10cSrcweir pObj->onTerminated(); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir 187cdf0e10cSrcweir class ThreadData 188cdf0e10cSrcweir { 189cdf0e10cSrcweir ThreadData( const ThreadData& ); 190cdf0e10cSrcweir ThreadData& operator= (const ThreadData& ); 191cdf0e10cSrcweir public: 192cdf0e10cSrcweir /// Create a thread specific local data key 193cdf0e10cSrcweir ThreadData( oslThreadKeyCallbackFunction pCallback= 0 ) 194cdf0e10cSrcweir { 195cdf0e10cSrcweir m_hKey = osl_createThreadKey( pCallback ); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir /// Destroy a thread specific local data key 199cdf0e10cSrcweir ~ThreadData() 200cdf0e10cSrcweir { 201cdf0e10cSrcweir osl_destroyThreadKey(m_hKey); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir /** Set the data associated with the data key. 205cdf0e10cSrcweir @returns True if operation was successfull 206cdf0e10cSrcweir */ 207cdf0e10cSrcweir sal_Bool SAL_CALL setData(void *pData) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir return (osl_setThreadKeyData(m_hKey, pData)); 210cdf0e10cSrcweir } 211cdf0e10cSrcweir 212cdf0e10cSrcweir /** Get the data associated with the data key. 213cdf0e10cSrcweir @returns The data asscoitaed with the data key or 214cdf0e10cSrcweir NULL if no data was set 215cdf0e10cSrcweir */ 216cdf0e10cSrcweir void* SAL_CALL getData() 217cdf0e10cSrcweir { 218cdf0e10cSrcweir return osl_getThreadKeyData(m_hKey); 219cdf0e10cSrcweir } 220cdf0e10cSrcweir 221cdf0e10cSrcweir operator oslThreadKey() const 222cdf0e10cSrcweir { 223cdf0e10cSrcweir return m_hKey; 224cdf0e10cSrcweir } 225cdf0e10cSrcweir 226cdf0e10cSrcweir private: 227cdf0e10cSrcweir oslThreadKey m_hKey; 228cdf0e10cSrcweir }; 229cdf0e10cSrcweir 230cdf0e10cSrcweir } // end namespace osl 231cdf0e10cSrcweir #endif 232cdf0e10cSrcweir #endif 233