The Gaudi Framework  v39r1 (adb068b2)
FiberManager.h
Go to the documentation of this file.
1 
2 /***********************************************************************************\
3 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
4 * *
5 * This software is distributed under the terms of the Apache version 2 licence, *
6 * copied verbatim in the file "LICENSE". *
7 * *
8 * In applying this licence, CERN does not waive the privileges and immunities *
9 * granted to it by virtue of its status as an Intergovernmental Organization *
10 * or submit itself to any jurisdiction. *
11 \***********************************************************************************/
12 #pragma once
13 
14 #include <boost/fiber/algo/shared_work.hpp>
15 #include <boost/fiber/condition_variable.hpp>
16 #include <boost/fiber/fiber.hpp>
17 #include <boost/fiber/mutex.hpp>
18 
19 #include <fmt/format.h>
20 
34 class FiberManager {
35 public:
42  for ( int i = 0; i < n_threads; ++i ) {
44  boost::fibers::use_scheduling_algorithm<boost::fibers::algo::shared_work>( true );
46  m_shuttingDown_cv.wait( lck );
47  } ) );
48  }
49  }
50 
52  m_shuttingDown_cv.notify_all();
53 
54  for ( std::thread& t : m_threads ) { t.join(); }
55  }
56 
65  template <typename F>
66  void schedule( F&& func ) {
67  if ( !m_activated ) {
68  // Since we never call boost::this_fiber::yield, fibers never actually run on this thread
69  boost::fibers::use_scheduling_algorithm<boost::fibers::algo::shared_work>( true );
70  m_activated = true;
71  }
72  boost::fibers::fiber( boost::fibers::launch::post, std::forward<F>( func ) ).detach();
73  }
74 
75 private:
76  boost::fibers::condition_variable m_shuttingDown_cv{};
77  boost::fibers::mutex m_shuttingDown_mtx{};
79  bool m_activated = false; // set to true when first fiber is scheduled
80 };
FiberManager
Definition: FiberManager.h:34
FiberManager::m_threads
std::vector< std::thread > m_threads
Definition: FiberManager.h:78
prepareBenchmark.n_threads
n_threads
Definition: prepareBenchmark.py:40
std::vector< std::thread >
FiberManager::~FiberManager
~FiberManager()
Definition: FiberManager.h:51
bug_34121.t
t
Definition: bug_34121.py:31
std::thread
STL class.
cpluginsvc.func
func
Definition: cpluginsvc.py:235
FiberManager::schedule
void schedule(F &&func)
Schedule work to run on the asynchronous pool.
Definition: FiberManager.h:66
std::unique_lock
STL class.
FiberManager::FiberManager
FiberManager(int n_threads)
FiberManager constructor.
Definition: FiberManager.h:41
FiberManager::m_shuttingDown_mtx
boost::fibers::mutex m_shuttingDown_mtx
Definition: FiberManager.h:77
std::vector::emplace_back
T emplace_back(T... args)
FiberManager::m_activated
bool m_activated
Definition: FiberManager.h:79
FiberManager::m_shuttingDown_cv
boost::fibers::condition_variable m_shuttingDown_cv
Definition: FiberManager.h:76