LCOV - code coverage report
Current view: top level - cppu/source/threadpool - thread.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 65 75 86.7 %
Date: 2014-11-03 Functions: 13 13 100.0 %
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             : #include <stdio.h>
      21             : #include <osl/diagnose.h>
      22             : #include <uno/threadpool.h>
      23             : 
      24             : #include <com/sun/star/lang/DisposedException.hpp>
      25             : #include <com/sun/star/uno/Reference.hxx>
      26             : #include <com/sun/star/uno/XInterface.hpp>
      27             : #include <rtl/ustring.h>
      28             : #include <rtl/ustring.hxx>
      29             : 
      30             : #include "thread.hxx"
      31             : #include "jobqueue.hxx"
      32             : #include "threadpool.hxx"
      33             : 
      34             : using namespace osl;
      35             : using namespace rtl;
      36             : 
      37             : namespace cppu_threadpool {
      38             : 
      39             : 
      40         100 :     ThreadAdmin::ThreadAdmin(): m_disposed(false) {}
      41             : 
      42         100 :     ThreadAdmin::~ThreadAdmin()
      43             :     {
      44             : #if OSL_DEBUG_LEVEL > 1
      45             :         if( m_lst.size() )
      46             :         {
      47             :             fprintf( stderr, "%lu Threads left\n" , static_cast<unsigned long>(m_lst.size()) );
      48             :         }
      49             : #endif
      50         100 :     }
      51             : 
      52        1667 :     void ThreadAdmin::add( rtl::Reference< ORequestThread > const & p )
      53             :     {
      54        1667 :         MutexGuard aGuard( m_mutex );
      55        1667 :         if( m_disposed )
      56             :         {
      57             :             throw css::lang::DisposedException(
      58             :                         "cppu_threadpool::ORequestThread created after"
      59           0 :                         " cppu_threadpool::ThreadAdmin has been disposed");
      60             :         }
      61        1667 :         m_lst.push_back( p );
      62        1667 :     }
      63             : 
      64        1667 :     void ThreadAdmin::remove_locked( rtl::Reference< ORequestThread > const & p )
      65             :     {
      66        1667 :         ::std::list< rtl::Reference< ORequestThread > >::iterator ii = ::std::find( m_lst.begin(), m_lst.end(), p );
      67        1667 :         if( ii != m_lst.end() )
      68             :         {
      69        1567 :             m_lst.erase( ii );
      70             :         }
      71        1667 :     }
      72             : 
      73        1667 :     void ThreadAdmin::remove( rtl::Reference< ORequestThread > const & p )
      74             :     {
      75        1667 :         MutexGuard aGuard( m_mutex );
      76        1667 :         remove_locked( p );
      77        1667 :     }
      78             : 
      79         100 :     void ThreadAdmin::join()
      80             :     {
      81             :         {
      82         100 :             MutexGuard aGuard( m_mutex );
      83         100 :             m_disposed = true;
      84             :         }
      85             :         for (;;)
      86             :         {
      87         200 :             rtl::Reference< ORequestThread > pCurrent;
      88             :             {
      89         200 :                 MutexGuard aGuard( m_mutex );
      90         200 :                 if( m_lst.empty() )
      91             :                 {
      92         100 :                     break;
      93             :                 }
      94         100 :                 pCurrent = m_lst.front();
      95         100 :                 m_lst.pop_front();
      96             :             }
      97         100 :             pCurrent->join();
      98         100 :         }
      99         100 :     }
     100             : 
     101             : 
     102        1667 :     ORequestThread::ORequestThread( ThreadPoolHolder const &aThreadPool,
     103             :                                     JobQueue *pQueue,
     104             :                                     const ByteSequence &aThreadId,
     105             :                                     bool bAsynchron )
     106             :         : m_aThreadPool( aThreadPool )
     107             :         , m_pQueue( pQueue )
     108             :         , m_aThreadId( aThreadId )
     109        1667 :         , m_bAsynchron( bAsynchron )
     110        1667 :     {}
     111             : 
     112        3334 :     ORequestThread::~ORequestThread() {}
     113             : 
     114      366460 :     void ORequestThread::setTask( JobQueue *pQueue,
     115             :                                   const ByteSequence &aThreadId,
     116             :                                   bool bAsynchron )
     117             :     {
     118      366460 :         m_pQueue = pQueue;
     119      366460 :         m_aThreadId = aThreadId;
     120      366460 :         m_bAsynchron = bAsynchron;
     121      366460 :     }
     122             : 
     123        1667 :     void ORequestThread::launch()
     124             :     {
     125             :         // Assumption is that osl::Thread::create returns normally with a true
     126             :         // return value iff it causes osl::Thread::run to start executing:
     127        1667 :         acquire();
     128        1667 :         ThreadAdmin & rThreadAdmin = m_aThreadPool->getThreadAdmin();
     129        1667 :         osl::ClearableMutexGuard g(rThreadAdmin.m_mutex);
     130        1667 :         rThreadAdmin.add( this );
     131             :         try {
     132        1667 :             if (!create()) {
     133           0 :                 throw std::runtime_error("osl::Thread::create failed");
     134             :             }
     135           0 :         } catch (...) {
     136           0 :             rThreadAdmin.remove_locked( this );
     137           0 :             g.clear();
     138           0 :             release();
     139           0 :             throw;
     140        1667 :         }
     141        1667 :     }
     142             : 
     143        1667 :     void ORequestThread::onTerminated()
     144             :     {
     145        1667 :         m_aThreadPool->getThreadAdmin().remove( this );
     146        1667 :         release();
     147        1667 :     }
     148             : 
     149        1667 :     void ORequestThread::run()
     150             :     {
     151        1667 :         osl_setThreadName("cppu_threadpool::ORequestThread");
     152             : 
     153             :         try
     154             :         {
     155      371452 :             while ( m_pQueue )
     156             :             {
     157      368119 :                 if( ! m_bAsynchron )
     158             :                 {
     159      311780 :                     if ( !uno_bindIdToCurrentThread( m_aThreadId.getHandle() ) )
     160             :                     {
     161             :                         OSL_ASSERT( false );
     162             :                     }
     163             :                 }
     164             : 
     165     1104475 :                 while( ! m_pQueue->isEmpty() )
     166             :                 {
     167             :                     // Note : Oneways should not get a disposable disposeid,
     168             :                     //        It does not make sense to dispose a call in this state.
     169             :                     //        That's way we put it an disposeid, that can't be used otherwise.
     170             :                     m_pQueue->enter(
     171             :                         sal::static_int_cast< sal_Int64 >(
     172             :                             reinterpret_cast< sal_IntPtr >(this)),
     173      368226 :                         true );
     174             : 
     175      368227 :                     if( m_pQueue->isEmpty() )
     176             :                     {
     177      368216 :                         m_aThreadPool->revokeQueue( m_aThreadId , m_bAsynchron );
     178             :                         // Note : revokeQueue might have failed because m_pQueue.isEmpty()
     179             :                         //        may be false (race).
     180             :                     }
     181             :                 }
     182             : 
     183      368127 :                 delete m_pQueue;
     184      368127 :                 m_pQueue = 0;
     185             : 
     186      368127 :                 if( ! m_bAsynchron )
     187             :                 {
     188      311785 :                     uno_releaseIdFromCurrentThread();
     189             :                 }
     190             : 
     191      368127 :                 m_aThreadPool->waitInPool( this );
     192             :             }
     193             :         }
     194           0 :         catch (...)
     195             :         {
     196             :             // Work around the problem that onTerminated is not called if run
     197             :             // throws an exception:
     198           0 :             onTerminated();
     199           0 :             throw;
     200             :         }
     201        1667 :     }
     202             : }
     203             : 
     204             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10