UDK 3.2.7 C/C++ API Reference
osl/thread.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 
00029 #ifndef _THREAD_HXX_
00030 #define _THREAD_HXX_
00031 
00032 #include "sal/config.h"
00033 
00034 #include <cassert>
00035 
00036 #include <osl/time.h>
00037 
00038 
00039 #include <osl/diagnose.h>
00040 #include <osl/thread.h>
00041 #include <rtl/alloc.h>
00042 
00043 namespace osl
00044 {
00050 extern "C" inline void SAL_CALL threadFunc( void* param);
00051 
00059 class Thread
00060 {
00061     Thread( const Thread& );
00062     Thread& operator= ( const Thread& );
00063 public:
00064     // these are here to force memory de/allocation to sal lib.
00065     inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (())
00066         { return ::rtl_allocateMemory( nSize ); }
00067     inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (())
00068         { ::rtl_freeMemory( pMem ); }
00069     inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW (())
00070         { return pMem; }
00071     inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (())
00072         {}
00073 
00074     Thread(): m_hThread(0){}
00075 
00076     virtual  ~Thread()
00077     {
00078         osl_destroyThread( m_hThread);
00079     }
00080 
00081     sal_Bool SAL_CALL create()
00082     {
00083         assert(m_hThread == 0); // only one running thread per instance
00084         m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
00085         if (m_hThread == 0)
00086         {
00087             return false;
00088         }
00089         osl_resumeThread(m_hThread);
00090         return true;
00091     }
00092 
00093     sal_Bool SAL_CALL createSuspended()
00094     {
00095         assert(m_hThread == 0); // only one running thread per instance
00096         if( m_hThread)
00097             return sal_False;
00098         m_hThread= osl_createSuspendedThread( threadFunc,
00099                                              (void*)this);
00100         return m_hThread != 0;
00101     }
00102 
00103     virtual void SAL_CALL suspend()
00104     {
00105         if( m_hThread )
00106             osl_suspendThread(m_hThread);
00107     }
00108 
00109     virtual void SAL_CALL resume()
00110     {
00111         if( m_hThread )
00112             osl_resumeThread(m_hThread);
00113     }
00114 
00115     virtual void SAL_CALL terminate()
00116     {
00117         if( m_hThread )
00118             osl_terminateThread(m_hThread);
00119     }
00120 
00121     virtual void SAL_CALL join()
00122     {
00123         osl_joinWithThread(m_hThread);
00124     }
00125 
00126     sal_Bool SAL_CALL isRunning() const
00127     {
00128         return osl_isThreadRunning(m_hThread);
00129     }
00130 
00131     void SAL_CALL setPriority( oslThreadPriority Priority)
00132     {
00133         if( m_hThread )
00134             osl_setThreadPriority(m_hThread, Priority);
00135     }
00136 
00137     oslThreadPriority SAL_CALL getPriority() const
00138     {
00139         return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
00140     }
00141 
00142     oslThreadIdentifier SAL_CALL getIdentifier() const
00143     {
00144         return osl_getThreadIdentifier(m_hThread);
00145     }
00146 
00147     static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
00148     {
00149         return osl_getThreadIdentifier(0);
00150     }
00151 
00152     static void SAL_CALL wait(const TimeValue& Delay)
00153     {
00154         osl_waitThread(&Delay);
00155     }
00156 
00157     static void SAL_CALL yield()
00158     {
00159         osl_yieldThread();
00160     }
00161 
00162     static inline void setName(char const * name) throw () {
00163         osl_setThreadName(name);
00164     }
00165 
00166     virtual sal_Bool SAL_CALL schedule()
00167     {
00168         return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
00169     }
00170 
00171     SAL_CALL operator oslThread() const
00172     {
00173         return m_hThread;
00174     }
00175 
00176 protected:
00177 
00181     friend void SAL_CALL threadFunc( void* param);
00182 
00183     virtual void SAL_CALL run() = 0;
00184 
00185     virtual void SAL_CALL onTerminated()
00186     {
00187     }
00188 
00189 private:
00190     oslThread m_hThread;
00191 };
00192 
00193 extern "C" inline void SAL_CALL threadFunc( void* param)
00194 {
00195         Thread* pObj= (Thread*)param;
00196         pObj->run();
00197         pObj->onTerminated();
00198 }
00199 
00200 class ThreadData
00201 {
00202     ThreadData( const ThreadData& );
00203     ThreadData& operator= (const ThreadData& );
00204 public:
00206     ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
00207     {
00208         m_hKey = osl_createThreadKey( pCallback );
00209     }
00210 
00212     ~ThreadData()
00213     {
00214            osl_destroyThreadKey(m_hKey);
00215     }
00216 
00220     sal_Bool SAL_CALL setData(void *pData)
00221     {
00222            return (osl_setThreadKeyData(m_hKey, pData));
00223     }
00224 
00229     void* SAL_CALL getData()
00230     {
00231            return osl_getThreadKeyData(m_hKey);
00232     }
00233 
00234     operator oslThreadKey() const
00235     {
00236         return m_hKey;
00237     }
00238 
00239 private:
00240     oslThreadKey m_hKey;
00241 };
00242 
00243 } // end namespace osl
00244 
00245 #endif
00246 
00247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines