Author: sayer
Date: 2009-10-22 01:13:22 +0200 (Thu, 22 Oct 2009)
New Revision: 1552
Modified:
trunk/core/AmPlugIn.cpp
trunk/core/AmPlugIn.h
Log:
thread-safe application name registering and mapping (name2app)
Modified: trunk/core/AmPlugIn.cpp
===================================================================
--- trunk/core/AmPlugIn.cpp 2009-10-21 22:35:27 UTC (rev 1551)
+++ trunk/core/AmPlugIn.cpp 2009-10-21 23:13:22 UTC (rev 1552)
@@ -256,12 +256,16 @@
register_logging_fac(it->second);
}
+ name2app_mut.lock();
for(std::map<std::string,AmSessionFactory*>::iterator it = name2app.begin();
it != name2app.end(); it++){
err = it->second->onLoad();
- if(err)
+ if(err) {
+ name2app_mut.unlock();
return err;
+ }
}
+ name2app_mut.unlock();
DBG("AmPlugIn: Initialized plugins.\n");
@@ -435,10 +439,15 @@
AmSessionFactory* AmPlugIn::getFactory4App(const string& app_name)
{
+ AmSessionFactory* res = NULL;
+
+ name2app_mut.lock();
std::map<std::string,AmSessionFactory*>::iterator it =
name2app.find(app_name);
- if(it != name2app.end())
- return it->second;
- return 0;
+ if(it != name2app.end())
+ res = it->second;
+ name2app_mut.unlock();
+
+ return res;
}
AmSessionEventHandlerFactory* AmPlugIn::getFactory4Seh(const string& name)
@@ -520,21 +529,24 @@
AmSessionFactory* sf = dynamic_cast<AmSessionFactory*>(f);
if(!sf){
ERROR("invalid application plug-in!\n");
- goto error;
+ return -1;
}
+ name2app_mut.lock();
+
if(name2app.find(sf->getName()) != name2app.end()){
ERROR("application '%s' already loaded !\n",sf->getName().c_str());
- goto error;
- }
-
+ name2app_mut.unlock();
+ return -1;
+ }
+
name2app.insert(std::make_pair(sf->getName(),sf));
DBG("application '%s' loaded.\n",sf->getName().c_str());
+ name2app_mut.unlock();
+
return 0;
- error:
- return -1;
}
int AmPlugIn::loadSehPlugIn(AmPluginFactory* f)
@@ -748,15 +760,21 @@
bool AmPlugIn::registerFactory4App(const string& app_name, AmSessionFactory* f)
{
+ bool res;
+
+ name2app_mut.lock();
std::map<std::string,AmSessionFactory*>::iterator it =
name2app.find(app_name);
if(it != name2app.end()){
WARN("Application '%s' has already been registered and cannot be
registered a second time\n",
app_name.c_str());
- return false;
+ res = false;
+ } else {
+ name2app.insert(make_pair(app_name,f));
+ res = true;
}
-
- name2app.insert(make_pair(app_name,f));
- return true;
+ name2app_mut.unlock();
+
+ return res;
}
AmSessionFactory* AmPlugIn::findSessionFactory(AmSipRequest& req)
Modified: trunk/core/AmPlugIn.h
===================================================================
--- trunk/core/AmPlugIn.h 2009-10-21 22:35:27 UTC (rev 1551)
+++ trunk/core/AmPlugIn.h 2009-10-21 23:13:22 UTC (rev 1552)
@@ -29,6 +29,8 @@
#ifndef _AmPlugIn_h_
#define _AmPlugIn_h_
+#include "AmThread.h"
+
#include <string>
#include <map>
#include <vector>
@@ -94,6 +96,8 @@
std::map<string,amci_inoutfmt_t*> file_formats;
std::map<string,AmSessionFactory*> name2app;
+ AmMutex name2app_mut;
+
std::map<string,AmSessionEventHandlerFactory*> name2seh;
std::map<string,AmPluginFactory*> name2base;
std::map<string,AmDynInvokeFactory*> name2di;
_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev