Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:07:20

0001 #include <map>
0002 #include <mutex>
0003 
0004 #include "GeneratorInterface/LHEInterface/interface/LHEProxy.h"
0005 
0006 using namespace lhef;
0007 
0008 static std::mutex mutex;
0009 
0010 typedef std::map<LHEProxy::ProxyID, std::weak_ptr<LHEProxy> > ProxyMap;
0011 
0012 static ProxyMap *getProxyMapInstance() {
0013   static struct Sentinel {
0014     Sentinel() : instance(new ProxyMap) {}
0015     ~Sentinel() {
0016       delete instance;
0017       instance = nullptr;
0018     }
0019 
0020     ProxyMap *instance;
0021   } sentinel;
0022 
0023   return sentinel.instance;
0024 }
0025 
0026 LHEProxy::LHEProxy(ProxyID id) : id(id) {}
0027 
0028 LHEProxy::~LHEProxy() {
0029   std::scoped_lock scoped_lock(mutex);
0030 
0031   ProxyMap *map = getProxyMapInstance();
0032   if (map)
0033     map->erase(id);
0034 }
0035 
0036 std::shared_ptr<LHEProxy> LHEProxy::create() {
0037   static LHEProxy::ProxyID nextProxyID = 0;
0038 
0039   std::scoped_lock scoped_lock(mutex);
0040 
0041   std::shared_ptr<LHEProxy> proxy(new LHEProxy(++nextProxyID));
0042 
0043   ProxyMap *map = getProxyMapInstance();
0044   if (map)
0045     map->insert(ProxyMap::value_type(proxy->getID(), proxy));
0046 
0047   return proxy;
0048 }
0049 
0050 std::shared_ptr<LHEProxy> LHEProxy::find(ProxyID id) {
0051   std::scoped_lock scoped_lock(mutex);
0052 
0053   ProxyMap *map = getProxyMapInstance();
0054   if (!map)
0055     return std::shared_ptr<LHEProxy>();
0056 
0057   ProxyMap::const_iterator pos = map->find(id);
0058   if (pos == map->end())
0059     return std::shared_ptr<LHEProxy>();
0060 
0061   return std::shared_ptr<LHEProxy>(pos->second);
0062 }