Module: sems Branch: 1.5 Commit: 940c2b8d5836126cdfb7f7415065b16ea18f9890 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=940c2b8d5836126cdfb7f7415065b16ea18f9890
Author: Raphael Coeffic <[email protected]> Committer: Raphael Coeffic <[email protected]> Date: Tue Nov 20 15:32:47 2012 +0100 added reference counting to plug-in interfaces. Allows to register the same plug-in factory under different purposes, but still destroying it correctly. AmPluginFactory::onUnload() had to be removed as it would not have worked due to multiple inheritance from this same class. Instead, plug-in implementors should implement atomic_ref_cnt::on_destroy (new method) to achieve the same functionality. --- apps/db_reg_agent/DBRegAgent.h | 5 +++++ core/AmApi.h | 8 ++------ core/AmPlugIn.cpp | 30 +++++++++++++++--------------- core/atomic_types.h | 5 ++++- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/apps/db_reg_agent/DBRegAgent.h b/apps/db_reg_agent/DBRegAgent.h index 191bd6c..868d999 100644 --- a/apps/db_reg_agent/DBRegAgent.h +++ b/apps/db_reg_agent/DBRegAgent.h @@ -157,6 +157,11 @@ class DBRegAgent int onLoad(); + // atomic_ref_cnt interface + void on_destroy() { + onUnload(); + } + void onUnload(); RegistrationTimer registration_scheduler; diff --git a/core/AmApi.h b/core/AmApi.h index a8faa73..63bc444 100644 --- a/core/AmApi.h +++ b/core/AmApi.h @@ -35,6 +35,7 @@ #include "AmConfigReader.h" #include "AmArg.h" #include "AmEventQueue.h" +#include "atomic_types.h" #include <stdarg.h> @@ -63,6 +64,7 @@ class AmDynInvoke * \brief Base interface for plugin factories */ class AmPluginFactory + : public virtual atomic_ref_cnt { string plugin_name; @@ -81,12 +83,6 @@ class AmPluginFactory * @return 1 on error. */ virtual int onLoad()=0; - - - /** - * Enables the plug-in to deinitialize once the server is stopped. - */ - virtual void onUnload() { }; }; /** diff --git a/core/AmPlugIn.cpp b/core/AmPlugIn.cpp index 98557fc..9a8b513 100644 --- a/core/AmPlugIn.cpp +++ b/core/AmPlugIn.cpp @@ -110,21 +110,12 @@ AmPlugIn::AmPlugIn() { } -static std::set<AmPluginFactory*> deleted_factories; -static std::set<string> deleted_factories_names; static void delete_plugin_factory(std::pair<string, AmPluginFactory*> pf) { - if ((deleted_factories.find(pf.second) == deleted_factories.end()) && - (deleted_factories_names.find(pf.first) == deleted_factories_names.end())) { - DBG("onUnload of plugin '%s'\n", pf.first.c_str()); - pf.second->onUnload(); + DBG("decreasing reference to plug-in factory: %s\n", pf.first.c_str()); + dec_ref(pf.second); - DBG("deleting plug-in factory: %s\n", pf.first.c_str()); - deleted_factories.insert(pf.second); - deleted_factories_names.insert(pf.first); - delete pf.second; - } } AmPlugIn::~AmPlugIn() @@ -570,7 +561,11 @@ int AmPlugIn::loadAppPlugIn(AmPluginFactory* f) name2app.insert(std::make_pair(sf->getName(),sf)); DBG("application '%s' loaded.\n",sf->getName().c_str()); - module_objects.insert(std::make_pair(sf->getName(),sf)); + inc_ref(sf); + if(!module_objects.insert(std::make_pair(sf->getName(),sf)).second){ + // insertion failed + dec_ref(sf); + } name2app_mut.unlock(); return 0; @@ -589,7 +584,8 @@ int AmPlugIn::loadSehPlugIn(AmPluginFactory* f) ERROR("session component '%s' already loaded !\n",sf->getName().c_str()); goto error; } - + + inc_ref(sf); name2seh.insert(std::make_pair(sf->getName(),sf)); DBG("session component '%s' loaded.\n",sf->getName().c_str()); @@ -601,7 +597,11 @@ int AmPlugIn::loadSehPlugIn(AmPluginFactory* f) int AmPlugIn::loadBasePlugIn(AmPluginFactory* f) { - name2base.insert(std::make_pair(f->getName(),f)); + inc_ref(f); + if(!name2base.insert(std::make_pair(f->getName(),f)).second){ + // insertion failed + dec_ref(f); + } return 0; } @@ -813,7 +813,7 @@ AmSessionFactory* AmPlugIn::findSessionFactory(const AmSipRequest& req, string& ERROR(comp_name "'%s' already registered !\n", param_name.c_str()); \ return false; \ } \ - \ + inc_ref(f); \ instance()->map_name.insert(std::make_pair(param_name,f)); \ DBG(comp_name " '%s' registered.\n",param_name.c_str()); \ return true; diff --git a/core/atomic_types.h b/core/atomic_types.h index d1bd84f..0fadfab 100644 --- a/core/atomic_types.h +++ b/core/atomic_types.h @@ -198,6 +198,7 @@ protected: : atomic_int() {} virtual ~atomic_ref_cnt() {} + virtual void on_destroy() {} friend void inc_ref(atomic_ref_cnt* rc); friend void dec_ref(atomic_ref_cnt* rc); @@ -212,8 +213,10 @@ inline void inc_ref(atomic_ref_cnt* rc) inline void dec_ref(atomic_ref_cnt* rc) { assert(rc); - if(rc->dec_and_test()) + if(rc->dec_and_test()){ + rc->on_destroy(); delete rc; + } } _______________________________________________ Semsdev mailing list [email protected] http://lists.iptel.org/mailman/listinfo/semsdev
