static const char RPC_PORT_SIGNAL_DEBUG[] = "Debug";
static const char RPC_PORT_SIGNAL_NEW[] = "New";
+std::atomic<DebugPort*> DebugPort::inst_;
+std::mutex DebugPort::mutex_;
+
DebugPort::~DebugPort() {
if (!disposed_)
Dispose();
}
+DebugPort* DebugPort::GetInst() {
+ DebugPort* inst = inst_.load(std::memory_order_acquire);
+ if (inst == nullptr) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ inst = inst_.load(std::memory_order_relaxed);
+ if (inst == nullptr) {
+ inst = new DebugPort();
+ inst_.store(inst, std::memory_order_release);
+ }
+ }
+
+ std::lock_guard<std::recursive_mutex> rec_lock(inst->GetMutex());
+ if (inst->disposed_)
+ inst->Init();
+
+ return inst;
+}
+
void DebugPort::Dispose() {
Unwatch();
Unsubscribe();
int delegate_port_;
};
- static DebugPort& GetInst() {
- static DebugPort inst;
-
- std::lock_guard<std::recursive_mutex> lock(inst.GetMutex());
- if (inst.disposed_)
- inst.Init();
- return inst;
- }
+ static DebugPort* GetInst();
void Dispose();
bool IsConnected();
const void* buf, unsigned int size);
private:
+ static std::atomic<DebugPort*> inst_;
+ static std::mutex mutex_;
+
std::recursive_mutex& GetMutex() const {
- return mutex_;
+ return rec_mutex_;
}
void Init();
std::thread thread_;
std::atomic<bool> is_running_;
SharedQueue<std::shared_ptr<Parcel>> queue_;
- mutable std::recursive_mutex mutex_;
+ mutable std::recursive_mutex rec_mutex_;
};
} // namespace internal
proxy->main_port_.reset();
proxy->delegate_port_.reset();
}
- DebugPort::GetInst().RemoveSession(fd);
+ DebugPort::GetInst()->RemoveSession(fd);
return FALSE;
}
listener->OnDisconnected(proxy->target_appid_);
if (proxy->main_port_.get() != nullptr) {
- DebugPort::GetInst().RemoveSession(proxy->main_port_->GetFd());
+ DebugPort::GetInst()->RemoveSession(proxy->main_port_->GetFd());
proxy->main_port_.reset();
}
proxy->delegate_port_.reset();
main_port_.reset(new ProxyPort(this, fds_[0], target_appid_, false));
delegate_port_.reset(new ProxyPort(this, fds_[1], target_appid_));
listener_->OnConnected(target_appid_, main_port_.get());
- DebugPort::GetInst().AddSession(port_name_, target_appid_, fds_[0], fds_[1]);
+ DebugPort::GetInst()->AddSession(port_name_, target_appid_, fds_[0], fds_[1]);
}
// LCOV_EXCL_START
IEventListener* listener = listener_;
listener_ = nullptr;
listener->OnDisconnected(target_appid_);
- DebugPort::GetInst().RemoveSession(fds_[0]);
+ DebugPort::GetInst()->RemoveSession(fds_[0]);
}
// LCOV_EXCL_STOP
main_port_.reset(new ProxyPort(this, fds_[0], target_appid_, false));
delegate_port_.reset(new ProxyPort(this, fds_[1], target_appid_));
listener_->OnConnected(target_appid_, main_port_.get());
- DebugPort::GetInst().AddSession(port_name, target_appid_, fds_[0], fds_[1]);
-
+ DebugPort::GetInst()->AddSession(port_name, target_appid_, fds_[0], fds_[1]);
return RPC_PORT_ERROR_NONE;
}
void Proxy::DisconnectPort() {
if (main_port_.get() != nullptr) {
- DebugPort::GetInst().RemoveSession(main_port_->GetFd());
+ DebugPort::GetInst()->RemoveSession(main_port_->GetFd());
main_port_.reset();
}
}
if (ret < 0)
return ret;
- auto& debug_port = DebugPort::GetInst();
- debug_port.Send(port->GetFd(), true, seq, buf, size);
+ auto* debug_port = DebugPort::GetInst();
+ debug_port->Send(port->GetFd(), true, seq, buf, size);
return RPC_PORT_ERROR_NONE;
}
if (ret < 0)
return ret;
- auto& debug_port = DebugPort::GetInst();
- debug_port.Send(port->GetFd(), false, seq, buf, size);
+ auto* debug_port = DebugPort::GetInst();
+ debug_port->Send(port->GetFd(), false, seq, buf, size);
return RPC_PORT_ERROR_NONE;
}
while (iter != ports_.end()) {
if ((*iter)->GetInstance().compare(instance) == 0) {
LOGI("Close: fd(%d)", (*iter)->GetFd());
- DebugPort::GetInst().RemoveSession((*iter)->GetFd());
+ DebugPort::GetInst()->RemoveSession((*iter)->GetFd());
iter = ports_.erase(iter);
} else {
iter++;
break;
}
}
- DebugPort::GetInst().AddSession(port_name_, sender, fds[0], fds[1]);
+
+ _W("sender_appid(%s), instance(%s), main_fd(%d), delegate_fd(%d)",
+ sender.c_str(), main_port->GetInstance().c_str(), fds[0], fds[1]);
+ listener_->OnConnected(sender, main_port->GetInstance());
+ DebugPort::GetInst()->AddSession(port_name_, sender, fds[0], fds[1]);
}
Stub::AcceptedPort::AcceptedPort(Stub* parent, bool isDelegate, int fd,