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