Skip to content

Commit

Permalink
cyber: remove poco shared library dependency
Browse files Browse the repository at this point in the history
rebased on master HEAD
  • Loading branch information
daohu527 authored and storypku committed Jan 6, 2021
1 parent 919f478 commit a7f25a7
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 161 deletions.
14 changes: 13 additions & 1 deletion cyber/class_loader/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ load("//tools:cpplint.bzl", "cpplint")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "shared_library",
srcs = [
"utility/shared_library.cc",
],
hdrs = [
"utility/shared_library.h",
"utility/exception.h",
],
linkopts = ['-ldl'],
)

cc_library(
name = "class_loader",
srcs = [
Expand All @@ -19,7 +31,7 @@ cc_library(
deps = [
"//cyber:init",
"//cyber/common:log",
"@poco//:PocoFoundation",
"//cyber/class_loader:shared_library",
],
)

Expand Down
56 changes: 28 additions & 28 deletions cyber/class_loader/utility/class_loader_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ std::recursive_mutex& GetClassFactoryMapMapMutex() {
return m;
}

std::recursive_mutex& GetLibPathPocoShareLibMutex() {
std::recursive_mutex& GetLibPathShareLibMutex() {
static std::recursive_mutex m;
return m;
}
Expand All @@ -38,8 +38,8 @@ BaseToClassFactoryMapMap& GetClassFactoryMapMap() {
return instance;
}

LibpathPocolibVector& GetLibPathPocoShareLibVector() {
static LibpathPocolibVector instance;
LibpathSharedlibVector& GetLibPathShareLibVector() {
static LibpathSharedlibVector instance;
return instance;
}

Expand Down Expand Up @@ -151,10 +151,10 @@ void DestroyClassFactoryObjectsOfLibrary(const std::string& library_path,
}
}

LibpathPocolibVector::iterator FindLoadedLibrary(
LibpathSharedlibVector::iterator FindLoadedLibrary(
const std::string& library_path) {
LibpathPocolibVector& opened_libraries = GetLibPathPocoShareLibVector();
LibpathPocolibVector::iterator itr;
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
LibpathSharedlibVector::iterator itr;
for (itr = opened_libraries.begin(); itr != opened_libraries.end(); ++itr) {
if (itr->first == library_path) {
break;
Expand All @@ -164,10 +164,10 @@ LibpathPocolibVector::iterator FindLoadedLibrary(
}

bool IsLibraryLoadedByAnybody(const std::string& library_path) {
std::lock_guard<std::recursive_mutex> lck(GetLibPathPocoShareLibMutex());
std::lock_guard<std::recursive_mutex> lck(GetLibPathShareLibMutex());

LibpathPocolibVector& opened_libraries = GetLibPathPocoShareLibVector();
LibpathPocolibVector::iterator itr = FindLoadedLibrary(library_path);
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
LibpathSharedlibVector::iterator itr = FindLoadedLibrary(library_path);
return itr != opened_libraries.end();
}

Expand Down Expand Up @@ -205,35 +205,35 @@ bool LoadLibrary(const std::string& library_path, ClassLoader* loader) {
return true;
}

PocoLibraryPtr poco_library = nullptr;
SharedLibraryPtr shared_library = nullptr;
static std::recursive_mutex loader_mutex;
{
std::lock_guard<std::recursive_mutex> lck(loader_mutex);

try {
SetCurActiveClassLoader(loader);
SetCurLoadingLibraryName(library_path);
poco_library = PocoLibraryPtr(new Poco::SharedLibrary(library_path));
} catch (const Poco::LibraryLoadException& e) {
shared_library = SharedLibraryPtr(new SharedLibrary(library_path));
} catch (const LibraryLoadException& e) {
SetCurLoadingLibraryName("");
SetCurActiveClassLoader(nullptr);
AERROR << "poco LibraryLoadException: " << e.message();
} catch (const Poco::LibraryAlreadyLoadedException& e) {
AERROR << "LibraryLoadException: " << e.what();
} catch (const LibraryAlreadyLoadedException& e) {
SetCurLoadingLibraryName("");
SetCurActiveClassLoader(nullptr);
AERROR << "poco LibraryAlreadyLoadedException: " << e.message();
} catch (const Poco::NotFoundException& e) {
AERROR << "LibraryAlreadyLoadedException: " << e.what();
} catch (const NotFoundException& e) {
SetCurLoadingLibraryName("");
SetCurActiveClassLoader(nullptr);
AERROR << "poco NotFoundException: " << e.message();
AERROR << "NotFoundException: " << e.what();
}

SetCurLoadingLibraryName("");
SetCurActiveClassLoader(nullptr);
}

if (poco_library == nullptr) {
AERROR << "poco shared library failed: " << library_path;
if (shared_library == nullptr) {
AERROR << "shared library failed: " << library_path;
return false;
}

Expand All @@ -242,18 +242,18 @@ bool LoadLibrary(const std::string& library_path, ClassLoader* loader) {
AWARN << "Class factory objs counts is 0, maybe registerclass failed.";
}

std::lock_guard<std::recursive_mutex> lck(GetLibPathPocoShareLibMutex());
LibpathPocolibVector& opened_libraries = GetLibPathPocoShareLibVector();
std::lock_guard<std::recursive_mutex> lck(GetLibPathShareLibMutex());
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
opened_libraries.emplace_back(
std::pair<std::string, PocoLibraryPtr>(library_path, poco_library));
std::pair<std::string, SharedLibraryPtr>(library_path, shared_library));
return true;
}

void UnloadLibrary(const std::string& library_path, ClassLoader* loader) {
{
std::lock_guard<std::recursive_mutex> lck(GetLibPathPocoShareLibMutex());
LibpathPocolibVector& opened_libraries = GetLibPathPocoShareLibVector();
LibpathPocolibVector::iterator itr = FindLoadedLibrary(library_path);
std::lock_guard<std::recursive_mutex> lck(GetLibPathShareLibMutex());
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
LibpathSharedlibVector::iterator itr = FindLoadedLibrary(library_path);
if (itr == opened_libraries.end()) {
AERROR << "Attempt to UnloadLibrary lib, but can't find lib: "
<< library_path;
Expand All @@ -265,15 +265,15 @@ void UnloadLibrary(const std::string& library_path, ClassLoader* loader) {
DestroyClassFactoryObjectsOfLibrary(library_path, loader);

if (GetAllClassFactoryObjectsOfLibrary(library_path).empty()) {
itr->second->unload();
itr->second->Unload();
itr = opened_libraries.erase(itr);
} else {
AWARN << "ClassFactory Objects still remain in memory,meaning other "
"ClassLoaders are still using library:"
<< library_path;
}
} catch (const Poco::RuntimeException& e) {
AERROR << "library unLoad error: Poco::RuntimeException: " << e.message();
} catch (const std::exception& e) {
AERROR << "library unLoad error: " << e.what();
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions cyber/class_loader/utility/class_loader_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
#include <utility>
#include <vector>

#include "Poco/SharedLibrary.h"

#include "cyber/class_loader/utility/shared_library.h"
#include "cyber/class_loader/utility/class_factory.h"
#include "cyber/common/log.h"

Expand All @@ -44,18 +43,18 @@ class ClassLoader;

namespace utility {

using PocoLibraryPtr = std::shared_ptr<Poco::SharedLibrary>;
using SharedLibraryPtr = std::shared_ptr<SharedLibrary>;
using ClassClassFactoryMap =
std::map<std::string, utility::AbstractClassFactoryBase*>;
using BaseToClassFactoryMapMap = std::map<std::string, ClassClassFactoryMap>;
using LibpathPocolibVector =
std::vector<std::pair<std::string, PocoLibraryPtr>>;
using LibpathSharedlibVector =
std::vector<std::pair<std::string, SharedLibraryPtr>>;
using ClassFactoryVector = std::vector<AbstractClassFactoryBase*>;

BaseToClassFactoryMapMap& GetClassFactoryMapMap();
std::recursive_mutex& GetClassFactoryMapMapMutex();
LibpathPocolibVector& GetLibPathPocoShareLibVector();
std::recursive_mutex& GetLibPathPocoShareLibMutex();
LibpathSharedlibVector& GetLibPathShareLibVector();
std::recursive_mutex& GetLibPathShareLibMutex();
ClassClassFactoryMap& GetClassFactoryMapByBaseClass(
const std::string& typeid_base_class_name);
std::string GetCurLoadingLibraryName();
Expand Down
51 changes: 51 additions & 0 deletions cyber/class_loader/utility/exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#ifndef CYBER_CLASS_LOADER_UTILITY_EXCEPTION_H_
#define CYBER_CLASS_LOADER_UTILITY_EXCEPTION_H_

#include <string>
#include <exception>


namespace apollo {
namespace cyber {
namespace class_loader {
namespace utility {

#define DECLARE_EXCEPTION_CLASS(CLS, BASE) \
class CLS : public BASE { \
public: \
explicit CLS(const std::string& err_msg) : err_msg_(err_msg) {} \
const char* what() const noexcept override { \
return err_msg_.c_str(); \
} \
private: \
std::string err_msg_; \
};


DECLARE_EXCEPTION_CLASS(LibraryAlreadyLoadedException, std::exception)
DECLARE_EXCEPTION_CLASS(LibraryLoadException, std::exception)
DECLARE_EXCEPTION_CLASS(NotFoundException, std::exception)


} // namespace utility
} // namespace class_loader
} // namespace cyber
} // namespace apollo

#endif // CYBER_CLASS_LOADER_UTILITY_EXCEPTION_H_
96 changes: 96 additions & 0 deletions cyber/class_loader/utility/shared_library.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#include "cyber/class_loader/utility/shared_library.h"

#include <dlfcn.h>

namespace apollo {
namespace cyber {
namespace class_loader {
namespace utility {

SharedLibrary::SharedLibrary() : handle_(nullptr) {}

SharedLibrary::SharedLibrary(const std::string& filename) {
Load(filename, 0);
}

SharedLibrary::SharedLibrary(const std::string& filename, int flags) {
Load(filename, flags);
}

void SharedLibrary::Load(const std::string& filename) {
Load(filename, 0);
}

void SharedLibrary::Load(const std::string& filename, int flags) {
std::lock_guard<std::mutex> lock(mutex_);
if (handle_) throw LibraryAlreadyLoadedException(filename);

int real_flag = RTLD_LAZY;
if (flags & SHLIB_LOCAL) {
real_flag |= SHLIB_LOCAL;
} else {
real_flag |= SHLIB_GLOBAL;
}
handle_ = dlopen(filename.c_str(), real_flag);
if (!handle_) {
const char* err = dlerror();
throw LibraryLoadException(err ? std::string(err) : filename);
}

filename_ = filename;
}

void SharedLibrary::Unload() {
std::lock_guard<std::mutex> lock(mutex_);
if (handle_) {
dlclose(handle_);
handle_ = nullptr;
}
}

bool SharedLibrary::IsLoaded() {
std::lock_guard<std::mutex> lock(mutex_);
return handle_ != nullptr;
}

bool SharedLibrary::HasSymbol(const std::string& name) {
return GetSymbol(name) != nullptr;
}

void* SharedLibrary::GetSymbol(const std::string& name) {
std::lock_guard<std::mutex> lock(mutex_);
if (!handle_) return nullptr;

void* result = dlsym(handle_, name.c_str());
if (!result) {
throw NotFoundException(name);
}

return result;
}

SharedLibrary::~SharedLibrary() {
Unload();
}


} // namespace utility
} // namespace class_loader
} // namespace cyber
} // namespace apollo
Loading

0 comments on commit a7f25a7

Please sign in to comment.