Skip to content

Commit

Permalink
Fixed a problem that PhysicalPort does not shut down normally
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Jul 15, 2020
1 parent 0171ee4 commit 67076cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/projects/modules/physical_port/physical_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ class PhysicalPort : public ov::EnableSharedFromThis<PhysicalPort>

bool Close();

void IncreaseRefCount()
{
_ref_count++;
}

void DecreaseRefCount()
{
_ref_count--;
}

int GetRefCount() const
{
return _ref_count;
}

ov::SocketState GetState() const;

ov::SocketType GetType() const
Expand Down Expand Up @@ -73,6 +88,8 @@ class PhysicalPort : public ov::EnableSharedFromThis<PhysicalPort>
volatile bool _need_to_stop;
std::thread _thread;

std::atomic<int> _ref_count { 0 };

std::vector<PhysicalPortObserver *> _observer_list;

std::shared_mutex _worker_mutex;
Expand Down
10 changes: 9 additions & 1 deletion src/projects/modules/physical_port/physical_port_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ PhysicalPortManager::~PhysicalPortManager()

std::shared_ptr<PhysicalPort> PhysicalPortManager::CreatePort(ov::SocketType type, const ov::SocketAddress &address)
{
auto lock_guard = std::lock_guard(_port_list_mutex);

auto key = std::make_pair(type, address);
auto item = _port_list.find(key);
std::shared_ptr<PhysicalPort> port = nullptr;
Expand All @@ -42,11 +44,15 @@ std::shared_ptr<PhysicalPort> PhysicalPortManager::CreatePort(ov::SocketType typ
port = item->second;
}

port->IncreaseRefCount();

return port;
}

bool PhysicalPortManager::DeletePort(std::shared_ptr<PhysicalPort> &port)
{
auto lock_guard = std::lock_guard(_port_list_mutex);

auto key = std::make_pair(port->GetType(), port->GetAddress());
auto item = _port_list.find(key);

Expand All @@ -56,7 +62,9 @@ bool PhysicalPortManager::DeletePort(std::shared_ptr<PhysicalPort> &port)
return false;
}

if (port.use_count() == 2)
port->DecreaseRefCount();

if(port->GetRefCount() == 0)
{
// last reference
_port_list.erase(item);
Expand Down
2 changes: 2 additions & 0 deletions src/projects/modules/physical_port/physical_port_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ class PhysicalPortManager : public ov::Singleton<PhysicalPortManager>
PhysicalPortManager();

std::map<std::pair<ov::SocketType, ov::SocketAddress>, std::shared_ptr<PhysicalPort>> _port_list;

std::mutex _port_list_mutex;
};

0 comments on commit 67076cc

Please sign in to comment.