class_loader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
31 
32 #include <string>
33 
34 #include "Poco/SharedLibrary.h"
35 
36 namespace class_loader
37 {
38 
40 
42 {
44 }
45 
46 std::string systemLibraryPrefix()
47 {
48 #ifndef _WIN32
49  return "lib";
50 #endif
51  return "";
52 }
53 
54 std::string systemLibrarySuffix()
55 {
56  return Poco::SharedLibrary::suffix();
57 }
58 
59 
60 std::string systemLibraryFormat(const std::string & library_name)
61 {
62  return systemLibraryPrefix() + library_name + systemLibrarySuffix();
63 }
64 
65 ClassLoader::ClassLoader(const std::string & library_path, bool ondemand_load_unload)
66 : ondemand_load_unload_(ondemand_load_unload),
67  library_path_(library_path),
68  load_ref_count_(0),
69  plugin_ref_count_(0)
70 {
71  CONSOLE_BRIDGE_logDebug(
72  "class_loader.ClassLoader: "
73  "Constructing new ClassLoader (%p) bound to library %s.",
74  this, library_path.c_str());
76  loadLibrary();
77  }
78 }
79 
81 {
82  CONSOLE_BRIDGE_logDebug("%s",
83  "class_loader.ClassLoader: "
84  "Destroying class loader, unloading associated library...\n");
85  unloadLibrary(); // TODO(mikaelarguedas): while(unloadLibrary() > 0){} ??
86 }
87 
89 {
91 }
92 
94 {
96 }
97 
99 {
100  boost::recursive_mutex::scoped_lock lock(load_ref_count_mutex_);
103 }
104 
106 {
107  return unloadLibraryInternal(true);
108 }
109 
110 int ClassLoader::unloadLibraryInternal(bool lock_plugin_ref_count)
111 {
112  boost::recursive_mutex::scoped_lock load_ref_lock(load_ref_count_mutex_);
113  boost::recursive_mutex::scoped_lock plugin_ref_lock;
114  if (lock_plugin_ref_count) {
115  plugin_ref_lock = boost::recursive_mutex::scoped_lock(plugin_ref_count_mutex_);
116  }
117 
118  if (plugin_ref_count_ > 0) {
119  CONSOLE_BRIDGE_logWarn("class_loader.ClassLoader: SEVERE WARNING!!!\n"
120  "Attempting to unload %s\n"
121  "while objects created by this library still exist in the heap!\n"
122  "You should delete your objects before destroying the ClassLoader. "
123  "The library will NOT be unloaded.", library_path_.c_str());
124  } else {
126  if (0 == load_ref_count_) {
128  } else if (load_ref_count_ < 0) {
129  load_ref_count_ = 0;
130  }
131  }
132  return load_ref_count_;
133 }
134 
135 } // namespace class_loader
class_loader::impl::loadLibrary
CLASS_LOADER_PUBLIC void loadLibrary(const std::string &library_path, ClassLoader *loader)
Loads a library into memory if it has not already been done so. Attempting to load an already loaded ...
Definition: class_loader_core.cpp:430
class_loader::ClassLoader::hasUnmanagedInstanceBeenCreated
static CLASS_LOADER_PUBLIC bool hasUnmanagedInstanceBeenCreated()
Getter for if an unmanaged (i.e. unsafe) instance has been created flag.
Definition: class_loader.cpp:41
class_loader
Definition: class_loader.hpp:50
class_loader::systemLibraryFormat
CLASS_LOADER_PUBLIC std::string systemLibraryFormat(const std::string &library_name)
Returns a platform specific version of a basic library name.
Definition: class_loader.cpp:60
class_loader::ClassLoader::isOnDemandLoadUnloadEnabled
CLASS_LOADER_PUBLIC bool isOnDemandLoadUnloadEnabled()
Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when th...
Definition: class_loader.hpp:219
class_loader::ClassLoader::load_ref_count_mutex_
boost::recursive_mutex load_ref_count_mutex_
Definition: class_loader.hpp:330
class_loader::systemLibrarySuffix
CLASS_LOADER_PUBLIC std::string systemLibrarySuffix()
Returns runtime library extension for native os.
Definition: class_loader.cpp:54
class_loader::ClassLoader::~ClassLoader
virtual CLASS_LOADER_PUBLIC ~ClassLoader()
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically.
Definition: class_loader.cpp:80
class_loader::ClassLoader::isLibraryLoadedByAnyClassloader
CLASS_LOADER_PUBLIC bool isLibraryLoadedByAnyClassloader()
Indicates if a library is loaded by some entity in the plugin system (another ClassLoader),...
Definition: class_loader.cpp:93
class_loader::ClassLoader::plugin_ref_count_mutex_
boost::recursive_mutex plugin_ref_count_mutex_
Definition: class_loader.hpp:332
class_loader::impl::isLibraryLoadedByAnybody
CLASS_LOADER_PUBLIC bool isLibraryLoadedByAnybody(const std::string &library_path)
Indicates if passed library has been loaded by ANY ClassLoader.
Definition: class_loader_core.cpp:291
class_loader::ClassLoader::loadLibrary
CLASS_LOADER_PUBLIC void loadLibrary()
Attempts to load a library on behalf of the ClassLoader. If the library is already opened,...
Definition: class_loader.cpp:98
class_loader::ClassLoader::plugin_ref_count_
int plugin_ref_count_
Definition: class_loader.hpp:331
class_loader.hpp
class_loader::systemLibraryPrefix
CLASS_LOADER_PUBLIC std::string systemLibraryPrefix()
Returns the default library prefix for the native os.
Definition: class_loader.cpp:46
class_loader::ClassLoader::unloadLibrary
CLASS_LOADER_PUBLIC int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened,...
Definition: class_loader.cpp:105
class_loader::ClassLoader::load_ref_count_
int load_ref_count_
Definition: class_loader.hpp:329
class_loader::ClassLoader::unloadLibraryInternal
CLASS_LOADER_PUBLIC int unloadLibraryInternal(bool lock_plugin_ref_count)
As the library may be unloaded in "on-demand load/unload" mode, unload maybe called from createInstan...
Definition: class_loader.cpp:110
class_loader::ClassLoader::has_unmananged_instance_been_created_
static CLASS_LOADER_PUBLIC bool has_unmananged_instance_been_created_
Definition: class_loader.hpp:335
class_loader::impl::isLibraryLoaded
CLASS_LOADER_PUBLIC bool isLibraryLoaded(const std::string &library_path, ClassLoader *loader)
Indicates if passed library loaded within scope of a ClassLoader. The library maybe loaded in memory,...
Definition: class_loader_core.cpp:306
class_loader::ClassLoader::library_path_
std::string library_path_
Definition: class_loader.hpp:328
class_loader::ClassLoader::getLibraryPath
CLASS_LOADER_PUBLIC std::string getLibraryPath()
Gets the full-qualified path and name of the library associated with this class loader.
Definition: class_loader.hpp:115
class_loader::impl::unloadLibrary
CLASS_LOADER_PUBLIC void unloadLibrary(const std::string &library_path, ClassLoader *loader)
Unloads a library if it loaded in memory and cleans up its corresponding class factories....
Definition: class_loader_core.cpp:521
class_loader::ClassLoader::ClassLoader
CLASS_LOADER_PUBLIC ClassLoader(const std::string &library_path, bool ondemand_load_unload=false)
Constructor for ClassLoader.
Definition: class_loader.cpp:65
class_loader::ClassLoader::isLibraryLoaded
CLASS_LOADER_PUBLIC bool isLibraryLoaded()
Indicates if a library is loaded within the scope of this ClassLoader. Note that the library may alre...
Definition: class_loader.cpp:88


class_loader
Author(s): Mirza Shah, Steven! Ragnarök
autogenerated on Fri Jan 12 2024 04:01:32