Files
libreoffice/cppu/source/threadpool/jobqueue.cxx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

178 lines
4.9 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-13 14:17:57 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
2000-09-18 14:29:57 +00:00
#include <cassert>
2000-09-18 14:29:57 +00:00
#include "jobqueue.hxx"
#include "threadpool.hxx"
2000-09-18 14:29:57 +00:00
namespace cppu_threadpool {
JobQueue::JobQueue() :
m_nToDo( 0 ),
m_bSuspended( false ),
m_DisposedCallerAdmin( DisposedCallerAdmin::getInstance() )
2000-09-18 14:29:57 +00:00
{
}
void JobQueue::add( void *pThreadSpecificData, RequestFun * doRequest )
2000-09-18 14:29:57 +00:00
{
std::scoped_lock guard( m_mutex );
2000-09-18 14:29:57 +00:00
Job job = { pThreadSpecificData , doRequest };
m_lstJob.push_back( job );
if( ! m_bSuspended )
{
m_cndWait.notify_all();
2000-09-18 14:29:57 +00:00
}
m_nToDo ++;
}
void *JobQueue::enter( void const * nDisposeId , bool bReturnWhenNoJob )
2000-09-18 14:29:57 +00:00
{
void *pReturn = nullptr;
2000-09-18 14:29:57 +00:00
{
// synchronize with the dispose calls
std::scoped_lock guard( m_mutex );
2010-10-11 10:41:50 +01:00
if( m_DisposedCallerAdmin->isDisposed( nDisposeId ) )
2000-09-18 14:29:57 +00:00
{
return nullptr;
2000-09-18 14:29:57 +00:00
}
m_lstCallstack.push_front( nDisposeId );
}
while( true )
2000-09-18 14:29:57 +00:00
{
struct Job job={nullptr,nullptr};
2000-09-18 14:29:57 +00:00
{
std::unique_lock guard( m_mutex );
while (m_bSuspended
|| (m_lstCallstack.front() != nullptr && !bReturnWhenNoJob
&& m_lstJob.empty()))
2000-09-18 14:29:57 +00:00
{
m_cndWait.wait(guard);
2000-09-18 14:29:57 +00:00
}
if( nullptr == m_lstCallstack.front() )
2000-09-18 14:29:57 +00:00
{
// disposed !
Remove a potentially already enqueued response when a bridge is disposed ...while a remote call is in progress. Otherwise, if the same thread later makes another remote call (through another bridge), it would erroneously pair the repsonse for the disposed call with the second call. UITest_calc_demo started to fail that way occasionally, presumably after 77445e201c45e5593761e8399c32f80eea2178a4 "Make Chart Creation Wizard async", but for reasons rather unrelated to the contents of that commit. What apparently happened is that in one test's tearDown, the bridge between the Python and the soffice process happened to be disposed during the XDesktop::terminate call from Python's main thread, so that the response (of type BOOLEAN) remained in the JobQueue. Then during the next test's setUp, XUnoUrlResolver::resolve from Python's main thread would internally make a remote queryInterface call for the initial StarOffice.ComponentContext object, which returns an ANY of either VOID or XInterface type. But that call was then mis-matched with the leftover BOOLEAN response, causing failure. I was able to reproduce that reliably on Linux with a local hack of > diff --git a/cppu/source/threadpool/jobqueue.cxx b/cppu/source/threadpool/jobqueue.cxx > index 6c9324521f40..a87770bf8935 100644 > --- a/cppu/source/threadpool/jobqueue.cxx > +++ b/cppu/source/threadpool/jobqueue.cxx > @@ -71,6 +71,7 @@ namespace cppu_threadpool { > } > > m_cndWait.wait(); > + timespec ms{0, 1000000}; nanosleep(&ms, nullptr); > > struct Job job={nullptr,nullptr}; > { introducing a sleep, so that other threads have a higher chance to dispose the bridge (when the call being processed here is that XDesktop::dispose) after the successful wait() but before the response is processed. UITest_calc_demo then failed with [...] > Execution time for create_chart.CalcChartUIDemo.test_activate_chart: 6.520 > tearDown: calling terminate()... > caught while TearDown: > Traceback (most recent call last): > File "uitest/libreoffice/connection.py", line 127, in tearDown > xDesktop.terminate() > libreoffice.connection.com.sun.star.lang.DisposedException: Binary URP bridge disposed during call binaryurp/source/bridge.cxx:611 > > ok > test_cancel_immediately (create_chart.CalcChartUIDemo) ... [...] > warn:sal.osl.pipe:423851:425076:sal/osl/unx/pipe.cxx:442: recv() failed: ECONNRESET > warn:binaryurp:423851:425076:binaryurp/source/bridge.cxx:843: undisposed bridge "" in state 2, potential deadlock ahead [...] > ====================================================================== > ERROR: test_cancel_immediately (create_chart.CalcChartUIDemo) > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "uitest/uitest/framework.py", line 25, in setUp > self.connection.setUp() > File "uitest/libreoffice/connection.py", line 176, in setUp > conn.setUp() > File "uitest/libreoffice/connection.py", line 57, in setUp > self.xContext = self.connect(socket) > File "uitest/libreoffice/connection.py", line 107, in connect > xContext = xUnoResolver.resolve(url) > uno.com.sun.star.uno.RuntimeException: initial object queryInterface for OID "StarOffice.ComponentContext" returned ANY of type boolean binaryurp/source/bridge.cxx:883 [...] Change-Id: Icf9aadbb38e7aafffff844fe8e9ae99e165c1f33 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96326 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2020-06-15 13:22:58 +02:00
if (!m_lstJob.empty() && m_lstJob.front().doRequest == nullptr) {
// If this thread was waiting for a remote response, that response may or
// may not have been enqueued; if it has not been enqueued, there cannot be
// another enqueued response, so it is always correct to remove any enqueued
// response here:
m_lstJob.pop_front();
}
2000-09-18 14:29:57 +00:00
break;
}
if( m_lstJob.empty() )
2000-09-18 14:29:57 +00:00
{
assert(bReturnWhenNoJob);
break;
2000-09-18 14:29:57 +00:00
}
job = m_lstJob.front();
m_lstJob.pop_front();
2000-09-18 14:29:57 +00:00
}
if( job.doRequest )
{
job.doRequest( job.pThreadSpecificData );
std::scoped_lock guard( m_mutex );
2000-09-18 14:29:57 +00:00
m_nToDo --;
}
else
{
pReturn = job.pThreadSpecificData;
std::scoped_lock guard( m_mutex );
m_nToDo --;
2000-09-18 14:29:57 +00:00
break;
}
}
{
// synchronize with the dispose calls
std::scoped_lock guard( m_mutex );
2000-09-18 14:29:57 +00:00
m_lstCallstack.pop_front();
}
return pReturn;
}
void JobQueue::dispose( void const * nDisposeId )
2000-09-18 14:29:57 +00:00
{
std::scoped_lock guard( m_mutex );
for( auto& rId : m_lstCallstack )
2000-09-18 14:29:57 +00:00
{
if( rId == nDisposeId )
2000-09-18 14:29:57 +00:00
{
rId = nullptr;
2000-09-18 14:29:57 +00:00
}
}
if( !m_lstCallstack.empty() && ! m_lstCallstack.front() )
{
// The thread is waiting for a disposed pCallerId, let it go
m_cndWait.notify_all();
2000-09-18 14:29:57 +00:00
}
}
void JobQueue::suspend()
{
std::scoped_lock guard( m_mutex );
m_bSuspended = true;
2000-09-18 14:29:57 +00:00
}
void JobQueue::resume()
{
std::scoped_lock guard( m_mutex );
m_bSuspended = false;
2000-09-18 14:29:57 +00:00
if( ! m_lstJob.empty() )
{
m_cndWait.notify_all();
2000-09-18 14:29:57 +00:00
}
}
bool JobQueue::isEmpty() const
2000-09-18 14:29:57 +00:00
{
std::scoped_lock guard( m_mutex );
2000-09-18 14:29:57 +00:00
return m_lstJob.empty();
}
bool JobQueue::isCallstackEmpty() const
2000-09-18 14:29:57 +00:00
{
std::scoped_lock guard( m_mutex );
2000-09-18 14:29:57 +00:00
return m_lstCallstack.empty();
}
bool JobQueue::isBusy() const
2000-09-18 14:29:57 +00:00
{
std::scoped_lock guard( m_mutex );
2000-09-18 14:29:57 +00:00
return m_nToDo > 0;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */