mirror of
https://github.com/awslabs/aws-crt-php.git
synced 2026-08-18 10:07:12 +00:00
Organized modules, added indirection for FFI vs extension loading (#10)
* mv src -> ext, lib -> src to be more in line with PHP module expectations * Fixed dependency for ffi test * Fixed path in format-check to C source * CRT will now try the extension first, then FFI
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace AWS\CRT;
|
||||
|
||||
use AWS\CRT\Internal\Extension;
|
||||
use AWS\CRT\Internal\FFI;
|
||||
|
||||
use \RuntimeException;
|
||||
|
||||
/**
|
||||
* Wrapper for the interface to the CRT. There only ever needs to be one of these, but
|
||||
* additional instances won't cost anything other than their memory.
|
||||
* Creating an instance of any NativeResource will activate the CRT binding. User code
|
||||
* should only need to create one of these if they are only accessing CRT:: static functions.
|
||||
*/
|
||||
final class CRT {
|
||||
|
||||
private static $impl = null;
|
||||
private static $refcount = 0;
|
||||
|
||||
function __construct() {
|
||||
if (is_null(self::$impl)) {
|
||||
try {
|
||||
self::$impl = new Extension();
|
||||
} catch (RuntimeException $rex) {
|
||||
try {
|
||||
self::$impl = new FFI();
|
||||
} catch (RuntimeException $frex) {
|
||||
throw new RuntimeException('Unable to initialize AWS CRT via extension or FFI', -1, $frex);
|
||||
}
|
||||
}
|
||||
}
|
||||
++self::$refcount;
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
if (--self::$refcount == 0) {
|
||||
self::$impl = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return integer last error code reported within the CRT
|
||||
*/
|
||||
public static function last_error() {
|
||||
return self::$impl->aws_crt_last_error();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $error Error code from the CRT, usually delivered via callback or {@see last_error}
|
||||
* @return string Human-readable description of the provided error code
|
||||
*/
|
||||
public static function error_str($error) {
|
||||
return self::$impl->aws_crt_error_str((int) $error);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $error Error code from the CRT, usually delivered via callback or {@see last_error}
|
||||
* @return string Name/enum identifier for the provided error code
|
||||
*/
|
||||
public static function error_name($error) {
|
||||
return self::$impl->aws_crt_error_name((int) $error);
|
||||
}
|
||||
|
||||
function event_loop_group_new($num_threads) {
|
||||
return self::$impl->aws_crt_event_loop_group_new($num_threads);
|
||||
}
|
||||
|
||||
function event_loop_group_release($elg) {
|
||||
return self::$impl->aws_crt_event_loop_group_release($elg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace AWS\CRT\IO;
|
||||
|
||||
use AWS\CRT\NativeResource as NativeResource;
|
||||
|
||||
/**
|
||||
* Represents 1 or more event loops (1 per thread) for doing I/O and background tasks.
|
||||
* Typically, every application has one EventLoopGroup.
|
||||
*
|
||||
* @param array options:
|
||||
* - int num_threads - Number of worker threads in the EventLoopGroup. Defaults to 0/1 per logical core.
|
||||
*/
|
||||
final class EventLoopGroup extends NativeResource {
|
||||
|
||||
static function defaults() {
|
||||
return array(
|
||||
'num_threads' => 0,
|
||||
);
|
||||
}
|
||||
|
||||
function __construct(array $options = array()) {
|
||||
parent::__construct();
|
||||
if (count($options) == 0) {
|
||||
$options = self::defaults();
|
||||
}
|
||||
$this->acquire(self::$crt->event_loop_group_new($options['num_threads']));
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
self::$crt->event_loop_group_release($this->release());
|
||||
parent::__destruct();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace AWS\CRT\Internal;
|
||||
|
||||
use \RuntimeException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Forwards calls on to awscrt PHP extension functions
|
||||
*/
|
||||
final class Extension {
|
||||
function __construct() {
|
||||
if (!extension_loaded('awscrt')) {
|
||||
throw new RuntimeException('awscrt extension is not loaded');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards any call made on this object to the extension function of the
|
||||
* same name with the supplied arguments. Argument type hinting and checking
|
||||
* occurs at the CRT wrapper.
|
||||
*/
|
||||
function __call(string $name, $args) {
|
||||
return call_user_func_array($name, $args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace AWS\CRT\Internal;
|
||||
|
||||
use \Exception;
|
||||
use \RuntimeException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Forwards calls on to libaws-crt-ffi via FFI
|
||||
*/
|
||||
final class FFI {
|
||||
private static $ffi = null;
|
||||
private static $refcount = 0;
|
||||
|
||||
function __construct() {
|
||||
if (is_null(self::$ffi)) {
|
||||
try {
|
||||
self::$ffi = \FFI::cdef(file_get_contents(__DIR__ . "/../../../api.h"), __DIR__ . "/../../../libaws-crt-ffi.so");
|
||||
self::init();
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException('Exception while initializing CRT via FFI', 0, $e);
|
||||
}
|
||||
}
|
||||
++self::$refcount;
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
if (--self::$refcount == 0) {
|
||||
self::clean_up();
|
||||
self::$ffi = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards any call made on this object to the FFI function of the
|
||||
* same name with the supplied arguments. Argument type hinting and checking
|
||||
* occurs at the CRT wrapper.
|
||||
*/
|
||||
function __call(string $name, $args) {
|
||||
return call_user_func_array(array(self::$ffi, $name), $args);
|
||||
}
|
||||
|
||||
private static function init() {
|
||||
return self::$ffi->aws_crt_init();
|
||||
}
|
||||
|
||||
private static function clean_up() {
|
||||
return self::$ffi->aws_crt_clean_up();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace AWS\CRT;
|
||||
|
||||
use AWS\CRT\CRT as CRT;
|
||||
|
||||
/**
|
||||
* Base class for all native resources, tracks all outstanding resources
|
||||
* and provides basic leak checking
|
||||
*/
|
||||
abstract class NativeResource {
|
||||
protected static $crt = null;
|
||||
protected static $resources = [];
|
||||
protected $native = null;
|
||||
|
||||
function __construct() {
|
||||
if (is_null(self::$crt)) {
|
||||
self::$crt = new CRT();
|
||||
}
|
||||
|
||||
self::$resources[spl_object_hash($this)] = 1;
|
||||
}
|
||||
|
||||
protected function acquire($handle) {
|
||||
$this->native = $handle;
|
||||
}
|
||||
|
||||
protected function release() {
|
||||
$native = $this->native;
|
||||
$this->native = null;
|
||||
return $native;
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
// Should have been destroyed and released by derived resource
|
||||
assert($this->native == null);
|
||||
unset(self::$resources[spl_object_hash($this)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user