LCOV - code coverage report
Current view: top level - libreoffice/solver/unxlngi6.pro/inc/osl - thread.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 46 75 61.3 %
Date: 2012-12-17 Functions: 17 26 65.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10