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 :
36 : namespace cppu_threadpool {
37 :
38 : // ----------------------------------------------------------------------------------
39 0 : ThreadAdmin::ThreadAdmin(): m_disposed(false) {}
40 :
41 0 : ThreadAdmin::~ThreadAdmin()
42 : {
43 : #if OSL_DEBUG_LEVEL > 1
44 : if( m_lst.size() )
45 : {
46 : fprintf( stderr, "%lu Threads left\n" , static_cast<unsigned long>(m_lst.size()) );
47 : }
48 : #endif
49 0 : }
50 :
51 0 : void ThreadAdmin::add( rtl::Reference< ORequestThread > const & p )
52 : {
53 0 : MutexGuard aGuard( m_mutex );
54 0 : if( m_disposed )
55 : {
56 : throw css::lang::DisposedException(
57 : rtl::OUString(
58 : RTL_CONSTASCII_USTRINGPARAM(
59 : "cppu_threadpool::ORequestThread created after"
60 : " cppu_threadpool::ThreadAdmin has been disposed")),
61 0 : css::uno::Reference< css::uno::XInterface >());
62 : }
63 0 : m_lst.push_back( p );
64 0 : }
65 :
66 0 : void ThreadAdmin::remove_locked( rtl::Reference< ORequestThread > const & p )
67 : {
68 0 : ::std::list< rtl::Reference< ORequestThread > >::iterator ii = ::std::find( m_lst.begin(), m_lst.end(), p );
69 0 : if( ii != m_lst.end() )
70 : {
71 0 : m_lst.erase( ii );
72 : }
73 0 : }
74 :
75 0 : void ThreadAdmin::remove( rtl::Reference< ORequestThread > const & p )
76 : {
77 0 : MutexGuard aGuard( m_mutex );
78 0 : remove_locked( p );
79 0 : }
80 :
81 0 : void ThreadAdmin::join()
82 : {
83 : {
84 0 : MutexGuard aGuard( m_mutex );
85 0 : m_disposed = true;
86 : }
87 0 : for (;;)
88 : {
89 0 : rtl::Reference< ORequestThread > pCurrent;
90 : {
91 0 : MutexGuard aGuard( m_mutex );
92 0 : if( m_lst.empty() )
93 : {
94 : break;
95 : }
96 0 : pCurrent = m_lst.front();
97 0 : m_lst.pop_front();
98 : }
99 0 : pCurrent->join();
100 0 : }
101 0 : }
102 :
103 : // ----------------------------------------------------------------------------------
104 0 : ORequestThread::ORequestThread( ThreadPoolHolder const &aThreadPool,
105 : JobQueue *pQueue,
106 : const ByteSequence &aThreadId,
107 : sal_Bool bAsynchron )
108 : : m_aThreadPool( aThreadPool )
109 : , m_pQueue( pQueue )
110 : , m_aThreadId( aThreadId )
111 0 : , m_bAsynchron( bAsynchron )
112 0 : {}
113 :
114 0 : ORequestThread::~ORequestThread() {}
115 :
116 0 : void ORequestThread::setTask( JobQueue *pQueue,
117 : const ByteSequence &aThreadId,
118 : sal_Bool bAsynchron )
119 : {
120 0 : m_pQueue = pQueue;
121 0 : m_aThreadId = aThreadId;
122 0 : m_bAsynchron = bAsynchron;
123 0 : }
124 :
125 0 : void ORequestThread::launch()
126 : {
127 : // Assumption is that osl::Thread::create returns normally with a true
128 : // return value iff it causes osl::Thread::run to start executing:
129 0 : acquire();
130 0 : ThreadAdmin & rThreadAdmin = m_aThreadPool->getThreadAdmin();
131 0 : osl::ClearableMutexGuard g(rThreadAdmin.m_mutex);
132 0 : rThreadAdmin.add( this );
133 : try {
134 0 : if (!create()) {
135 0 : throw std::runtime_error("osl::Thread::create failed");
136 : }
137 0 : } catch (...) {
138 0 : rThreadAdmin.remove_locked( this );
139 0 : g.clear();
140 0 : release();
141 0 : throw;
142 0 : }
143 0 : }
144 :
145 0 : void ORequestThread::onTerminated()
146 : {
147 0 : m_aThreadPool->getThreadAdmin().remove( this );
148 0 : release();
149 0 : }
150 :
151 0 : void ORequestThread::run()
152 : {
153 : try
154 : {
155 0 : while ( m_pQueue )
156 : {
157 0 : if( ! m_bAsynchron )
158 : {
159 0 : if ( !uno_bindIdToCurrentThread( m_aThreadId.getHandle() ) )
160 : {
161 : OSL_ASSERT( false );
162 : }
163 : }
164 :
165 0 : 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 0 : sal_True );
174 :
175 0 : if( m_pQueue->isEmpty() )
176 : {
177 0 : 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 0 : delete m_pQueue;
184 0 : m_pQueue = 0;
185 :
186 0 : if( ! m_bAsynchron )
187 : {
188 0 : uno_releaseIdFromCurrentThread();
189 : }
190 :
191 0 : 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 0 : }
202 : }
203 :
204 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|