Files
loongoffice/cppu/source/threadpool/thread.cxx
Stephan Bergmann de5a8470ca Prevent self-join in ThreadAdmin::join
Depending on timing, it can apparently happen at least during
JunitTest_unordf_complex that a cppu_threadpool::ORequestThread having executed
a binaryurp::IncomingRequest holds the last binaryurp::Bridge reference:

> #2  0x00007fac4ad6bf97 in __assert_fail_base (fmt=<optimized out>, assertion=assertion@entry=0x7fac4b4e8c22 "false", file=file@entry=0x7fac4b4eaec7 "sal/osl/unx/thread.cxx", line=line@entry=443, function=function@entry=0x7fac4b4eaefd "void osl_joinWithThread(oslThread)") at assert.c:92
> #3  0x00007fac4ad6c042 in __GI___assert_fail (assertion=0x7fac4b4e8c22 "false", file=0x7fac4b4eaec7 "sal/osl/unx/thread.cxx", line=443, function=0x7fac4b4eaefd "void osl_joinWithThread(oslThread)") at assert.c:101
> #4  0x00007fac4b4d5258 in osl_joinWithThread(oslThread) (Thread=0x7fabfc0011d0) at sal/osl/unx/thread.cxx:443
> #5  0x00007fac47fbdde5 in cppu_threadpool::ThreadAdmin::join() (this=0x7fac1c0069e0) at cppu/source/threadpool/thread.cxx:89
> #6  0x00007fac47fc23af in cppu_threadpool::ThreadPool::joinWorkers() (this=<optimized out>) at cppu/source/threadpool/threadpool.cxx:179
> #7  0x00007fac47fc35b7 in uno_threadpool_destroy(uno_ThreadPool) (hPool=<optimized out>) at cppu/source/threadpool/threadpool.cxx:499
> #8  0x00007fac25b03b5c in binaryurp::Bridge::terminate(bool) (this=0x7fac24117078, final=true) at binaryurp/source/bridge.cxx:277
> #9  0x00007fac25b0794e in binaryurp::Bridge::dispose() (this=0x7fac24117078) at binaryurp/source/bridge.cxx:904
> #10 0x00007fac25b07215 in binaryurp::Bridge::~Bridge() (this=0x7fac24117078) at binaryurp/source/bridge.cxx:847
> #11 0x00007fac25b07409 in binaryurp::Bridge::~Bridge() (this=0x7fac24117078) at binaryurp/source/bridge.cxx:838
> #12 0x00007fac25b39ee1 in std::default_delete<binaryurp::IncomingRequest>::operator()(binaryurp::IncomingRequest*) const (this=<optimized out>, __ptr=0x7fabfc00ba50) at /usr/lib/gcc/x86_64-redhat-linux/6.2.1/../../../../include/c++/6.2.1/bits/unique_ptr.h:76
> #13 0x00007fac25b38fa8 in std::unique_ptr<binaryurp::IncomingRequest, std::default_delete<binaryurp::IncomingRequest> >::~unique_ptr() (this=0x7fac0bb74ac0) at /usr/lib/gcc/x86_64-redhat-linux/6.2.1/../../../../include/c++/6.2.1/bits/unique_ptr.h:236
> #14 0x00007fac25b36d71 in request(void*) (pThreadSpecificData=<optimized out>) at binaryurp/source/reader.cxx:83
> #15 0x00007fac47fbaa95 in cppu_threadpool::JobQueue::enter(long, bool) (this=<optimized out>, nDisposeId=<optimized out>, bReturnWhenNoJob=<optimized out>) at cppu/source/threadpool/jobqueue.cxx:107
> #16 0x00007fac47fbe1f9 in cppu_threadpool::ORequestThread::run() (this=0x7fabfc001160) at cppu/source/threadpool/thread.cxx:165
> #17 0x00007fac47fbf9ca in threadFunc(void*) (param=0x7fabfc001170) at include/osl/thread.hxx:185
> #18 0x00007fac4b4d5a3c in osl_thread_start_Impl(void*) (pData=0x7fabfc0011d0) at sal/osl/unx/thread.cxx:240
> #19 0x00007fac4ab2a5ca in start_thread (arg=0x7fac0bb75700) at pthread_create.c:333
> #20 0x00007fac4ae420ed in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109

while the main thread is already shutting down:

> #19 0x00007fac4b14100f in desktop::Desktop::doShutdown() (this=0x7fff8a6149d0) at desktop/source/app/app.cxx:1783
> #20 0x00007fac4b13f505 in desktop::Desktop::Main() (this=0x7fff8a6149d0) at desktop/source/app/app.cxx:1716
> #21 0x00007fac419a0f79 in ImplSVMain() () at vcl/source/app/svmain.cxx:185
> #22 0x00007fac419a1c14 in SVMain() () at vcl/source/app/svmain.cxx:223
> #23 0x00007fac4b17c773 in soffice_main() () at desktop/source/app/sofficemain.cxx:166
> #24 0x0000000000400916 in sal_main () at desktop/source/app/main.c:48
> #25 0x00000000004008fb in main (argc=<optimized out>, argv=<optimized out>) at desktop/source/app/main.c:47

