/* -*- 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 .
*/
/****************** * This method lets the thread wait a certain amount of time. If within this timespan * a new request comes in, this thread is reused. This is done only to improve performance, * it is not required for threadpool functionality.
******************/ void ThreadPool::waitInPool( rtl::Reference< ORequestThread > const & pThread )
{
WaitingThread waitingThread(pThread);
{
std::scoped_lock guard( m_mutexWaitingThreadList );
m_dequeThreads.push_front( &waitingThread );
}
// let the thread wait 2 seconds
waitingThread.condition.wait( std::chrono::seconds(2) );
{
std::scoped_lock guard ( m_mutexWaitingThreadList ); if( waitingThread.thread.is() )
{ // thread wasn't reused, remove it from the list
WaitingThreadDeque::iterator ii = find(
m_dequeThreads.begin(), m_dequeThreads.end(), &waitingThread );
OSL_ASSERT( ii != m_dequeThreads.end() );
m_dequeThreads.erase( ii );
}
}
}
void ThreadPool::joinWorkers()
{
{
std::scoped_lock guard( m_mutexWaitingThreadList ); for (autoconst& thread : m_dequeThreads)
{ // wake the threads up
thread->condition.set();
}
}
m_aThreadAdmin.join();
}
bool ThreadPool::createThread( JobQueue *pQueue , const ByteSequence &aThreadId, bool bAsynchron )
{
{ // Can a thread be reused ?
std::scoped_lock guard( m_mutexWaitingThreadList ); if( ! m_dequeThreads.empty() )
{ // inform the thread and let it go struct WaitingThread *pWaitingThread = m_dequeThreads.back();
pWaitingThread->thread->setTask( pQueue , aThreadId , bAsynchron );
pWaitingThread->thread = nullptr;
// remove from list
m_dequeThreads.pop_back();
// let the thread go
pWaitingThread->condition.set(); returntrue;
}
}
ThreadIdHashMap::iterator ii = m_mapQueue.find( aThreadId );
OSL_ASSERT( ii != m_mapQueue.end() );
if( bAsynchron )
{ if( ! (*ii).second.second->isEmpty() )
{ // another thread has put something into the queue returnfalse;
}
(*ii).second.second = nullptr; if( (*ii).second.first )
{ // all oneway request have been processed, now // synchronous requests may go on
(*ii).second.first->resume();
}
} else
{ if( ! (*ii).second.first->isEmpty() )
{ // another thread has put something into the queue returnfalse;
}
(*ii).second.first = nullptr;
}
// All uno_ThreadPool handles in g_pThreadpoolHashSet with overlapping life // spans share one ThreadPool instance. When g_pThreadpoolHashSet becomes empty // (within the last uno_threadpool_destroy) all worker threads spawned by that // ThreadPool instance are joined (which implies that uno_threadpool_destroy // must never be called from a worker thread); afterwards, the next call to // uno_threadpool_create (if any) will lead to a new ThreadPool instance.
extern"C" uno_ThreadPool SAL_CALL
uno_threadpool_create() noexcept
{
MutexGuard guard( Mutex::getGlobalMutex() );
ThreadPoolHolder p; if( ! g_pThreadpoolHashSet )
{
g_pThreadpoolHashSet = new ThreadpoolHashSet;
p = new ThreadPool;
} else
{
assert( !g_pThreadpoolHashSet->empty() );
p = g_pThreadpoolHashSet->begin()->second;
}
// Just ensure that the handle is unique in the process (via heap)
uno_ThreadPool h = newstruct _uno_ThreadPool;
g_pThreadpoolHashSet->emplace( h, p ); return h;
}
extern"C"void SAL_CALL
uno_threadpool_detach(SAL_UNUSED_PARAMETER uno_ThreadPool) noexcept
{ // we might do here some tidying up in case a thread called attach but never detach
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.