-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run G4 workers on their own dedicated threads #34899
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7280fe5
Added ThreadHandoff class
Dr15Jones e51cf0d
Run G4 worker on its own dedicated thread
Dr15Jones aa64686
applied code format
Dr15Jones 12c1f60
Removed thread_local from RunManagerMTWorker
Dr15Jones e058dfb
Be able to set worker thread stack size in configuration
Dr15Jones bb5bf39
Set random number system in worker thread
Dr15Jones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#ifndef SimG4Core_Application_ThreadHandoff_h | ||
#define SimG4Core_Application_ThreadHandoff_h | ||
// -*- C++ -*- | ||
// | ||
// Package: SimG4Core/Application | ||
// Class : ThreadHandoff | ||
// | ||
/**\class ThreadHandoff ThreadHandoff.h "SimG4Core/Application/interface/ThreadHandoff.h" | ||
|
||
Description: [one line class summary] | ||
|
||
Usage: | ||
<usage> | ||
|
||
*/ | ||
// | ||
// Original Author: Christopher Jones | ||
// Created: Mon, 16 Aug 2021 13:51:53 GMT | ||
// | ||
|
||
// system include files | ||
#include <condition_variable> | ||
#include <cstring> //strerror_r | ||
#include <exception> | ||
#include <mutex> | ||
#include <pthread.h> | ||
|
||
// user include files | ||
|
||
// forward declarations | ||
|
||
namespace omt { | ||
class ThreadHandoff { | ||
public: | ||
explicit ThreadHandoff(int stackSize); | ||
~ThreadHandoff(); | ||
|
||
ThreadHandoff(const ThreadHandoff&) = delete; // stop default | ||
const ThreadHandoff& operator=(const ThreadHandoff&) = delete; // stop default | ||
|
||
template <typename F> | ||
void runAndWait(F&& iF) { | ||
Functor<F> f{std::move(iF)}; | ||
|
||
std::unique_lock<std::mutex> lck(m_mutex); | ||
m_loopReady = false; | ||
m_toRun = &f; | ||
|
||
m_threadHandoff.notify_one(); | ||
|
||
m_threadHandoff.wait(lck, [this]() { return m_loopReady; }); | ||
auto e = f.exception(); | ||
if (e) { | ||
std::rethrow_exception(e); | ||
} | ||
} | ||
|
||
void stopThread() { | ||
runAndWait([this]() { m_stopThread = true; }); | ||
} | ||
|
||
private: | ||
class FunctorBase { | ||
public: | ||
virtual ~FunctorBase() {} | ||
virtual void execute() = 0; | ||
}; | ||
template <typename F> | ||
class Functor : public FunctorBase { | ||
public: | ||
explicit Functor(F&& iF) : m_f(std::move(iF)) {} | ||
void execute() final { | ||
try { | ||
m_f(); | ||
} catch (...) { | ||
m_except = std::current_exception(); | ||
} | ||
} | ||
std::exception_ptr exception() { return m_except; } | ||
|
||
private: | ||
F m_f; | ||
std::exception_ptr m_except; | ||
}; | ||
|
||
static void* threadLoop(void* iArgs); | ||
|
||
// ---------- member data -------------------------------- | ||
pthread_t m_thread; | ||
std::mutex m_mutex; | ||
std::condition_variable m_threadHandoff; | ||
|
||
FunctorBase* m_toRun{nullptr}; | ||
bool m_loopReady{false}; | ||
bool m_stopThread{false}; | ||
}; | ||
} // namespace omt | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Dr15Jones , is it safe calling edm::ServiceRegistry::instance() in a destructor? May be better keeping the pointer as a class member?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The framework guarantees that it is safe to use the Service system in module destructors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
If the framework guarantee the order of destruction, it is fine.