LCOV - code coverage report
Current view: top level - include/osl - thread.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 59 75 78.7 %
Date: 2015-06-13 12:38:46 Functions: 21 26 80.8 %
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 INCLUDED_OSL_THREAD_HXX
      21             : #define INCLUDED_OSL_THREAD_HXX
      22             : 
      23             : #include <sal/config.h>
      24             : 
      25             : #include <cassert>
      26             : 
      27             : #include <osl/time.h>
      28             : #include <osl/thread.h>
      29             : #include <rtl/alloc.h>
      30             : 
      31             : namespace osl
      32             : {
      33             : /** threadFunc is the function which is executed by the threads
      34             :     created by the osl::Thread class. The function's signature
      35             :     matches the one of oslWorkerFunction which is declared in
      36             :     osl/thread.h .
      37             : */
      38             : extern "C" inline void SAL_CALL threadFunc( void* param);
      39             : 
      40             : /**
      41             :    A thread abstraction.
      42             : 
      43             :    @deprecated use ::salhelper::Thread instead.  Only the static member
      44             :    functions ::osl::Thread::getCurrentIdentifier, ::osl::Thread::wait, and
      45             :    ::osl::Thread::yield are not deprecated.
      46             :  */
      47             : class Thread
      48             : {
      49             :     Thread( const Thread& ) SAL_DELETED_FUNCTION;
      50             :     Thread& operator= ( const Thread& ) SAL_DELETED_FUNCTION;
      51             : public:
      52             :     // these are here to force memory de/allocation to sal lib.
      53           7 :     inline static void * SAL_CALL operator new( size_t nSize )
      54           7 :         { return ::rtl_allocateMemory( nSize ); }
      55           3 :     inline static void SAL_CALL operator delete( void * pMem )
      56           3 :         { ::rtl_freeMemory( pMem ); }
      57             :     inline static void * SAL_CALL operator new( size_t, void * pMem )
      58             :         { return pMem; }
      59             :     inline static void SAL_CALL operator delete( void *, void * )
      60             :         {}
      61             : 
      62        8059 :     Thread(): m_hThread(0){}
      63             : 
      64        8047 :     virtual  ~Thread()
      65        8047 :     {
      66        8047 :         osl_destroyThread( m_hThread);
      67        8047 :     }
      68             : 
      69        5719 :     bool SAL_CALL create()
      70             :     {
      71             :         assert(m_hThread == 0); // only one running thread per instance
      72        5719 :         m_hThread = osl_createSuspendedThread( threadFunc, static_cast<void*>(this));
      73        5719 :         if (m_hThread == 0)
      74             :         {
      75           0 :             return false;
      76             :         }
      77        5719 :         osl_resumeThread(m_hThread);
      78        5719 :         return true;
      79             :     }
      80             : 
      81           0 :     bool SAL_CALL createSuspended()
      82             :     {
      83             :         assert(m_hThread == 0); // only one running thread per instance
      84           0 :         if( m_hThread)
      85           0 :             return false;
      86             :         m_hThread= osl_createSuspendedThread( threadFunc,
      87           0 :                                              static_cast<void*>(this));
      88           0 :         return m_hThread != 0;
      89             :     }
      90             : 
      91           0 :     virtual void SAL_CALL suspend()
      92             :     {
      93           0 :         if( m_hThread )
      94           0 :             osl_suspendThread(m_hThread);
      95           0 :     }
      96             : 
      97           5 :     virtual void SAL_CALL resume()
      98             :     {
      99           5 :         if( m_hThread )
     100           5 :             osl_resumeThread(m_hThread);
     101           5 :     }
     102             : 
     103           6 :     virtual void SAL_CALL terminate()
     104             :     {
     105           6 :         if( m_hThread )
     106           6 :             osl_terminateThread(m_hThread);
     107           6 :     }
     108             : 
     109        8520 :     virtual void SAL_CALL join()
     110             :     {
     111        8520 :         osl_joinWithThread(m_hThread);
     112        8520 :     }
     113             : 
     114          18 :     bool SAL_CALL isRunning() const
     115             :     {
     116          18 :         return osl_isThreadRunning(m_hThread);
     117             :     }
     118             : 
     119           4 :     void SAL_CALL setPriority( oslThreadPriority Priority)
     120             :     {
     121           4 :         if( m_hThread )
     122           0 :             osl_setThreadPriority(m_hThread, Priority);
     123           4 :     }
     124             : 
     125           0 :     oslThreadPriority SAL_CALL getPriority() const
     126             :     {
     127           0 :         return m_hThread ? osl_getThreadPriority(m_hThread) : osl_Thread_PriorityUnknown;
     128             :     }
     129             : 
     130         138 :     oslThreadIdentifier SAL_CALL getIdentifier() const
     131             :     {
     132         138 :         return osl_getThreadIdentifier(m_hThread);
     133             :     }
     134             : 
     135   125306474 :     static oslThreadIdentifier SAL_CALL getCurrentIdentifier()
     136             :     {
     137   125306474 :         return osl_getThreadIdentifier(0);
     138             :     }
     139             : 
     140           4 :     static void SAL_CALL wait(const TimeValue& Delay)
     141             :     {
     142           4 :         osl_waitThread(&Delay);
     143           3 :     }
     144             : 
     145           0 :     static void SAL_CALL yield()
     146             :     {
     147           0 :         osl_yieldThread();
     148           0 :     }
     149             : 
     150        6696 :     static inline void setName(char const * name) throw () {
     151        6696 :         osl_setThreadName(name);
     152        6696 :     }
     153             : 
     154          12 :     virtual bool SAL_CALL schedule()
     155             :     {
     156          12 :         return m_hThread && osl_scheduleThread(m_hThread);
     157             :     }
     158             : 
     159             :     SAL_CALL operator oslThread() const
     160             :     {
     161             :         return m_hThread;
     162             :     }
     163             : 
     164             : protected:
     165             : 
     166             :     /** The thread functions calls the protected functions
     167             :         run and onTerminated.
     168             :     */
     169             :     friend void SAL_CALL threadFunc( void* param);
     170             : 
     171             :     virtual void SAL_CALL run() = 0;
     172             : 
     173          20 :     virtual void SAL_CALL onTerminated()
     174             :     {
     175          20 :     }
     176             : 
     177             : private:
     178             :     oslThread m_hThread;
     179             : };
     180             : 
     181        5719 : extern "C" inline void SAL_CALL threadFunc( void* param)
     182             : {
     183        5719 :         Thread* pObj= static_cast<Thread*>(param);
     184        5719 :         pObj->run();
     185        5711 :         pObj->onTerminated();
     186        5712 : }
     187             : 
     188             : class ThreadData
     189             : {
     190             :     ThreadData( const ThreadData& ) SAL_DELETED_FUNCTION;
     191             :     ThreadData& operator= (const ThreadData& ) SAL_DELETED_FUNCTION;
     192             : public:
     193             :      /// Create a thread specific local data key
     194          11 :     ThreadData( oslThreadKeyCallbackFunction pCallback= 0 )
     195             :     {
     196          11 :         m_hKey = osl_createThreadKey( pCallback );
     197          11 :     }
     198             : 
     199             :     /// Destroy a thread specific local data key
     200           8 :     ~ThreadData()
     201             :     {
     202           8 :            osl_destroyThreadKey(m_hKey);
     203           8 :     }
     204             : 
     205             :     /** Set the data associated with the data key.
     206             :         @returns True if operation was successful
     207             :     */
     208          15 :     bool SAL_CALL setData(void *pData)
     209             :     {
     210          15 :            return (osl_setThreadKeyData(m_hKey, pData));
     211             :     }
     212             : 
     213             :     /** Get the data associated with the data key.
     214             :         @returns The data asscoitaed with the data key or
     215             :         NULL if no data was set
     216             :     */
     217          14 :     void* SAL_CALL getData()
     218             :     {
     219          14 :            return osl_getThreadKeyData(m_hKey);
     220             :     }
     221             : 
     222             :     operator oslThreadKey() const
     223             :     {
     224             :         return m_hKey;
     225             :     }
     226             : 
     227             : private:
     228             :     oslThreadKey m_hKey;
     229             : };
     230             : 
     231             : } // end namespace osl
     232             : 
     233             : #endif
     234             : 
     235             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11