Skip to content

Commit

Permalink
Add ref/unref to sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Sep 15, 2015
1 parent 63f4f2e commit c88aeeb
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/Loop/Manager/Uv/SocketManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class SocketManager implements SocketManagerInterface
*/
private $sockets = [];

/**
* @var \Icicle\Loop\Events\SocketEventInterface[]
*/
private $unreferenced = [];

/**
* @var callable Callback for poll events.
*/
Expand Down Expand Up @@ -133,8 +138,8 @@ public function __destruct()
*/
public function isEmpty(): bool
{
foreach ($this->polls as $poll) {
if (\uv_is_active($poll)) {
foreach ($this->polls as $id =>$poll) {
if (\uv_is_active($poll) && !isset($this->unreferenced[$id])) {
return false;
}
}
Expand Down Expand Up @@ -231,7 +236,7 @@ public function free(SocketEventInterface $socket)
$id = (int) $socket->getResource();

if (isset($this->sockets[$id]) && $socket === $this->sockets[$id]) {
unset($this->sockets[$id]);
unset($this->sockets[$id], $this->unreferenced[$id]);

if (isset($this->polls[$id])) {
\uv_close($this->polls[$id]);
Expand All @@ -258,6 +263,26 @@ public function isFreed(SocketEventInterface $socket): bool
return !isset($this->sockets[$id]) || $socket !== $this->sockets[$id];
}

/**
* {@inheritdoc}
*/
public function reference(SocketEventInterface $socket)
{
unset($this->unreferenced[(int) $socket->getResource()]);
}

/**
* {@inheritdoc}
*/
public function unreference(SocketEventInterface $socket)
{
$id = (int) $socket->getResource();

if (isset($this->sockets[$id]) && $socket === $this->sockets[$id]) {
$this->unreferenced[$id] = $socket;
}
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit c88aeeb

Please sign in to comment.