fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / cppu / source / threadpool / thread.cxx
blobd1d448f429069b98f099fdbcfc0937b1c3d0afa1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cstdlib>
23 #include <osl/diagnose.h>
24 #include <uno/threadpool.h>
26 #include "thread.hxx"
27 #include "jobqueue.hxx"
28 #include "threadpool.hxx"
30 using namespace osl;
31 using namespace rtl;
33 namespace cppu_threadpool {
36 ThreadAdmin::ThreadAdmin(): m_disposed(false) {}
38 ThreadAdmin::~ThreadAdmin()
40 #if OSL_DEBUG_LEVEL > 1
41 if( m_lst.size() )
43 fprintf( stderr, "%lu Threads left\n" , static_cast<unsigned long>(m_lst.size()) );
45 #endif
48 bool ThreadAdmin::add( rtl::Reference< ORequestThread > const & p )
50 MutexGuard aGuard( m_mutex );
51 if( m_disposed )
53 return false;
55 m_lst.push_back( p );
56 return true;
59 void ThreadAdmin::remove_locked( rtl::Reference< ORequestThread > const & p )
61 ::std::list< rtl::Reference< ORequestThread > >::iterator ii = ::std::find( m_lst.begin(), m_lst.end(), p );
62 if( ii != m_lst.end() )
64 m_lst.erase( ii );
68 void ThreadAdmin::remove( rtl::Reference< ORequestThread > const & p )
70 MutexGuard aGuard( m_mutex );
71 remove_locked( p );
74 void ThreadAdmin::join()
77 MutexGuard aGuard( m_mutex );
78 m_disposed = true;
80 for (;;)
82 rtl::Reference< ORequestThread > pCurrent;
84 MutexGuard aGuard( m_mutex );
85 if( m_lst.empty() )
87 break;
89 pCurrent = m_lst.front();
90 m_lst.pop_front();
92 pCurrent->join();
97 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 , m_bAsynchron( bAsynchron )
107 ORequestThread::~ORequestThread() {}
109 void ORequestThread::setTask( JobQueue *pQueue,
110 const ByteSequence &aThreadId,
111 bool bAsynchron )
113 m_pQueue = pQueue;
114 m_aThreadId = aThreadId;
115 m_bAsynchron = bAsynchron;
118 bool ORequestThread::launch()
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 acquire();
123 ThreadAdmin & rThreadAdmin = m_aThreadPool->getThreadAdmin();
124 osl::ClearableMutexGuard g(rThreadAdmin.m_mutex);
125 if (!rThreadAdmin.add( this )) {
126 return false;
128 try {
129 if (!create()) {
130 std::abort();
132 } catch (...) {
133 rThreadAdmin.remove_locked( this );
134 g.clear();
135 release();
136 throw;
138 return true;
141 void ORequestThread::onTerminated()
143 m_aThreadPool->getThreadAdmin().remove( this );
144 release();
147 void ORequestThread::run()
149 osl_setThreadName("cppu_threadpool::ORequestThread");
153 while ( m_pQueue )
155 if( ! m_bAsynchron )
157 if ( !uno_bindIdToCurrentThread( m_aThreadId.getHandle() ) )
159 OSL_ASSERT( false );
163 while( ! m_pQueue->isEmpty() )
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 true );
173 if( m_pQueue->isEmpty() )
175 m_aThreadPool->revokeQueue( m_aThreadId , m_bAsynchron );
176 // Note : revokeQueue might have failed because m_pQueue.isEmpty()
177 // may be false (race).
181 delete m_pQueue;
182 m_pQueue = 0;
184 if( ! m_bAsynchron )
186 uno_releaseIdFromCurrentThread();
189 m_aThreadPool->waitInPool( this );
192 catch (...)
194 // Work around the problem that onTerminated is not called if run
195 // throws an exception:
196 onTerminated();
197 throw;
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */