From cd292163113134ccb7e037da70718bb6aaf6567c Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 13 Sep 2013 17:34:05 +0000 Subject: Add a module for preloading libraries, which supersedes that in daemon --- project2/basics/Jamfile.jam | 2 ++ project2/basics/preload.cpp | 52 +++++++++++++++++++++++++++++++++++ project2/daemon/p2daemonAppEngine.cpp | 18 ++---------- project2/daemon/p2daemonAppEngine.h | 4 --- 4 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 project2/basics/preload.cpp diff --git a/project2/basics/Jamfile.jam b/project2/basics/Jamfile.jam index b276ba1..02de4c3 100644 --- a/project2/basics/Jamfile.jam +++ b/project2/basics/Jamfile.jam @@ -3,6 +3,7 @@ alias glibmm : : : : "`pkg-config --libs glibmm-2.4`" ; lib boost_filesystem : : boost_filesystem ; +lib dl ; cpp-pch pch : pch.hpp : ../../libmisc @@ -16,6 +17,7 @@ lib p2basics : . ../../libmisc glibmm + dl boost_filesystem ../common//p2common ; diff --git a/project2/basics/preload.cpp b/project2/basics/preload.cpp new file mode 100644 index 0000000..e8aa6e9 --- /dev/null +++ b/project2/basics/preload.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +using namespace boost::filesystem; + +class Preload { + public: + INITOPTIONS; + + private: + typedef boost::shared_ptr Library; + typedef std::map Libraries; + + static void LoadLibrary(const VariableType & librarypath) + { + const auto beforeOpts = *Plugable::ComponentType::components(); + + void * handle = dlopen(librarypath, RTLD_NOW); + if (handle) { + Logger()->messagebf(LOG_DEBUG, "Loaded library '%s'", librarypath.as()); + } + else { + Logger()->messagebf(LOG_ERR, "Failed to load library '%s' (%s)", librarypath.as(), dlerror()); + throw std::runtime_error("module load failed"); + } + libs[librarypath.as()] = boost::shared_ptr(handle, &dlclose); + + const auto afterOpts = Plugable::ComponentType::components(); + BOOST_FOREACH(const auto & opt, *afterOpts) { + if (std::find(beforeOpts.begin(), beforeOpts.end(), opt) == beforeOpts.end()) { + opt->reset(); + } + } + } + + static void UnloadLibraries() + { + libs.clear(); + } + + static Libraries libs; +}; + +Preload::Libraries Preload::libs; + +DECLARE_OPTIONS(Preload, "Library preload options") +("library", Options::functions(Preload::LoadLibrary, Preload::UnloadLibraries), "Libraries to preload") +END_OPTIONS(Preload) + diff --git a/project2/daemon/p2daemonAppEngine.cpp b/project2/daemon/p2daemonAppEngine.cpp index cb414c5..7daed70 100644 --- a/project2/daemon/p2daemonAppEngine.cpp +++ b/project2/daemon/p2daemonAppEngine.cpp @@ -1,20 +1,18 @@ #include "p2daemonAppEngine.h" #include +#include #include std::string DaemonAppEngine::daemonType; -boost::optional DaemonAppEngine::libraryPath; Glib::ustring DaemonAppEngine::reqPlatform; DaemonPtr DaemonAppEngine::daemon; DECLARE_OPTIONS(DaemonAppEngine, "Project2 Daemon options") ("daemon.type", Options::value(&daemonType), "The core daemon module to run") -("daemon.library", Options::value(&libraryPath), "The optional library containing the core daemon to load") ("daemon.platform", Options::value(&reqPlatform), "Platform") END_OPTIONS(DaemonAppEngine); -DaemonAppEngine::DaemonAppEngine(int argc, char ** argv) : - library(!!libraryPath ? loadLibrary(*libraryPath) : boost::shared_ptr()) +DaemonAppEngine::DaemonAppEngine(int argc, char ** argv) { daemon = DaemonLoader::createNew(daemonType, argc, argv); } @@ -54,15 +52,3 @@ DaemonAppEngine::process() const Logger()->message(LOG_INFO, "Daemon exitted."); } -boost::shared_ptr -DaemonAppEngine::loadLibrary(const std::string & path) -{ - void * handle = dlopen(path.c_str(), RTLD_NOW); - if (!handle) { - Logger()->messagebf(LOG_ERR, "Failed to load library '%s' (%s)", path, dlerror()); - throw std::runtime_error("module load failed"); - } - return boost::shared_ptr(handle, &dlclose); -} - - diff --git a/project2/daemon/p2daemonAppEngine.h b/project2/daemon/p2daemonAppEngine.h index 236bef6..e6632f2 100644 --- a/project2/daemon/p2daemonAppEngine.h +++ b/project2/daemon/p2daemonAppEngine.h @@ -16,12 +16,8 @@ class DaemonAppEngine { static Glib::ustring reqPlatform; static std::string daemonType; - static boost::optional libraryPath; private: - static boost::shared_ptr loadLibrary(const std::string & path); - - boost::shared_ptr library; static void shutdown(int); static DaemonPtr daemon; }; -- cgit v1.2.3