class_loader.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "class_loader/class_loader.hpp"
00031 
00032 #include <string>
00033 
00034 namespace class_loader
00035 {
00036 
00037 bool ClassLoader::has_unmananged_instance_been_created_ = false;
00038 
00039 bool ClassLoader::hasUnmanagedInstanceBeenCreated()
00040 {
00041   return ClassLoader::has_unmananged_instance_been_created_;
00042 }
00043 
00044 std::string systemLibrarySuffix()
00045 {
00046   return Poco::SharedLibrary::suffix();
00047 }
00048 
00049 ClassLoader::ClassLoader(const std::string & library_path, bool ondemand_load_unload)
00050 : ondemand_load_unload_(ondemand_load_unload),
00051   library_path_(library_path),
00052   load_ref_count_(0),
00053   plugin_ref_count_(0)
00054 {
00055   CONSOLE_BRIDGE_logDebug(
00056     "class_loader.ClassLoader: "
00057     "Constructing new ClassLoader (%p) bound to library %s.",
00058     this, library_path.c_str());
00059   if (!isOnDemandLoadUnloadEnabled()) {
00060     loadLibrary();
00061   }
00062 }
00063 
00064 ClassLoader::~ClassLoader()
00065 {
00066   CONSOLE_BRIDGE_logDebug("%s",
00067     "class_loader.ClassLoader: "
00068     "Destroying class loader, unloading associated library...\n");
00069   unloadLibrary();  // TODO(mikaelarguedas): while(unloadLibrary() > 0){} ??
00070 }
00071 
00072 bool ClassLoader::isLibraryLoaded()
00073 {
00074   return class_loader::class_loader_private::isLibraryLoaded(getLibraryPath(), this);
00075 }
00076 
00077 bool ClassLoader::isLibraryLoadedByAnyClassloader()
00078 {
00079   return class_loader::class_loader_private::isLibraryLoadedByAnybody(getLibraryPath());
00080 }
00081 
00082 void ClassLoader::loadLibrary()
00083 {
00084   boost::recursive_mutex::scoped_lock lock(load_ref_count_mutex_);
00085   load_ref_count_ = load_ref_count_ + 1;
00086   class_loader::class_loader_private::loadLibrary(getLibraryPath(), this);
00087 }
00088 
00089 int ClassLoader::unloadLibrary()
00090 {
00091   return unloadLibraryInternal(true);
00092 }
00093 
00094 int ClassLoader::unloadLibraryInternal(bool lock_plugin_ref_count)
00095 {
00096   boost::recursive_mutex::scoped_lock load_ref_lock(load_ref_count_mutex_);
00097   boost::recursive_mutex::scoped_lock plugin_ref_lock;
00098   if (lock_plugin_ref_count) {
00099     plugin_ref_lock = boost::recursive_mutex::scoped_lock(plugin_ref_count_mutex_);
00100   }
00101 
00102   if (plugin_ref_count_ > 0) {
00103     CONSOLE_BRIDGE_logWarn("%s",
00104       "class_loader.ClassLoader: "
00105       "SEVERE WARNING!!! Attempting to unload library while objects created by this loader "
00106       "exist in the heap! "
00107       "You should delete your objects before attempting to unload the library or "
00108       "destroying the ClassLoader. The library will NOT be unloaded.");
00109   } else {
00110     load_ref_count_ = load_ref_count_ - 1;
00111     if (0 == load_ref_count_) {
00112       class_loader::class_loader_private::unloadLibrary(getLibraryPath(), this);
00113     } else if (load_ref_count_ < 0) {
00114       load_ref_count_ = 0;
00115     }
00116   }
00117   return load_ref_count_;
00118 }
00119 
00120 }  // namespace class_loader


class_loader
Author(s): Mirza Shah
autogenerated on Thu Jun 6 2019 20:43:27