Skip to content
Snippets Groups Projects

Do not arbitrarily release Python GIL in HLTControlFlowMgr

Merged Marco Clemencic requested to merge do-not-release-gil into master
All threads resolved!
2 files
+ 33
48
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -38,7 +38,6 @@
#include "ThreadPool.h"
#include "tbb/task_arena.h"
#include "tbb/task_scheduler_observer.h"
#include <Python.h>
#include <algorithm>
#include <chrono>
#include <condition_variable>
@@ -322,13 +321,6 @@ namespace {
void on_scheduler_entry( bool ) override { m_thread_count++; }
void on_scheduler_exit( bool ) override { m_thread_count--; }
};
/// Helper class to temporarily release the GIL to allow other threads to take it.
struct PyGILReleaseGuard {
PyGILReleaseGuard() : state( PyEval_SaveThread() ) {}
~PyGILReleaseGuard() { PyEval_RestoreThread( state ); }
PyThreadState* state;
};
} // namespace
StatusCode HLTControlFlowMgr::initialize() {
@@ -841,50 +833,44 @@ StatusCode HLTControlFlowMgr::nextEvent( int maxevt ) {
[]( uint64_t total, auto const& counter ) { return total + counter.sum().count(); } );
};
{
// Release the GIL so that XMLSummarySvc can acquire it from within the event execution thread.
// Probably XMLSummarySvc fails with multiple threads but it should be redone anyway.
PyGILReleaseGuard gilRelease;
while ( maxevt < 0 || m_nextevt < maxevt ) {
if ( m_shutdown_now ) {
shutdown_threadpool();
while ( not empty() ) pop();
return StatusCode::FAILURE;
}
if ( m_startTimeAtEvt == m_nextevt ) {
auto currtime = std::time( nullptr );
info() << "Timing started at: " << std::put_time( std::localtime( &currtime ), "%T" ) << endmsg;
m_alloc_tracker->beginTracking();
startTimeAndTicks.emplace( Clock::now(), getTotalWorkerTicks() );
}
if ( m_stopTimeAtEvt == m_nextevt ) {
endTimeAndTicks.emplace( Clock::now(), getTotalWorkerTicks() );
m_alloc_tracker->endTracking();
auto currtime = std::time( nullptr );
info() << "Timing stopped at: " << std::put_time( std::localtime( &currtime ), "%T" ) << endmsg;
}
auto evtContext = createEventContext();
if ( !evtContext.valid() ) {
if ( m_nextevt == -1 ) break; // finished
shutdown_threadpool();
return StatusCode::FAILURE; // else we have an success --> exit loop
}
push( std::move( evtContext ) );
pop();
} // end main loop on finished events
if ( !endTimeAndTicks ) {
while ( maxevt < 0 || m_nextevt < maxevt ) {
if ( m_shutdown_now ) {
shutdown_threadpool();
while ( not empty() ) pop();
return StatusCode::FAILURE;
}
if ( m_startTimeAtEvt == m_nextevt ) {
auto currtime = std::time( nullptr );
info() << "Timing started at: " << std::put_time( std::localtime( &currtime ), "%T" ) << endmsg;
m_alloc_tracker->beginTracking();
startTimeAndTicks.emplace( Clock::now(), getTotalWorkerTicks() );
}
if ( m_stopTimeAtEvt == m_nextevt ) {
endTimeAndTicks.emplace( Clock::now(), getTotalWorkerTicks() );
m_stopTimeAtEvt = m_finishedEvt;
m_alloc_tracker->endTracking();
auto currtime = std::time( nullptr );
info() << "Timing stopped at: " << std::put_time( std::localtime( &currtime ), "%T" ) << endmsg;
}
auto evtContext = createEventContext();
if ( !evtContext.valid() ) {
if ( m_nextevt == -1 ) break; // finished
shutdown_threadpool();
return StatusCode::FAILURE; // else we have an success --> exit loop
}
push( std::move( evtContext ) );
pop();
} // end main loop on finished events
if ( !endTimeAndTicks ) {
endTimeAndTicks.emplace( Clock::now(), getTotalWorkerTicks() );
m_stopTimeAtEvt = m_finishedEvt;
m_alloc_tracker->endTracking();
}
shutdown_threadpool();
while ( not empty() ) pop();
shutdown_threadpool();
while ( not empty() ) pop();
releaseEvtSelContext().ignore();
}
releaseEvtSelContext().ignore();
if ( !startTimeAndTicks ) {
info() << "---> Loop over " << m_finishedEvt << " Events Finished - "
Loading