RTXI  3.0.0
The Real-Time eXperiment Interface Reference Manual
dlplugin.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah,
4  Will Cornell Medical College
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "dlplugin.hpp"
22 
23 #include "debug.hpp"
24 
26 {
27  this->unloadAll();
28 }
29 
30 int DLL::Loader::load(const char* library)
31 {
32  DLL::dll_info dll {std::string(library), nullptr};
33  const std::unique_lock<std::mutex> lk(this->m_dll_mutex);
34  auto loc_iter =
35  std::find(this->loaded_plugins.begin(), this->loaded_plugins.end(), dll);
36  if (loc_iter != this->loaded_plugins.end()) {
37  return 0;
38  }
39 
40  void* handle = ::dlopen(library, RTLD_LOCAL | RTLD_NOW);
41  if (handle == nullptr) {
42  ERROR_MSG("DLL::Loader::load : Unable to load library {}", library);
43  // NOLINTNEXTLINE
44  ERROR_MSG("dlopen : {}", dlerror());
45  return -1;
46  }
47  dll.handle = handle;
48  this->loaded_plugins.push_back(dll);
49  return 0;
50 }
51 
52 void DLL::Loader::unload(const char* library)
53 {
54  const DLL::dll_info dll {std::string(library), nullptr};
55  const std::unique_lock<std::mutex> lk(this->m_dll_mutex);
56  auto loc_iter =
57  std::find(this->loaded_plugins.begin(), this->loaded_plugins.end(), dll);
58  if (loc_iter == this->loaded_plugins.end()) {
59  return;
60  }
61  ::dlclose(loc_iter->handle);
62  this->loaded_plugins.erase(loc_iter);
63 }
64 
66 {
67  for (auto& dll : this->loaded_plugins) {
68  this->unload(dll.library_name.c_str());
69  }
70 }
void unloadAll()
Definition: dlplugin.cpp:65
void unload(const char *library)
Definition: dlplugin.cpp:52
int load(const char *library)
Definition: dlplugin.cpp:30
void ERROR_MSG(const std::string &errmsg, Args... args)
Definition: debug.hpp:36