Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:43

0001 #include <map>
0002 #include <mutex>
0003 
0004 #include "GeneratorInterface/Herwig7Interface/interface/Proxy.h"
0005 
0006 using namespace ThePEG;
0007 
0008 static std::mutex mutex;
0009 
0010 typedef std::map<ProxyBase::ProxyID, std::weak_ptr<ProxyBase> > 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 ProxyBase::ProxyBase(ProxyID id) : id(id) {}
0027 
0028 ProxyBase::~ProxyBase() {
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<ProxyBase> ProxyBase::create(ctor_t ctor) {
0037   static ProxyBase::ProxyID nextProxyID = 0;
0038 
0039   std::scoped_lock scoped_lock(mutex);
0040 
0041   std::shared_ptr<ProxyBase> proxy(ctor(++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<ProxyBase> ProxyBase::find(ProxyID id) {
0051   std::scoped_lock scoped_lock(mutex);
0052 
0053   ProxyMap *map = getProxyMapInstance();
0054   if (!map)
0055     return std::shared_ptr<ProxyBase>();
0056 
0057   ProxyMap::const_iterator pos = map->find(id);
0058   if (pos == map->end())
0059     return std::shared_ptr<ProxyBase>();
0060 
0061   return std::shared_ptr<ProxyBase>(pos->second);
0062 }