Skip to content

Commit

Permalink
corrections for threading, not working worsted!
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Mar 8, 2023
1 parent 2d45305 commit 9dde543
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 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
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
2 changes: 1 addition & 1 deletion zend/StandardModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,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
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 9dde543

Please sign in to comment.