Skip to content

Commit

Permalink
Merge branch 'main' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Mar 8, 2023
2 parents 168fa1b + 9dde543 commit c7265a3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 54 deletions.
1 change: 1 addition & 0 deletions headers/linux_native_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// pi /usr/lib/aarch64-linux-gnu/libpthread.so
// macos /usr/lib/libpthread.dylib

typedef int ts_rsrc_id;
typedef unsigned char __u_char;
typedef unsigned short int __u_short;
typedef unsigned int __u_int;
Expand Down
1 change: 1 addition & 0 deletions headers/windows_pthreads.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define FFI_SCOPE "__threads__"
#define FFI_LIB ".\\lib\\Windows\\pthreadVC3.dll"

typedef int ts_rsrc_id;
typedef signed long int __int64;
typedef unsigned int uintptr_t;
typedef char *va_list;
Expand Down
50 changes: 25 additions & 25 deletions headers/zeWin8.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ struct _zval_struct
{
struct
{
uint8_t type;
uint8_t type_flags;
zend_uchar type;
zend_uchar type_flags;
union
{
uint16_t extra;
Expand Down Expand Up @@ -247,10 +247,10 @@ struct _zend_array
{
struct
{
uint8_t flags;
uint8_t _unused;
uint8_t nIteratorsCount;
uint8_t _unused2;
zend_uchar flags;
zend_uchar _unused;
zend_uchar nIteratorsCount;
zend_uchar _unused2;
} v;
uint32_t flags;
} u;
Expand Down Expand Up @@ -345,7 +345,7 @@ typedef int (*zend_object_cast_t)(zend_object *readobj, zval *retval, int type);
typedef int (*zend_object_count_elements_t)(zend_object *object, zend_long *count);
typedef int (*zend_object_get_closure_t)(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, zend_bool check_only);
typedef HashTable *(*zend_object_get_gc_t)(zend_object *object, zval **table, int *n);
typedef int (*zend_object_do_operation_t)(uint8_t opcode, zval *result, zval *op1, zval *op2);
typedef int (*zend_object_do_operation_t)(zend_uchar opcode, zval *result, zval *op1, zval *op2);

struct _zend_object_handlers
{
Expand Down Expand Up @@ -400,8 +400,8 @@ typedef struct _zend_arg_info
typedef struct _zend_internal_function
{
/* Common elements */
uint8_t type;
uint8_t arg_flags[3]; /* bitset of arg_info.pass_by_reference */
zend_uchar type;
zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */
uint32_t fn_flags;
zend_string *function_name;
zend_class_entry *scope;
Expand Down Expand Up @@ -447,8 +447,8 @@ typedef struct _zend_try_catch_element

struct _zend_op_array
{
uint8_t type;
uint8_t arg_flags[3];
zend_uchar type;
zend_uchar arg_flags[3];
uint32_t fn_flags;
zend_string *function_name;
zend_class_entry *scope;
Expand Down Expand Up @@ -505,8 +505,8 @@ typedef union _znode_op

typedef struct _znode
{ /* used only during compilation */
uint8_t op_type;
uint8_t flag;
zend_uchar op_type;
zend_uchar flag;
union
{
znode_op op;
Expand All @@ -522,21 +522,21 @@ struct _zend_op
znode_op result;
uint32_t extended_value;
uint32_t lineno;
uint8_t opcode;
uint8_t op1_type;
uint8_t op2_type;
uint8_t result_type;
zend_uchar opcode;
zend_uchar op1_type;
zend_uchar op2_type;
zend_uchar result_type;
};

union _zend_function
{
uint8_t type; /* MUST be the first element of this struct! */
zend_uchar type; /* MUST be the first element of this struct! */
uint32_t quick_arg_flags;

struct
{
uint8_t type; /* never used */
uint8_t arg_flags[3]; /* bitset of arg_info.pass_by_reference */
zend_uchar type; /* never used */
zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */
uint32_t fn_flags;
zend_string *function_name;
zend_class_entry *scope;
Expand Down Expand Up @@ -1038,7 +1038,7 @@ struct _zend_compiler_globals
HashTable *auto_globals;

/* Refer to zend_yytnamerr() in zend_language_parser.y for meaning of values */
uint8_t parse_error;
zend_uchar parse_error;
zend_bool in_compilation;
zend_bool short_tags;

Expand Down Expand Up @@ -1222,7 +1222,7 @@ struct _zend_executor_globals
zend_op exception_op[3];
struct _zend_module_entry *current_module;
zend_bool active;
uint8_t flags;
zend_uchar flags;
zend_long assertions;
uint32_t ht_iterators_count;
uint32_t ht_iterators_used;
Expand Down Expand Up @@ -1260,15 +1260,15 @@ void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, i
void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type);
void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type, int resource_type2);

int zend_set_user_opcode_handler(uint8_t opcode, user_opcode_handler_t handler);
user_opcode_handler_t zend_get_user_opcode_handler(uint8_t opcode);
int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler);
user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode);

void zval_ptr_dtor(zval *zval_ptr);
void zval_internal_ptr_dtor(zval *zvalue);
void zval_add_ref(zval *p);
zval *zend_get_zval_ptr(const zend_op *opline, int op_type, const znode_op *node, const zend_execute_data *execute_data);

uint8_t zend_get_call_op(const zend_op *init_op, zend_function *fbc);
zend_uchar zend_get_call_op(const zend_op *init_op, zend_function *fbc);
void object_init(zval *arg);
zend_result object_init_ex(zval *arg, zend_class_entry *ce);

Expand Down
4 changes: 2 additions & 2 deletions test_thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function (\FFI\CData $memory): void {

// example from http://codingbison.com/c/c-pthreads-basics.html

define('MAX_THREADS', 2);
define('MAX_THREADS', 3);

function main()
{
Expand All @@ -42,7 +42,7 @@ function main()
];

$t = c_array_type('pthread_t', 'ts', MAX_THREADS);
$index = c_array_type('int', 'ze', MAX_THREADS);
$index = c_array_type('int', 'ts', MAX_THREADS);
$status = 0;
$arrLen = 0;
$i = 0;
Expand Down
8 changes: 5 additions & 3 deletions zend/StandardModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,16 @@ final public function ffi(): \FFI
* - Set the debug mode for this module.
* @param int $target_version `ZEND_MODULE_API_NO`
* - Set the API version for this module
* @param boolean $registerAndStartup
*
* @return self
*/
final public function __construct(
bool $restart_sapi = null,
bool $target_threads = \ZEND_THREAD_SAFE,
bool $target_debug = \ZEND_DEBUG_BUILD,
int $target_version = \ZEND_MODULE_API_NO
int $target_version = \ZEND_MODULE_API_NO,
bool $registerAndStartup = true
) {
if (!isset($this->ffi_tag))
return \ze_ffi()->zend_error(\E_ERROR, 'No `FFI` instance found!');
Expand All @@ -322,7 +324,7 @@ final public function __construct(
$ptr = $ext->ptr();
$this->update(\ze_ffi()->cast('zend_module_entry*', $ptr));
$this->addReflection($ptr->name);
} else {
} elseif ($registerAndStartup) {
$this->register();
$this->startup();
}
Expand Down Expand Up @@ -457,7 +459,7 @@ final public function register(): void
if (\PHP_ZTS) {
\tsrmls_activate();
$id = \ze_ffi()->tsrm_thread_id();
$this->global_rsrc[$id] = \ze_ffi()->new('ts_rsrc_id', false, $this->target_persistent);
$this->global_rsrc[$id] = $this->ffi()->new('ts_rsrc_id', false, $this->target_persistent);
$this->ze_other->globals_id_ptr = \FFI::addr($this->global_rsrc[$id]);
$this->ze_other->globals_size = \FFI::sizeof($this->ffi()->type($globalType));
$this->global_id[$id] = \ze_ffi()->ts_allocate_id(
Expand Down
7 changes: 2 additions & 5 deletions zend/Threading.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

declare(strict_types=1);

use FFI\CData;
use ZE\PThread;
use ZE\Thread;

Expand All @@ -22,7 +21,7 @@ function threads_customize(
): \ThreadsModule {
$module = \threads_get_module();
if (\is_null($module)) {
$module = new \ThreadsModule();
$module = new \ThreadsModule(null, \ZEND_THREAD_SAFE, \ZEND_DEBUG_BUILD, \ZEND_MODULE_API_NO, false);
$module->set_lifecycle(
$module_startup,
$module_shutdown,
Expand All @@ -39,7 +38,7 @@ function threads_customize(

return \ze_ffi()->zend_error(
\E_WARNING,
'Thread customization not possible, registration has already finish!'
'Thread customization not possible, registration has already finished!'
);
}

Expand All @@ -48,8 +47,6 @@ function threads_activate(): \ThreadsModule
$module = \threads_get_module();
if (\is_null($module)) {
$module = new \ThreadsModule();
$module->register();
$module->startup();
}

return $module;
Expand Down
73 changes: 54 additions & 19 deletions zend/ThreadsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class ThreadsModule extends \StandardModule

final public function thread_startup($runtime) //: Thread
{
//\ze_ffi()->tsrm_mutex_lock($this->module_mutex);
\ze_ffi()->tsrm_mutex_lock($this->module_mutex);

\ze_ffi()->ts_resource_ex(0, null);

Expand Down Expand Up @@ -92,12 +92,6 @@ final public function thread_shutdown()
/* Flush all output buffers */
\ze_ffi()->php_output_end_all();

// TODO: store the list of modules to reload in a global module variable
foreach (self::MODULES_TO_RELOAD as $module_name) {
$module = \zend_hash_str_find_ptr($module_name);
($module->request_shutdown_func)($module->type, $module->module_number);
}

/* Shutdown output layer (send the set HTTP headers, cleanup output handlers, etc.) */
\ze_ffi()->php_output_deactivate();

Expand Down Expand Up @@ -193,6 +187,35 @@ public function set_lifecycle(

public function startup(): void
{
$module = $this->ze_other_ptr;
\ze_ffi()->php_output_end_all();
\ze_ffi()->php_output_deactivate();
\ze_ffi()->php_output_shutdown();

\ze_ffi()->sapi_flush();
\ze_ffi()->sapi_deactivate();
\ze_ffi()->sapi_shutdown();

if ($this->r_startup) {
\ze_ffi()->sapi_module->activate = function (...$args) use ($module) {
$result = ($module->request_startup_func)($module->type, $module->module_number);
$sapi_result = !\is_null($this->original_sapi_activate) ? ($this->original_sapi_activate)(...$args) : \ZE::SUCCESS;

return ($result == $sapi_result && $result === \ZE::SUCCESS)
? \ZE::SUCCESS : \ZE::FAILURE;
};
}

if ($this->r_shutdown) {
\ze_ffi()->sapi_module->deactivate = function (...$args) use ($module) {
$result = ($module->request_shutdown_func)($module->type, $module->module_number);
$sapi_result = !\is_null($this->original_sapi_deactivate) ? ($this->original_sapi_deactivate)(...$args) : \ZE::SUCCESS;

return ($result == $sapi_result && $result === \ZE::SUCCESS)
? \ZE::SUCCESS : \ZE::FAILURE;
};
}

if (\is_null($this->output_mutex)) {
try {
$this->output_mutex = \ffi_ptr($this->get_globals('mutex'));
Expand All @@ -215,28 +238,46 @@ public function startup(): void
return $result;
};

parent::startup();
if (\ze_ffi()->zend_startup_module_ex($module) !== \ZE::SUCCESS) {
throw new \RuntimeException('Can not startup module ' . $this->module_name);
}

if ($this->r_shutdown)
\register_shutdown_function(
\closure_from($this, 'module_destructor')
);

\ze_ffi()->php_output_activate();

$result = \IS_PHP82
? \ze_ffi()->php_module_startup(\FFI::addr(\ze_ffi()->sapi_module), null)
: \ze_ffi()->php_module_startup(\FFI::addr(\ze_ffi()->sapi_module), null, 0);
if ($result !== \ZE::SUCCESS) {
throw new \RuntimeException(
'Can not restart SAPI module ' . \ffi_string(\ze_ffi()->sapi_module->name)
);
}
}

public function module_startup(int $type, int $module_number): int
{
$this->original_interrupt_handler = \ze_ffi()->zend_interrupt_function;
\ze_ffi()->zend_interrupt_function = \closure_from($this, 'thread_interrupt');
// $this->original_interrupt_handler = \ze_ffi()->zend_interrupt_function;
// \ze_ffi()->zend_interrupt_function = \closure_from($this, 'thread_interrupt');
return !\is_null($this->m_init)
? ($this->m_init)($type, $module_number) : \ZE::SUCCESS;
}

public function module_shutdown(int $type, int $module_number): int
{
\ze_ffi()->zend_interrupt_function = $this->original_interrupt_handler;
$this->original_interrupt_handler = null;
// \ze_ffi()->zend_interrupt_function = $this->original_interrupt_handler;
// $this->original_interrupt_handler = null;
return !\is_null($this->m_end)
? ($this->m_end)($type, $module_number) : \ZE::SUCCESS;
}

public function request_startup(int $type, int $module_number): int
{
$this->thread_startup($type, $module_number);
// $this->thread_startup($type, $module_number);
return !\is_null($this->r_init)
? ($this->r_init)($type, $module_number) : \ZE::SUCCESS;
}
Expand All @@ -250,19 +291,13 @@ public function request_shutdown(int $type, int $module_number): int

public function global_startup(CData $memory): void
{
parent::global_startup($memory);
if (!\is_null($this->g_init)) ($this->g_init)($memory);
}

public function global_shutdown(CData $memory): void
{
if (!\is_null($this->g_end)) ($this->g_end)($memory);

$this->__destruct();
}

public function __destruct()
{
if (!$this->target_persistent) {
if (\is_ze_ffi()) {
\ze_ffi()->sapi_module->ub_write = $this->original_sapi_output;
Expand Down

0 comments on commit c7265a3

Please sign in to comment.