提交 a4174645 编写于 作者: S storypku 提交者: Liu Jiaming

cyber: reorg optimized shared_library into independent directory

上级 a7f25a7b
......@@ -3,18 +3,6 @@ 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 = [
......@@ -31,7 +19,7 @@ cc_library(
deps = [
"//cyber:init",
"//cyber/common:log",
"//cyber/class_loader:shared_library",
"//cyber/class_loader/shared_library",
],
)
......
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "shared_library",
srcs = ["shared_library.cc"],
hdrs = ["shared_library.h"],
linkopts = ["-ldl"],
deps = [
":exceptions",
],
)
cc_library(
name = "exceptions",
hdrs = ["exceptions.h"],
)
cpplint()
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
* Copyright 2020 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.
......@@ -14,38 +14,29 @@
* limitations under the License.
*****************************************************************************/
#ifndef CYBER_CLASS_LOADER_UTILITY_EXCEPTION_H_
#define CYBER_CLASS_LOADER_UTILITY_EXCEPTION_H_
#ifndef CYBER_CLASS_LOADER_SHARED_LIBRARY_EXCEPTIONS_H_
#define CYBER_CLASS_LOADER_SHARED_LIBRARY_EXCEPTIONS_H_
#include <string>
#include <exception>
#include <stdexcept>
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_; \
#define DECLARE_SHARED_LIBRARY_EXCEPTION(CLS, BASE) \
class CLS : public BASE { \
public: \
explicit CLS(const std::string& err_msg) : BASE(err_msg) {} \
~CLS() throw() {} \
};
DECLARE_SHARED_LIBRARY_EXCEPTION(LibraryAlreadyLoadedException, std::runtime_error);
DECLARE_SHARED_LIBRARY_EXCEPTION(LibraryLoadException, std::runtime_error);
DECLARE_SHARED_LIBRARY_EXCEPTION(SymbolNotFoundException, std::runtime_error);
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_
#endif // CYBER_CLASS_LOADER_SHARED_LIBRARY_EXCEPTIONS_H_
......@@ -14,32 +14,31 @@
* limitations under the License.
*****************************************************************************/
#include "cyber/class_loader/utility/shared_library.h"
#include "cyber/class_loader/shared_library/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& path) {
Load(path, 0);
}
SharedLibrary::SharedLibrary(const std::string& filename, int flags) {
Load(filename, flags);
SharedLibrary::SharedLibrary(const std::string& path, int flags) {
Load(path, flags);
}
void SharedLibrary::Load(const std::string& filename) {
Load(filename, 0);
void SharedLibrary::Load(const std::string& path) {
Load(path, 0);
}
void SharedLibrary::Load(const std::string& filename, int flags) {
void SharedLibrary::Load(const std::string& path, int flags) {
std::lock_guard<std::mutex> lock(mutex_);
if (handle_) throw LibraryAlreadyLoadedException(filename);
if (handle_) throw LibraryAlreadyLoadedException(path);
int real_flag = RTLD_LAZY;
if (flags & SHLIB_LOCAL) {
......@@ -47,13 +46,13 @@ void SharedLibrary::Load(const std::string& filename, int flags) {
} else {
real_flag |= SHLIB_GLOBAL;
}
handle_ = dlopen(filename.c_str(), real_flag);
handle_ = dlopen(path.c_str(), real_flag);
if (!handle_) {
const char* err = dlerror();
throw LibraryLoadException(err ? std::string(err) : filename);
throw LibraryLoadException(err ? std::string(err) : path);
}
filename_ = filename;
path_ = path;
}
void SharedLibrary::Unload() {
......@@ -79,7 +78,7 @@ void* SharedLibrary::GetSymbol(const std::string& name) {
void* result = dlsym(handle_, name.c_str());
if (!result) {
throw NotFoundException(name);
throw SymbolNotFoundException(name);
}
return result;
......@@ -90,7 +89,6 @@ SharedLibrary::~SharedLibrary() {
}
} // namespace utility
} // namespace class_loader
} // namespace shared_library
} // namespace cyber
} // namespace apollo
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
* Copyright 2020 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.
......@@ -14,18 +14,17 @@
* limitations under the License.
*****************************************************************************/
#ifndef CYBER_CLASS_LOADER_UTILITY_SHARED_LIBRARY_H_
#define CYBER_CLASS_LOADER_UTILITY_SHARED_LIBRARY_H_
#ifndef CYBER_CLASS_LOADER_SHARED_LIBRARY_H_
#define CYBER_CLASS_LOADER_SHARED_LIBRARY_H_
#include <string>
#include <mutex>
#include "cyber/class_loader/utility/exception.h"
#include "cyber/class_loader/shared_library/exceptions.h"
namespace apollo {
namespace cyber {
namespace class_loader {
namespace utility {
class SharedLibrary {
public:
......@@ -36,15 +35,15 @@ class SharedLibrary {
SharedLibrary();
explicit SharedLibrary(const std::string& filename);
explicit SharedLibrary(const std::string& path);
SharedLibrary(const std::string& filename, int flags);
SharedLibrary(const std::string& path, int flags);
virtual ~SharedLibrary();
void Load(const std::string& filename);
void Load(const std::string& path);
void Load(const std::string& filename, int flags);
void Load(const std::string& path, int flags);
void Unload();
......@@ -54,21 +53,20 @@ class SharedLibrary {
void* GetSymbol(const std::string& name);
inline const std::string& GetPath() const { return filename_; }
inline const std::string& GetPath() const { return path_; }
SharedLibrary(const SharedLibrary&) = delete;
SharedLibrary& operator=(const SharedLibrary&) = delete;
private:
void* handle_;
std::string filename_;
void* handle_ = nullptr;
std::string path_;
std::mutex mutex_;
};
} // namespace utility
} // namespace class_loader
} // namespace cyber
} // namespace apollo
#endif // CYBER_CLASS_LOADER_UTILITY_SHARED_LIBRARY_H_
#endif // CYBER_CLASS_LOADER_SHARED_LIBRARY_H_
......@@ -28,7 +28,7 @@ std::recursive_mutex& GetClassFactoryMapMapMutex() {
return m;
}
std::recursive_mutex& GetLibPathShareLibMutex() {
std::recursive_mutex& GetLibPathSharedLibMutex() {
static std::recursive_mutex m;
return m;
}
......@@ -38,8 +38,8 @@ BaseToClassFactoryMapMap& GetClassFactoryMapMap() {
return instance;
}
LibpathSharedlibVector& GetLibPathShareLibVector() {
static LibpathSharedlibVector instance;
LibPathSharedLibVector& GetLibPathSharedLibVector() {
static LibPathSharedLibVector instance;
return instance;
}
......@@ -127,7 +127,7 @@ void DestroyClassFactoryObjectsOfLibrary(
if (class_factory_object->GetRelativeLibraryPath() == library_path &&
class_factory_object->IsOwnedBy(class_loader)) {
class_factory_object->RemoveOwnedClassLoader(class_loader);
// when no anybody owner,delete && erase
// when no anybody owner, delete && erase
if (!class_factory_object->IsOwnedByAnybody()) {
itr = class_factory_map->erase(itr);
delete class_factory_object;
......@@ -151,10 +151,10 @@ void DestroyClassFactoryObjectsOfLibrary(const std::string& library_path,
}
}
LibpathSharedlibVector::iterator FindLoadedLibrary(
LibPathSharedLibVector::iterator FindLoadedLibrary(
const std::string& library_path) {
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
LibpathSharedlibVector::iterator itr;
LibPathSharedLibVector& opened_libraries = GetLibPathSharedLibVector();
LibPathSharedLibVector::iterator itr;
for (itr = opened_libraries.begin(); itr != opened_libraries.end(); ++itr) {
if (itr->first == library_path) {
break;
......@@ -164,10 +164,10 @@ LibpathSharedlibVector::iterator FindLoadedLibrary(
}
bool IsLibraryLoadedByAnybody(const std::string& library_path) {
std::lock_guard<std::recursive_mutex> lck(GetLibPathShareLibMutex());
std::lock_guard<std::recursive_mutex> lck(GetLibPathSharedLibMutex());
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
LibpathSharedlibVector::iterator itr = FindLoadedLibrary(library_path);
LibPathSharedLibVector& opened_libraries = GetLibPathSharedLibVector();
LibPathSharedLibVector::iterator itr = FindLoadedLibrary(library_path);
return itr != opened_libraries.end();
}
......@@ -222,10 +222,10 @@ bool LoadLibrary(const std::string& library_path, ClassLoader* loader) {
SetCurLoadingLibraryName("");
SetCurActiveClassLoader(nullptr);
AERROR << "LibraryAlreadyLoadedException: " << e.what();
} catch (const NotFoundException& e) {
} catch (const SymbolNotFoundException& e) {
SetCurLoadingLibraryName("");
SetCurActiveClassLoader(nullptr);
AERROR << "NotFoundException: " << e.what();
AERROR << "SymbolNotFoundException: " << e.what();
}
SetCurLoadingLibraryName("");
......@@ -242,8 +242,8 @@ 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(GetLibPathShareLibMutex());
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
std::lock_guard<std::recursive_mutex> lck(GetLibPathSharedLibMutex());
LibPathSharedLibVector& opened_libraries = GetLibPathSharedLibVector();
opened_libraries.emplace_back(
std::pair<std::string, SharedLibraryPtr>(library_path, shared_library));
return true;
......@@ -251,9 +251,9 @@ bool LoadLibrary(const std::string& library_path, ClassLoader* loader) {
void UnloadLibrary(const std::string& library_path, ClassLoader* loader) {
{
std::lock_guard<std::recursive_mutex> lck(GetLibPathShareLibMutex());
LibpathSharedlibVector& opened_libraries = GetLibPathShareLibVector();
LibpathSharedlibVector::iterator itr = FindLoadedLibrary(library_path);
std::lock_guard<std::recursive_mutex> lck(GetLibPathSharedLibMutex());
LibPathSharedLibVector& opened_libraries = GetLibPathSharedLibVector();
LibPathSharedLibVector::iterator itr = FindLoadedLibrary(library_path);
if (itr == opened_libraries.end()) {
AERROR << "Attempt to UnloadLibrary lib, but can't find lib: "
<< library_path;
......@@ -268,12 +268,12 @@ void UnloadLibrary(const std::string& library_path, ClassLoader* loader) {
itr->second->Unload();
itr = opened_libraries.erase(itr);
} else {
AWARN << "ClassFactory Objects still remain in memory,meaning other "
"ClassLoaders are still using library:"
AWARN << "ClassFactory objects still remain in memory, meaning other"
"class loaders are still using library:"
<< library_path;
}
} catch (const std::exception& e) {
AERROR << "library unLoad error: " << e.what();
} catch (const std::runtime_error& e) {
AERROR << "Library unload error: " << e.what();
}
}
}
......
......@@ -28,7 +28,7 @@
#include <utility>
#include <vector>
#include "cyber/class_loader/utility/shared_library.h"
#include "cyber/class_loader/shared_library/shared_library.h"
#include "cyber/class_loader/utility/class_factory.h"
#include "cyber/common/log.h"
......@@ -47,14 +47,14 @@ using SharedLibraryPtr = std::shared_ptr<SharedLibrary>;
using ClassClassFactoryMap =
std::map<std::string, utility::AbstractClassFactoryBase*>;
using BaseToClassFactoryMapMap = std::map<std::string, ClassClassFactoryMap>;
using LibpathSharedlibVector =
using LibPathSharedLibVector =
std::vector<std::pair<std::string, SharedLibraryPtr>>;
using ClassFactoryVector = std::vector<AbstractClassFactoryBase*>;
BaseToClassFactoryMapMap& GetClassFactoryMapMap();
std::recursive_mutex& GetClassFactoryMapMapMutex();
LibpathSharedlibVector& GetLibPathShareLibVector();
std::recursive_mutex& GetLibPathShareLibMutex();
LibPathSharedLibVector& GetLibPathSharedLibVector();
std::recursive_mutex& GetLibPathSharedLibMutex();
ClassClassFactoryMap& GetClassFactoryMapByBaseClass(
const std::string& typeid_base_class_name);
std::string GetCurLoadingLibraryName();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册