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 10, 2023
2 parents 371d436 + b8384b7 commit 793e808
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 225 deletions.
13 changes: 13 additions & 0 deletions headers/stubs/ze_ffi.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ interface rsrc_dtor_func_t extends closure
interface user_opcode_handler_t extends closure
{
}
/** @var callable (zval *pElement) */
interface copy_ctor_func_t extends closure
{
}
/** @var callable (LPVOID lpParameter) */
interface ThreadProc extends closure
{
Expand Down Expand Up @@ -450,6 +454,15 @@ public function zend_hash_add_or_update(HashTable &$ht, zend_string &$key, zval
/** @return zval */
public function zend_hash_next_index_insert(HashTable &$ht, zval &$pData);

/** @return void */
public function zend_hash_copy(HashTable &$target, HashTable &$source, copy_ctor_func_t $pCopyConstructor);

/** @return void */
public function zend_hash_destroy(HashTable &$ht);

/** @return void */
public function zend_hash_clean(HashTable &$ht);

/** @return zend_function */
public function zend_fetch_function(zend_string &$name);

Expand Down
6 changes: 0 additions & 6 deletions headers/ze81ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1483,12 +1483,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
6 changes: 0 additions & 6 deletions headers/ze82ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1502,12 +1502,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
6 changes: 0 additions & 6 deletions headers/ze83ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1562,12 +1562,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
6 changes: 0 additions & 6 deletions headers/ze8ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1304,12 +1304,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
6 changes: 0 additions & 6 deletions headers/zeWin81ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1599,12 +1599,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
6 changes: 0 additions & 6 deletions headers/zeWin82ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1618,12 +1618,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
6 changes: 0 additions & 6 deletions headers/zeWin83ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1680,12 +1680,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
6 changes: 0 additions & 6 deletions headers/zeWin8ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1420,12 +1420,6 @@ void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begi
void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

/* these 3 APIs should only be used by people that fully understand the threading model
* used by PHP/Zend and the selected SAPI. */
void *tsrm_new_interpreter_context(void);
void *tsrm_set_interpreter_context(void *new_ctx);
void tsrm_free_interpreter_context(void *context);

void *tsrm_get_ls_cache(void);
uint8_t tsrm_is_main_thread(void);
uint8_t tsrm_is_shutdown(void);
Expand Down
46 changes: 1 addition & 45 deletions preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,54 +780,10 @@ function zend_preloader(): void
}

\setup_ffi_loader('ts', $header);
\tsrmls_cache_define();
}
}
}

if (\PHP_ZTS)
\tsrmls_cache_define();
}

function tsrmls_cache_define()
{
if (\PHP_ZTS) {
global $_tsrm_ls_cache;
$_tsrm_ls_cache = null;
}
}

function tsrmls_cache_update()
{
if (\PHP_ZTS) {
global $_tsrm_ls_cache;
$_tsrm_ls_cache = \ze_ffi()->tsrm_get_ls_cache();
}
}

function tsrmls_cache(): ?CData
{
if (\PHP_ZTS) {
global $_tsrm_ls_cache;
return $_tsrm_ls_cache;
}

return null;
}

function tsrmls_activate()
{
if (\PHP_ZTS) {
\ze_ffi()->ts_resource_ex(0, null);
\tsrmls_cache_update();
}
}

function tsrmls_deactivate()
{
if (\PHP_ZTS) {
\ze_ffi()->ts_free_id(0);
\tsrmls_cache_define();
}
}

\ze_init();
Expand Down
4 changes: 2 additions & 2 deletions test_thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ function main()
$index = c_array_type('int', 'ts', MAX_THREADS);
$status = 0;
$arrLen = 0;
$i = 0;

$arrLen = ffi_sizeof($arrPaintings);
$arrLen = ffi_sizeof($arrPaintings) * \count($arrPaintings);

