Branch data 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 : : namespace {
35 : :
36 : : namespace css = com::sun::star;
37 : :
38 : : }
39 : :
40 : : using namespace osl;
41 : :
42 : : namespace cppu_threadpool {
43 : :
44 : : // ----------------------------------------------------------------------------------
45 [ + - ]: 100 : ThreadAdmin::ThreadAdmin(): m_disposed(false) {}
46 : :
47 : 98 : ThreadAdmin::~ThreadAdmin()
48 : : {
49 : : #if OSL_DEBUG_LEVEL > 1
50 : : if( m_lst.size() )
51 : : {
52 : : fprintf( stderr, "%lu Threads left\n" , static_cast<unsigned long>(m_lst.size()) );
53 : : }
54 : : #endif
55 : 98 : }
56 : :
57 : 1638 : void ThreadAdmin::add( rtl::Reference< ORequestThread > const & p )
58 : : {
59 [ + - ]: 1638 : MutexGuard aGuard( m_mutex );
60 [ - + ]: 1638 : if( m_disposed )
61 : : {
62 : : throw css::lang::DisposedException(
63 : : rtl::OUString(
64 : : RTL_CONSTASCII_USTRINGPARAM(
65 : : "cppu_threadpool::ORequestThread created after"
66 : : " cppu_threadpool::ThreadAdmin has been disposed")),
67 [ # # ][ # # ]: 0 : css::uno::Reference< css::uno::XInterface >());
68 : : }
69 [ + - ][ + - ]: 1638 : m_lst.push_back( p );
70 : 1638 : }
71 : :
72 : 1638 : void ThreadAdmin::remove_locked( rtl::Reference< ORequestThread > const & p )
73 : : {
74 [ + - ]: 1638 : ::std::list< rtl::Reference< ORequestThread > >::iterator ii = ::std::find( m_lst.begin(), m_lst.end(), p );
75 [ + + ]: 1638 : if( ii != m_lst.end() )
76 : : {
77 [ + - ]: 1587 : m_lst.erase( ii );
78 : : }
79 : 1638 : }
80 : :
81 : 1637 : void ThreadAdmin::remove( rtl::Reference< ORequestThread > const & p )
82 : : {
83 [ + - ]: 1637 : MutexGuard aGuard( m_mutex );
84 [ + - ][ + - ]: 1638 : remove_locked( p );
85 : 1638 : }
86 : :
87 : 98 : void ThreadAdmin::join()
88 : : {
89 : : {
90 [ + - ]: 98 : MutexGuard aGuard( m_mutex );
91 [ + - ]: 98 : m_disposed = true;
92 : : }
93 : 51 : for (;;)
94 : : {
95 : 149 : rtl::Reference< ORequestThread > pCurrent;
96 : : {
97 [ + - ]: 149 : MutexGuard aGuard( m_mutex );
98 [ + + ]: 149 : if( m_lst.empty() )
99 : : {
100 : : break;
101 : : }
102 [ + - ][ + - ]: 51 : pCurrent = m_lst.front();
103 [ + - ][ + - ]: 149 : m_lst.pop_front();
[ + + ]
104 : : }
105 [ + - ][ + + ]: 298 : pCurrent->join();
106 [ + - ]: 149 : }
107 : 98 : }
108 : :
109 : : // ----------------------------------------------------------------------------------
110 : 1638 : ORequestThread::ORequestThread( ThreadPoolHolder const &aThreadPool,
111 : : JobQueue *pQueue,
112 : : const ByteSequence &aThreadId,
113 : : sal_Bool bAsynchron )
114 : : : m_aThreadPool( aThreadPool )
115 : : , m_pQueue( pQueue )
116 : : , m_aThreadId( aThreadId )
117 [ + - ]: 1638 : , m_bAsynchron( bAsynchron )
118 : 1638 : {}
119 : :
120 [ + - ][ + - ]: 3276 : ORequestThread::~ORequestThread() {}
[ - + ]
121 : :
122 : 306277 : void ORequestThread::setTask( JobQueue *pQueue,
123 : : const ByteSequence &aThreadId,
124 : : sal_Bool bAsynchron )
125 : : {
126 : 306277 : m_pQueue = pQueue;
127 : 306277 : m_aThreadId = aThreadId;
128 : 306277 : m_bAsynchron = bAsynchron;
129 : 306277 : }
130 : :
131 : 1638 : void ORequestThread::launch()
132 : : {
133 : : // Assumption is that osl::Thread::create returns normally with a true
134 : : // return value iff it causes osl::Thread::run to start executing:
135 [ + - ]: 1638 : acquire();
136 : 1638 : ThreadAdmin & rThreadAdmin = m_aThreadPool->getThreadAdmin();
137 [ + - ]: 1638 : osl::ClearableMutexGuard g(rThreadAdmin.m_mutex);
138 [ + - ][ + - ]: 1638 : rThreadAdmin.add( this );
[ + - ]
139 : : try {
140 [ + - ][ - + ]: 1638 : if (!create()) {
141 [ # # ][ # # ]: 0 : throw std::runtime_error("osl::Thread::create failed");
142 : : }
143 : 0 : } catch (...) {
144 [ # # # # : 0 : rThreadAdmin.remove_locked( this );
# # ]
145 [ # # ]: 0 : g.clear();
146 [ # # ]: 0 : release();
147 : 0 : throw;
148 [ + - ]: 1638 : }
149 : 1638 : }
150 : :
151 : 1638 : void ORequestThread::onTerminated()
152 : : {
153 [ + - ]: 1638 : m_aThreadPool->getThreadAdmin().remove( this );
154 : 1638 : release();
155 : 1638 : }
156 : :
157 : 1638 : void ORequestThread::run()
158 : : {
159 : : try
160 : : {
161 [ + + ]: 309553 : while ( m_pQueue )
162 : : {
163 [ + + ]: 307915 : if( ! m_bAsynchron )
164 : : {
165 : 256755 : if ( !uno_bindIdToCurrentThread( m_aThreadId.getHandle() ) )
166 : : {
167 : : OSL_ASSERT( false );
168 : : }
169 : : }
170 : :
171 [ + - ][ + + ]: 616029 : while( ! m_pQueue->isEmpty() )
172 : : {
173 : : // Note : Oneways should not get a disposable disposeid,
174 : : // It does not make sense to dispose a call in this state.
175 : : // That's way we put it an disposeid, that can't be used otherwise.
176 : : m_pQueue->enter(
177 : : sal::static_int_cast< sal_Int64 >(
178 : : reinterpret_cast< sal_IntPtr >(this)),
179 [ + - ]: 308114 : sal_True );
180 : :
181 [ + - ][ + + ]: 308113 : if( m_pQueue->isEmpty() )
182 : : {
183 [ + - ]: 308020 : m_aThreadPool->revokeQueue( m_aThreadId , m_bAsynchron );
184 : : // Note : revokeQueue might have failed because m_pQueue.isEmpty()
185 : : // may be false (race).
186 : : }
187 : : }
188 : :
189 [ + ][ + - ]: 307914 : delete m_pQueue;
190 : 307914 : m_pQueue = 0;
191 : :
192 [ + + ]: 307914 : if( ! m_bAsynchron )
193 : : {
194 : 256755 : uno_releaseIdFromCurrentThread();
195 : : }
196 : :
197 [ + - ][ + - ]: 307914 : m_aThreadPool->waitInPool( this );
[ + - ]
198 : : }
199 : : }
200 : 0 : catch (...)
201 : : {
202 : : // Work around the problem that onTerminated is not called if run
203 : : // throws an exception:
204 [ # # ]: 0 : onTerminated();
205 : 0 : throw;
206 : : }
207 : 1638 : }
208 : : }
209 : :
210 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|