Apollo 10.0
自动驾驶开放平台
shared_library.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16//
17// Adapted from poco/Foundation/src/SharedLibrary_UNIX.cpp
18//
19// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
20// and Contributors.
21//
22// SPDX-License-Identifier: BSL-1.0
23//
25
26#include <dlfcn.h>
27
28namespace apollo {
29namespace cyber {
30namespace class_loader {
31
32std::mutex SharedLibrary::mutex_;
33
34SharedLibrary::SharedLibrary(const std::string& path) { Load(path, 0); }
35
36SharedLibrary::SharedLibrary(const std::string& path, int flags) {
37 Load(path, flags);
38}
39
40void SharedLibrary::Load(const std::string& path) { Load(path, 0); }
41
42void SharedLibrary::Load(const std::string& path, int flags) {
43 std::lock_guard<std::mutex> lock(mutex_);
44 if (handle_) throw LibraryAlreadyLoadedException(path);
45
46 int real_flag = RTLD_LAZY;
47 if (flags & SHLIB_LOCAL) {
48 real_flag |= RTLD_LOCAL;
49 } else {
50 real_flag |= RTLD_GLOBAL;
51 }
52 handle_ = dlopen(path.c_str(), real_flag);
53 if (!handle_) {
54 const char* err = dlerror();
55 throw LibraryLoadException(err ? std::string(err) : path);
56 }
57
58 path_ = path;
59}
60
62 std::lock_guard<std::mutex> lock(mutex_);
63 if (handle_) {
64 dlclose(handle_);
65 handle_ = nullptr;
66 }
67}
68
70 std::lock_guard<std::mutex> lock(mutex_);
71 return handle_ != nullptr;
72}
73
74bool SharedLibrary::HasSymbol(const std::string& name) {
75 return GetSymbol(name) != nullptr;
76}
77
78void* SharedLibrary::GetSymbol(const std::string& name) {
79 std::lock_guard<std::mutex> lock(mutex_);
80 if (!handle_) return nullptr;
81
82 void* result = dlsym(handle_, name.c_str());
83 if (!result) {
84 throw SymbolNotFoundException(name);
85 }
86
87 return result;
88}
89
91
92} // namespace class_loader
93} // namespace cyber
94} // namespace apollo
void * GetSymbol(const std::string &name)
bool HasSymbol(const std::string &name)
class register implement
Definition arena_queue.h:37