Skip to content
Snippets Groups Projects
Commit fb3d475c authored by Christoph Hasse's avatar Christoph Hasse :cartwheel_tone1:
Browse files

protect multi-job jit compilation from race conditions

parent 1b65336b
No related branches found
No related tags found
1 merge request!2699FunctorFactory, replace CLING backend with native compiler
......@@ -16,6 +16,7 @@
#include <chrono>
#include <cstdio>
#include <dlfcn.h>
#include <filesystem>
#include <fstream>
#include <set>
#include <vector>
......@@ -164,10 +165,17 @@ struct FunctorFactory : public extends<Service, Functors::IFactory> {
out.close();
}
// In a multi job scenario we want to avoid many jobs directly writing to
// the same file and potentially causing problems. Thus we first create a
// uniquely named library and then later rename it. The rename is atomic
// in the sense that a dlopen call will either load the already existing
// file or the newly renamed one. But it can't fail due to e.g. the file
// not having been fully overwritten or similar weird things.
auto const lib_tmp_full_path = m_jit_lib_dir + "FunctorJitLib_tmp_" + std::to_string( getpid() );
// functor_jitter is a shell script generated by cmake to invoke the
// correct compiler with the correct flags see:
// Phys/FunctorCore/CMakeLists.txt
auto cmd = "functor_jitter " + cpp_filename + " " + lib_full_path;
auto cmd = "functor_jitter " + cpp_filename + " " + lib_tmp_full_path;
if ( msgLevel( MSG::VERBOSE ) ) { verbose() << "Command that will be executed:\n" << cmd << endmsg; }
......@@ -178,6 +186,11 @@ struct FunctorFactory : public extends<Service, Functors::IFactory> {
info() << "Compilation of functor library took " << total_time.count() << " seconds" << endmsg;
if ( return_code != 0 ) { throw exception( "Non zero return code!\n" ); }
if ( msgLevel( MSG::VERBOSE ) ) {
verbose() << "Rename " << lib_tmp_full_path << " to " << lib_full_path << endmsg;
}
std::filesystem::rename( lib_tmp_full_path, lib_full_path );
m_lib_handle = LibHandle{dlopen( lib_full_path.c_str(), RTLD_LOCAL | RTLD_LAZY ), lib_closer};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment