-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
corrections, added Thread Channels, example/test
- some testing under PHPunit works best with `@runInSeparateProcess` - skip channel test under PHPunit long stall before segfault, no segfault under Windows though - update `Thread` class to properly handle multi arguments, docblock comment about cancel - these additions in reference to amphp/ext-uv#46
- Loading branch information
1 parent
6296fc4
commit d5784ce
Showing
6 changed files
with
134 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Async\Threads; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
final class TChannel | ||
{ | ||
/** @var array<resource,resource> */ | ||
protected $resource = []; | ||
|
||
public function __destruct() | ||
{ | ||
\fclose($this->resource[1]); | ||
\fclose($this->resource[0]); | ||
$this->resource = null; | ||
} | ||
|
||
public function __construct() | ||
{ | ||
$this->resource = \stream_socket_pair((\stripos(\PHP_OS, "win") === 0 ? \STREAM_PF_INET : \STREAM_PF_UNIX), | ||
\STREAM_SOCK_STREAM, | ||
\STREAM_IPPROTO_IP | ||
); | ||
} | ||
|
||
public function send(string $message) | ||
{ | ||
$result = \fwrite($this->resource[1], $message . "\n", \strlen($message . "\n")); | ||
\usleep(1); | ||
return $result; | ||
} | ||
|
||
public function recv() | ||
{ | ||
return \trim(\fgets($this->resource[0])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
include 'vendor/autoload.php'; | ||
|
||
use Async\Threads\Thread; | ||
|
||
$thread = new Thread(); | ||
[$_read, $_write] = \stream_socket_pair((\stripos(\PHP_OS, "win") === 0 ? \STREAM_PF_INET : \STREAM_PF_UNIX), | ||
\STREAM_SOCK_STREAM, | ||
\STREAM_IPPROTO_IP | ||
); | ||
|
||
$thread->create_ex(function ($write) { | ||
echo "[queue1] "; | ||
$result = \fwrite($write, "Thread 1\n"); | ||
\usleep(1); | ||
return $result; | ||
}, $_write)->then(function (int $output) { | ||
print "Thread 1 returned: " . $output . PHP_EOL; | ||
})->catch(function (\Throwable $e) { | ||
print $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
$t2 = $thread->create_ex(function ($read) { | ||
echo "[queue2] "; | ||
echo "Thread 2 Got " . \fgets($read); | ||
return 'finish'; | ||
}, $_read)->then(function (string $output) { | ||
print "Thread 2 returned: " . $output . PHP_EOL; | ||
})->catch(function (\Throwable $exception) { | ||
print $exception->getMessage() . PHP_EOL; | ||
}); | ||
|
||
$thread->join(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Async\Tests; | ||
|
||
use Async\Threads\Thread; | ||
use Async\Threads\TChannel; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ThreadTestMultiChannel extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!\ZEND_THREAD_SAFE && !\function_exists('uv_loop_new')) | ||
$this->markTestSkipped('Test skipped "uv_loop_new" and "PHP ZTS" missing. currently buggy - zend_mm_heap corrupted'); | ||
} | ||
|
||
/** | ||
* @runInSeparateProcess | ||
*/ | ||
public function testIt_can_handle_multi_channel() | ||
{ | ||
$this->markTestSkipped('buggy, long stall before segmentation fault'); | ||
|
||
$thread = new Thread(); | ||
$channel = new TChannel(); | ||
|
||
$thread->create_ex(function ($write) { | ||
return $write->send('Thread 1'); | ||
}, $channel)->then(function (int $result) { | ||
$this->assertEquals(9, $result); | ||
})->catch(function (\Throwable $e) { | ||
print $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
$t2 = $thread->create_ex(function ($read) { | ||
return $read->recv(); | ||
}, $channel)->then(function (string $result) { | ||
$this->assertEquals('Thread 1', $result); | ||
})->catch(function (\Throwable $exception) { | ||
print $exception->getMessage() . PHP_EOL; | ||
}); | ||
|
||
$thread->join(); | ||
} | ||
} |