The Gaudi Framework  master (bdbe7744)
Loading...
Searching...
No Matches
FiberManager.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11
12#include "FiberManager.h"
13#include <boost/version.hpp>
14#if ( BOOST_VERSION >= 108400 )
15# include <boost/fiber/stack_allocator_wrapper.hpp>
16#endif
17
19 // Start a fiber scheduler with a shared work queue on the current thread.
20 // The fiber scheduler is configured not to sleep in case there are no ready fibers, but this situation should never
21 // happen as the main fiber of this thread should never be suspended so it's always active and scheduler will never
22 // try to pick-up another fiber.
23 boost::fibers::use_scheduling_algorithm<SchedAlgo>();
24
25 // Create a pool of threads running fiber schedulers with shared work queue.
26 // The scheduler in each thread is configured to sleep while no fibers are ready and needs to be manual
27 // notified if it's asleep when a new work item is added.
28 const auto sleep_while_idle = true;
29 m_schedAlgoList.resize( n_threads );
30 m_threads.reserve( n_threads );
31 for ( int i = 0; i < n_threads; ++i ) {
32 m_threads.emplace_back( [this, i]() {
33 // Save handle to the scheduler of this thread so it can be manually notified.
34 auto* algo_ptr = new SchedAlgo( sleep_while_idle );
35 m_schedAlgoList.at( i ) = algo_ptr;
36#if ( BOOST_VERSION >= 108400 )
37 boost::fibers::initialize_thread( algo_ptr,
38 boost::fibers::make_stack_allocator_wrapper<boost::fibers::default_stack>() );
39#else
40 boost::fibers::context::active()->get_scheduler()->set_algo( algo_ptr );
41#endif
42 // Suspend the main fiber of this worker so it doesn't exit prematurely.
43 std::unique_lock lck{ m_shuttingDown_mtx };
44 m_shuttingDown_cv.wait( lck );
45 } );
46 }
47}
48
50 // Notify the main fibers of the worker threads to exit.
51 m_shuttingDown_cv.notify_all();
52}
boost::fibers::condition_variable m_shuttingDown_cv
boost::fibers::algo::shared_work SchedAlgo
std::vector< SchedAlgo * > m_schedAlgoList
boost::fibers::mutex m_shuttingDown_mtx
std::vector< std::jthread > m_threads
FiberManager(int n_threads)
FiberManager constructor.