2010-10-14 08:30:07 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-03-13 09:39:37 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-10 09:34:50 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2001-03-13 09:39:37 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2001-03-13 09:39:37 +00:00
|
|
|
*
|
2008-04-10 09:34:50 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2001-03-13 09:39:37 +00:00
|
|
|
*
|
2008-04-10 09:34:50 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2001-03-13 09:39:37 +00:00
|
|
|
*
|
2008-04-10 09:34:50 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2001-03-13 09:39:37 +00:00
|
|
|
*
|
2008-04-10 09:34:50 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2001-03-13 09:39:37 +00:00
|
|
|
*
|
2008-04-10 09:34:50 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2001-03-13 09:39:37 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#ifndef _THREAD_HXX_
|
|
|
|
#define _THREAD_HXX_
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2001-03-14 08:50:26 +00:00
|
|
|
#include <osl/time.h>
|
|
|
|
|
|
|
|
|
2001-03-13 09:39:37 +00:00
|
|
|
#include <osl/diagnose.h>
|
|
|
|
#include <osl/thread.h>
|
|
|
|
#include <rtl/alloc.h>
|
|
|
|
|
|
|
|
namespace osl
|
|
|
|
{
|
|
|
|
/** threadFunc is the function which is executed by the threads
|
|
|
|
created by the osl::Thread class. The function's signature
|
|
|
|
matches the one of oslWorkerFunction which is declared in
|
|
|
|
osl/thread.h .
|
|
|
|
*/
|
|
|
|
extern "C" inline void SAL_CALL threadFunc( void* param);
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
Thread( const Thread& );
|
|
|
|
Thread& operator= ( const Thread& );
|
|
|
|
public:
|
|
|
|
// these are here to force memory de/allocation to sal lib.
|
2001-03-13 10:58:57 +00:00
|
|
|
inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW (())
|
2001-03-13 09:39:37 +00:00
|
|
|
{ return ::rtl_allocateMemory( nSize ); }
|
2001-03-13 10:58:57 +00:00
|
|
|
inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW (())
|
2001-03-13 09:39:37 +00:00
|
|
|
{ ::rtl_freeMemory( pMem ); }
|
2001-03-13 10:58:57 +00:00
|
|
|
inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW (())
|
2001-03-13 09:39:37 +00:00
|
|
|
{ return pMem; }
|
2001-03-13 10:58:57 +00:00
|
|
|
inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW (())
|
2001-03-13 09:39:37 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
Thread(): m_hThread(0){}
|
|
|
|
|
|
|
|
virtual ~Thread()
|
|
|
|
{
|
2007-06-27 12:23:50 +00:00
|
|
|
osl_destroyThread( m_hThread);
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL create()
|
|
|
|
{
|
|
|
|
OSL_ASSERT(m_hThread == 0); // only one running thread per instance
|
|
|
|
if (m_hThread)
|
|
|
|
return sal_False;
|
|
|
|
|
2004-04-21 11:49:50 +00:00
|
|
|
m_hThread = osl_createSuspendedThread( threadFunc, (void*)this);
|
|
|
|
if ( m_hThread )
|
2001-03-13 09:39:37 +00:00
|
|
|
osl_resumeThread(m_hThread);
|
|
|
|
|
|
|
|
return m_hThread != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL createSuspended()
|
|
|
|
{
|
|
|
|
OSL_ASSERT(m_hThread == 0); // only one running thread per instance
|
|
|
|
if( m_hThread)
|
|
|
|
return sal_False;
|
|
|
|
m_hThread= osl_createSuspendedThread( threadFunc,
|
|
|
|
(void*)this);
|
|
|
|
return m_hThread != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SAL_CALL suspend()
|
|
|
|
{
|
2007-03-05 14:27:39 +00:00
|
|
|
if( m_hThread )
|
|
|
|
osl_suspendThread(m_hThread);
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SAL_CALL resume()
|
|
|
|
{
|
2007-03-05 14:27:39 +00:00
|
|
|
if( m_hThread )
|
|
|
|
osl_resumeThread(m_hThread);
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SAL_CALL terminate()
|
|
|
|
{
|
2007-03-05 14:27:39 +00:00
|
|
|
if( m_hThread )
|
|
|
|
osl_terminateThread(m_hThread);
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SAL_CALL join()
|
|
|
|
{
|
2007-06-27 12:23:50 +00:00
|
|
|
osl_joinWithThread(m_hThread);
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
2009-10-13 13:03:26 +02:00
|
|
|
sal_Bool SAL_CALL isRunning() const
|
2001-03-13 09:39:37 +00:00
|
|
|
{
|
2007-06-27 12:23:50 +00:00
|
|
|
return osl_isThreadRunning(m_hThread);
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SAL_CALL setPriority( oslThreadPriority Priority)
|
|
|
|
{
|
2007-03-05 14:27:39 +00:00
|
|
|
if( m_hThread )
|
|
|
|
osl_setThreadPriority(m_hThread, Priority);
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
2009-10-13 13:03:26 +02:00
|
|
|
oslThreadPriority SAL_CALL getPriority() const
|
2001-03-13 09:39:37 +00:00
|
|
|
{
|
2007-03-05 14:27:39 +00:00
|
|
|
return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oslThreadIdentifier SAL_CALL getIdentifier() const
|
|
|
|
{
|
|
|
|
return osl_getThreadIdentifier(m_hThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
|
|
|
|
{
|
|
|
|
return osl_getThreadIdentifier(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SAL_CALL wait(const TimeValue& Delay)
|
|
|
|
{
|
|
|
|
osl_waitThread(&Delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SAL_CALL yield()
|
|
|
|
{
|
|
|
|
osl_yieldThread();
|
|
|
|
}
|
|
|
|
|
2011-01-28 11:58:57 +01:00
|
|
|
static inline void setName(char const * name) throw () {
|
|
|
|
osl_setThreadName(name);
|
|
|
|
}
|
2001-03-13 09:39:37 +00:00
|
|
|
|
|
|
|
virtual sal_Bool SAL_CALL schedule()
|
|
|
|
{
|
2007-03-05 14:27:39 +00:00
|
|
|
return m_hThread ? osl_scheduleThread(m_hThread) : sal_False;
|
2001-03-13 09:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SAL_CALL operator oslThread() const
|
|
|
|
{
|
|
|
|
return m_hThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/** The thread functions calls the protected functions
|
|
|
|
run and onTerminated.
|
|
|
|
*/
|
|
|
|
friend void SAL_CALL threadFunc( void* param);
|
|
|
|
|
|
|
|
virtual void SAL_CALL run() = 0;
|
|
|
|
|
|
|
|
virtual void SAL_CALL onTerminated()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
oslThread m_hThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" inline void SAL_CALL threadFunc( void* param)
|
|
|
|
{
|
|
|
|
Thread* pObj= (Thread*)param;
|
|
|
|
pObj->run();
|
|
|
|
pObj->onTerminated();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThreadData
|
|
|
|
{
|
|
|
|
ThreadData( const ThreadData& );
|
|
|
|
ThreadData& operator= (const ThreadData& );
|
|
|
|
public:
|
2003-03-26 15:47:28 +00:00
|
|
|
/// Create a thread specific local data key
|
2001-03-13 09:39:37 +00:00
|
|
|
ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
|
|
|
|
{
|
|
|
|
m_hKey = osl_createThreadKey( pCallback );
|
|
|
|
}
|
|
|
|
|
2003-03-26 15:47:28 +00:00
|
|
|
/// Destroy a thread specific local data key
|
2001-03-13 09:39:37 +00:00
|
|
|
~ThreadData()
|
|
|
|
{
|
|
|
|
osl_destroyThreadKey(m_hKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the data associated with the data key.
|
|
|
|
@returns True if operation was successfull
|
|
|
|
*/
|
|
|
|
sal_Bool SAL_CALL setData(void *pData)
|
|
|
|
{
|
|
|
|
return (osl_setThreadKeyData(m_hKey, pData));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the data associated with the data key.
|
|
|
|
@returns The data asscoitaed with the data key or
|
2003-03-26 15:47:28 +00:00
|
|
|
NULL if no data was set
|
2001-03-13 09:39:37 +00:00
|
|
|
*/
|
|
|
|
void* SAL_CALL getData()
|
|
|
|
{
|
|
|
|
return osl_getThreadKeyData(m_hKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator oslThreadKey() const
|
|
|
|
{
|
|
|
|
return m_hKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
oslThreadKey m_hKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace osl
|
|
|
|
#endif
|
2001-03-13 10:58:57 +00:00
|
|
|
#endif
|
2010-10-14 08:30:07 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|