Change-Id: I34beac40e89f6d522af49f9dcdb3ed2fc1259c4b
2016-12-07 12:39:36 +01:00

204 lines
5.9 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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>
#include <algorithm>
#include <cstdlib>
#include <osl/diagnose.h>
#include <uno/threadpool.h>
#include <sal/log.hxx>
#include "thread.hxx"
#include "jobqueue.hxx"
#include "threadpool.hxx"
using namespace osl;
using namespace rtl;
namespace cppu_threadpool {
ThreadAdmin::ThreadAdmin(): m_disposed(false) {}
ThreadAdmin::~ThreadAdmin()
{
SAL_WARN_IF(m_lst.size(), "cppu.threadpool", m_lst.size() << "Threads left");
}
bool ThreadAdmin::add( rtl::Reference< ORequestThread > const & p )
{
MutexGuard aGuard( m_mutex );
if( m_disposed )
{
return false;
}
m_lst.push_back( p );
return true;
}
void ThreadAdmin::remove_locked( rtl::Reference< ORequestThread > const & p )
{
::std::list< rtl::Reference< ORequestThread > >::iterator ii = ::std::find( m_lst.begin(), m_lst.end(), p );
if( ii != m_lst.end() )
{
m_lst.erase( ii );
}
}
void ThreadAdmin::remove( rtl::Reference< ORequestThread > const & p )
{
MutexGuard aGuard( m_mutex );
remove_locked( p );
}
void ThreadAdmin::join()
{
{
MutexGuard aGuard( m_mutex );
m_disposed = true;
}
for (;;)
{
rtl::Reference< ORequestThread > pCurrent;
{
MutexGuard aGuard( m_mutex );
if( m_lst.empty() )
{
break;
}
pCurrent = m_lst.front();
m_lst.pop_front();
}
if (pCurrent->getIdentifier()
!= osl::Thread::getCurrentIdentifier())
{
pCurrent->join();
}
}
}
ORequestThread::ORequestThread( ThreadPoolHolder const &aThreadPool,
JobQueue *pQueue,
const ByteSequence &aThreadId,
bool bAsynchron )
: m_aThreadPool( aThreadPool )
, m_pQueue( pQueue )
, m_aThreadId( aThreadId )
, m_bAsynchron( bAsynchron )
{}
ORequestThread::~ORequestThread() {}
void ORequestThread::setTask( JobQueue *pQueue,
const ByteSequence &aThreadId,
bool bAsynchron )
{
m_pQueue = pQueue;
m_aThreadId = aThreadId;
m_bAsynchron = bAsynchron;
}
bool ORequestThread::launch()
{
// Assumption is that osl::Thread::create returns normally with a true
// return value iff it causes osl::Thread::run to start executing:
acquire();
ThreadAdmin & rThreadAdmin = m_aThreadPool->getThreadAdmin();
osl::ClearableMutexGuard g(rThreadAdmin.m_mutex);
if (!rThreadAdmin.add( this )) {
return false;
}
try {
if (!create()) {
std::abort();
}
} catch (...) {
rThreadAdmin.remove_locked( this );
g.clear();
release();
throw;
}
return true;
}
void ORequestThread::onTerminated()
{
m_aThreadPool->getThreadAdmin().remove( this );
release();
}
void ORequestThread::run()
{
osl_setThreadName("cppu_threadpool::ORequestThread");
try
{
while ( m_pQueue )
{
if( ! m_bAsynchron )
{
if ( !uno_bindIdToCurrentThread( m_aThreadId.getHandle() ) )
{
OSL_ASSERT( false );
}
}
while( ! m_pQueue->isEmpty() )
{
// Note : Oneways should not get a disposable disposeid,
// It does not make sense to dispose a call in this state.
// That's way we put it an disposeid, that can't be used otherwise.
m_pQueue->enter(
sal::static_int_cast< sal_Int64 >(
reinterpret_cast< sal_IntPtr >(this)),
true );
if( m_pQueue->isEmpty() )
{
m_aThreadPool->revokeQueue( m_aThreadId , m_bAsynchron );
// Note : revokeQueue might have failed because m_pQueue.isEmpty()
// may be false (race).
}
}
delete m_pQueue;
m_pQueue = nullptr;
if( ! m_bAsynchron )
{
uno_releaseIdFromCurrentThread();
}
m_aThreadPool->waitInPool( this );
}
}
catch (...)
{
// Work around the problem that onTerminated is not called if run
// throws an exception:
onTerminated();
throw;
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */