lichao
2021-06-01 365c864a587365fe443b11cc0cd7cfc8f8f8eb81
src/shm_msg_queue.cpp
@@ -53,22 +53,6 @@
ShmMsgQueue::~ShmMsgQueue() {}
#ifndef BH_USE_ATOMIC_Q
ShmMsgQueue::Mutex &ShmMsgQueue::GetMutex(const MQId id)
{
   static std::unordered_map<MQId, std::shared_ptr<Mutex>> imm;
   static std::mutex mtx;
   std::lock_guard<std::mutex> lock(mtx);
   auto pos = imm.find(id);
   if (pos == imm.end()) {
      pos = imm.emplace(id, new Mutex(id)).first;
      // pos = imm.emplace(id, new Mutex()).first;
   }
   return *pos->second;
}
#endif
bool ShmMsgQueue::Remove(SharedMemory &shm, const MQId id)
{
   Queue *q = Find(shm, id);
@@ -95,17 +79,9 @@
   try {
      //TODO find from center, or use offset.
      ShmMsgQueue dest(remote.offset_, shm, remote.id_);
#ifndef BH_USE_ATOMIC_Q
      Guard lock(GetMutex(remote_id));
#endif
      return dest.queue().TryWrite(val);
   } catch (...) {
      // SetLastError(eNotFound, "remote not found");
      return false;
   }
}
// Test shows that in the 2 cases:
// 1) build msg first, then find remote queue;
// 2) find remote queue first, then build msg;
// 1 is about 50% faster than 2, maybe cache related.