srand(time()); /* initialize random seed */
for ($i = 0; $i < MAX_THREADS; $i++) {
Expand All @@ -59,6 +58,7 @@ function main()
$t->addr_of($i),
null,
function (CData $arg) {
echo '---> ';
$arrPaintings = [
"The Last Supper", "Mona Lisa", "Potato Eaters",
"Cypresses", "Starry Night", "Water Lilies"
Expand Down
164 changes: 164 additions & 0 deletions zend/Threading.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

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

Expand Down Expand Up @@ -71,4 +72,167 @@ function pthread_init(): PThread

return new PThread($module);
}

function tsrmls_cache_define(): void
{
if (\PHP_ZTS) {
global $_tsrm_ls_cache;
$_tsrm_ls_cache[\ze_ffi()->tsrm_thread_id()] = null;
}
}

function tsrmls_cache_update(): void
{
if (\PHP_ZTS) {
global $_tsrm_ls_cache;
$_tsrm_ls_cache[\ze_ffi()->tsrm_thread_id()] = \ze_ffi()->tsrm_get_ls_cache();
}
}

function tsrmls_cache(): ?CData
{
if (\PHP_ZTS) {
global $_tsrm_ls_cache;
return $_tsrm_ls_cache[\ze_ffi()->tsrm_thread_id()];
}

return null;
}

function tsrmls_activate(): void
{
if (\PHP_ZTS) {
\ze_ffi()->ts_resource_ex(0, null);
\tsrmls_cache_update();
}
}

function tsrmls_deactivate(): void
{
if (\PHP_ZTS) {
\ze_ffi()->ts_free_id(0);
\tsrmls_cache_define();
}
}
/*
static void php_thread_executor_globals_reinit(zend_executor_globals *dest,
zend_executor_globals *src)
{
dest->current_module = src->current_module;
}
*/

/*
static void php_thread_compiler_globals_reinit(zend_compiler_globals *dest,
zend_compiler_globals *src)
{
zend_hash_clean(dest->function_table);
zend_hash_copy(dest->function_table, src->function_table,
(copy_ctor_func_t)function_add_ref, NULL,
sizeof(zend_function));
zend_hash_clean(dest->class_table);
zend_hash_copy(dest->class_table, src->class_table,
(copy_ctor_func_t)zend_class_add_ref, NULL,
sizeof(zend_class_entry*));
}
*/
function thread_startup($runtime) //: Thread
{
\ze_ffi()->ts_resource_ex(0, null);

if (\IS_WINDOWS) {
\tsrmls_cache_update();
\zend_pg('com_initialized', 0);
}

\zend_pg('in_error_log', 0);

\ze_ffi()->php_output_activate();

\zend_pg('modules_activated', 0);
\zend_pg('header_is_being_sent', 0);
\zend_pg('connection_status', 0);
\zend_pg('in_user_include', 0);

//\ze_ffi()->php_request_startup();

// \zend_sg('server_context', $runtime()->parent->server);

\zend_pg('expose_php', 0);
\zend_pg('auto_globals_jit', 1);
if (\PHP_VERSION_ID >= 80100)
\zend_pg('enable_dl', true);
else
\zend_pg('enable_dl', 1);

\zend_pg('during_request_startup', 0);
\zend_sg('sapi_started', 0);
\zend_sg('headers_sent', 1);
\zend_sg('request_info')->no_headers = 1;

// \ze_ffi()->tsrm_mutex_unlock($this->module_mutex);
// return $runtime;
}

function thread_shutdown()
{
/* Flush all output buffers */
\ze_ffi()->php_output_end_all();

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

/* SAPI related shutdown (free stuff) */
\ze_ffi()->sapi_deactivate();

// \ze_ffi()->php_request_shutdown(null);
//\zend_sg('server_context', NULL);

\ze_ffi()->sapi_shutdown();
\ze_ffi()->ts_free_thread();
}

function thread_func(CData $arg)
{
/** @var Thread|PThread */
$thread = \thread_startup(\zval_native_cast('zval*', $arg));

do {
/*
if (!$thread instanceof PThread) {
$status = $thread->wait();
if ($status != \ZE::SUCCESS) {
break;
}
}*/

while (!$thread->empty()) {
$thread->execute();
}
// pthread_mutex_lock($thread->counter_mutex);
// $thread->num_threads_working--;
// if (!$thread>num_threads_working) {
// pthread_cond_signal($thread->counter_all_idle);
// }
// pthread_mutex_unlock($thread->counter_mutex);
} while (true);

$exception = \zend_eg('exception');
if (\is_cdata($exception))
\ze_ffi()->zend_exception_error($exception, \E_ERROR);

\thread_shutdown();

return NULL;
}

/* Wait until all jobs have finished */
function thread_wait(Thread $pool)
{
// pthread_mutex_lock($thpool_p->thcount_lock);
// while ($thpool_p->jobqueue->len || $thpool_p->num_threads_working) {
// pthread_cond_wait($thpool_p->threads_all_idle, $thpool_p->thcount_lock);
// }
// pthread_mutex_unlock($thpool_p->thcount_lock);
}
}
Loading

0 comments on commit 793e808

Please sign in to comment.