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

Generated by: LCOV version 1.11