Skip to content

Commit

Permalink
First look at preferring autoload over class copying
Browse files Browse the repository at this point in the history
this is currently unfinished, and it will attempt to fire the autoloader for every class when INHERIT_ALL is used, which is not intended.

the aim is to avoid class copying wherever possible, since copying classes is a source of stability issues.

this change also allows sidestepping #73 for named classes.
  • Loading branch information
dktapps committed Apr 24, 2023
1 parent 6c90092 commit 8d0df96
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
20 changes: 14 additions & 6 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,21 @@ zend_bool pthreads_globals_object_connect(pthreads_zend_object_t* address, zend_
if (!ce) {
/* we may not know the class, can't use ce directly
from zend_object because it is from another context */
ce = pthreads_prepare_single_class(&pthreads->owner, pthreads->std.ce);
zend_try{
ce = pthreads_prepare_single_class(&pthreads->owner, pthreads->std.ce);
} zend_catch {
//beware of autoloading errors
ce = NULL;
valid = 0;
} zend_end_try();
}
if (ce) {
PTHREADS_ZG(connecting_object) = pthreads;
object_init_ex(object, ce);
PTHREADS_ZG(connecting_object) = NULL;
connection = PTHREADS_FETCH_FROM(Z_OBJ_P(object));
zend_hash_index_update_ptr(&PTHREADS_ZG(resolve), (zend_ulong)connection->ts_obj, connection);
}
PTHREADS_ZG(connecting_object) = pthreads;
object_init_ex(object, ce);
PTHREADS_ZG(connecting_object) = NULL;
connection = PTHREADS_FETCH_FROM(Z_OBJ_P(object));
zend_hash_index_update_ptr(&PTHREADS_ZG(resolve), (zend_ulong)connection->ts_obj, connection);
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/prepare.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,15 @@ static zend_class_entry* pthreads_create_entry(const pthreads_ident_t* source, z
return NULL;
}

if (candidate->type == ZEND_INTERNAL_CLASS
|| candidate->ce_flags & ZEND_ACC_PRELOADED
) {
return zend_lookup_class(candidate->name);
if ((candidate->ce_flags & ZEND_ACC_ANON_CLASS) == 0) {
//call autoloaders if available for named classes
prepared = zend_lookup_class(candidate->name);
if (prepared) {
return prepared;
} else if (candidate->type == ZEND_INTERNAL_CLASS || candidate->ce_flags & ZEND_ACC_PRELOADED) {
zend_error_noreturn(E_CORE_ERROR, "Internal and preloaded classes should always be able to be looked up");
return NULL;
}
}

lookup = zend_string_tolower(candidate->name);
Expand Down
37 changes: 19 additions & 18 deletions src/routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,34 @@ static void pthreads_routine_free(pthreads_routine_arg_t* r) {
} /* }}} */

/* {{{ */
static inline zend_bool pthreads_routine_run_function(pthreads_zend_object_t* object, pthreads_zend_object_t* connection, zval* work) {
static inline zend_bool pthreads_routine_run_function(pthreads_zend_object_t* object, zval* zv) {
zend_function* run;
pthreads_call_t call = PTHREADS_CALL_EMPTY;
zval zresult;
zend_execute_data execute_data;

memset(&execute_data, 0, sizeof(execute_data));
ZVAL_UNDEF(&zresult);
ZVAL_UNDEF(zv);

if (pthreads_connect(object, connection) != SUCCESS) {
return 0;
}
zend_try {
object_init_ex(zv, pthreads_prepare_single_class(&object->owner, object->std.ce));

if (pthreads_monitor_check(&object->ts_obj->monitor, PTHREADS_MONITOR_ERROR)) {
return 0;
}
pthreads_zend_object_t* connection = PTHREADS_FETCH_FROM(Z_OBJ_P(zv));

ZVAL_UNDEF(&zresult);
if (pthreads_connect(object, connection) != SUCCESS) {
return 0;
}

pthreads_monitor_add(&object->ts_obj->monitor, PTHREADS_MONITOR_RUNNING);
if (pthreads_monitor_check(&object->ts_obj->monitor, PTHREADS_MONITOR_ERROR)) {
return 0;
}

if (work)
pthreads_store_write(Z_OBJ_P(work), &PTHREADS_G(strings).worker, &PTHREADS_ZG(this), PTHREADS_STORE_NO_COERCE_ARRAY);
pthreads_monitor_add(&object->ts_obj->monitor, PTHREADS_MONITOR_RUNNING);

if (Z_OBJ_P(zv) != Z_OBJ_P(&PTHREADS_ZG(this)))
pthreads_store_write(Z_OBJ_P(zv), &PTHREADS_G(strings).worker, &PTHREADS_ZG(this), PTHREADS_STORE_NO_COERCE_ARRAY);

zend_try{
if ((run = zend_hash_find_ptr(&connection->std.ce->function_table, PTHREADS_G(strings).run))) {
if (run->type == ZEND_USER_FUNCTION) {
EG(current_execute_data) = &execute_data;
Expand Down Expand Up @@ -101,9 +106,7 @@ static void* pthreads_routine(pthreads_routine_arg_t* routine) {
pthreads_queue done_tasks_cache;

zend_first_try{
ZVAL_UNDEF(&PTHREADS_ZG(this));
object_init_ex(&PTHREADS_ZG(this), pthreads_prepare_single_class(&thread->owner, thread->std.ce));
pthreads_routine_run_function(thread, PTHREADS_FETCH_FROM(Z_OBJ_P(&PTHREADS_ZG(this))), NULL);
pthreads_routine_run_function(thread, &PTHREADS_ZG(this));

if (PTHREADS_IS_WORKER(thread)) {
zval task;
Expand All @@ -112,9 +115,7 @@ static void* pthreads_routine(pthreads_routine_arg_t* routine) {

while (pthreads_worker_next_task(thread->worker_data, &done_tasks_cache, &task) != PTHREADS_MONITOR_JOINED) {
zval that;
pthreads_zend_object_t* work = PTHREADS_FETCH_FROM(Z_OBJ(task));
object_init_ex(&that, pthreads_prepare_single_class(&work->owner, work->std.ce));
pthreads_routine_run_function(work, PTHREADS_FETCH_FROM(Z_OBJ(that)), &that);
pthreads_routine_run_function(PTHREADS_FETCH_FROM(Z_OBJ(task)), &that);
pthreads_worker_add_garbage(thread->worker_data, &done_tasks_cache, &that);
zval_ptr_dtor(&that);
}
Expand Down
4 changes: 3 additions & 1 deletion src/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ void pthreads_worker_add_garbage(pthreads_worker_data_t *worker_data, pthreads_q
pthreads_queue_push(&worker_data->gc, worker_data->running);
worker_data->running = NULL;
pthreads_monitor_unlock(worker_data->monitor);
pthreads_queue_push_new(done_tasks_cache, work_zval);
if (!Z_ISUNDEF_P(work_zval)) { //we may have failed to init the local connection object due to an autoloading error
pthreads_queue_push_new(done_tasks_cache, work_zval);
}
} else {
ZEND_ASSERT(0);
}
Expand Down

0 comments on commit 8d0df96

Please sign in to comment.