lichao
2021-06-03 69f60d8bcc5121eb952b57277c94ad5cecb8d44a
src/bh_api.cc
@@ -1,9 +1,11 @@
#include "bh_api.h"
#include "defs.h"
#include "topic_node.h"
#include <chrono>
#include <cstdio>
#include <memory>
using namespace std::chrono_literals;
using namespace bhome_shm;
using namespace bhome_msg;
@@ -30,17 +32,33 @@
}
std::unique_ptr<TopicNode> &ProcNodePtr()
{
   // client side init here.
   static std::mutex mtx;
   std::lock_guard<std::mutex> lk(mtx);
   static std::unique_ptr<TopicNode> ptr;
   if (!ptr && GlobalInit(BHomeShm())) {
      auto InitLog = []() {
         ns_log::AddLog(BHLogDir() + "bhshmq_node_" + GetProcExe() + ".log");
      ns_log::AddLog(BHLogDir() + "bhshmq_node_" + GetProcExe() + ".log", true);
         return true;
      };
      static bool init_log = InitLog();
      ptr.reset(new TopicNode(BHomeShm()));
   static std::string shm_name;
   static std::unique_ptr<TopicNode> ptr;
   std::lock_guard<std::mutex> lk(mtx);
   if (shm_name != BHomeShmName()) {
      shm_name = BHomeShmName();
      LOG_INFO() << "using shm " << shm_name;
      ptr.reset();
      // must reset/stop node before call BHomeShm() which resets shm.
      auto &shm = BHomeShm();
      for (int i = 0; !ptr && i < 3; ++i) {
         if (GlobalInit(shm)) {
            ptr.reset(new TopicNode(shm));
         } else {
            std::this_thread::sleep_for(1s); // make sure shm init done.
         }
      }
   }
   return ptr;
}