Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
16be72b
ffi: thread safe callbacks (preliminary)
smx-smx Nov 28, 2023
1de9669
ffi: make sure there are no in progress requests before posting a new…
smx-smx Nov 28, 2023
56c24d4
code style
smx-smx Nov 29, 2023
6423d3a
ffi: trace the requester and main thread IDs
smx-smx Nov 29, 2023
98eb079
ffi: fix deadlock when the callback invocation is from the main thread
smx-smx Nov 29, 2023
d762cfe
ffi: add tests/callback_threads
smx-smx Nov 29, 2023
29d6550
ffi: fix mutex unlock before zend_error_noreturn (fixes bug79177.phpt)
smx-smx Nov 29, 2023
ba483e6
ffi: remove wrongly placed restore of interrupt handler
smx-smx Nov 29, 2023
bce091d
ffi: have callbacks be handled by the thread that invoked them
smx-smx Nov 30, 2023
083cc9e
ffi: fix bug79177 once again
smx-smx Nov 30, 2023
34353aa
ffi: cleanup
smx-smx Nov 30, 2023
9164b16
ffi: initialize stack info for the new thread
smx-smx Dec 1, 2023
20dd9b1
ffi: fix vm_ack <-> vm_unlock deadlock
smx-smx Dec 1, 2023
e20a564
ffi: add missing includes for php_ffi.h
smx-smx Dec 2, 2023
af12a99
enable TSRM mutex APIs outside of ZTS
smx-smx Dec 2, 2023
261a1d3
tsrm: add cond API (POSIX only for now)
smx-smx Dec 2, 2023
2f71fa3
zend_globals_macros.h: add missing include
smx-smx Dec 3, 2023
391ca94
ffi: first version using fibers for callbacks
smx-smx Dec 3, 2023
e8b2b5f
ffi: fix tests
smx-smx Dec 3, 2023
e2fce83
tsrm: implement win32 cond API
smx-smx Dec 3, 2023
dc5496f
fix build errors
smx-smx Dec 3, 2023
37ee7de
Fix GH-13215 GCC 14 build
remicollet Jan 22, 2024
d2b2192
zend_ffi_callback_trampoline: fix build without ZEND_CHECK_STACK_LIMIT
smx-smx Apr 30, 2025
38f88d0
Merge commit '87d75328b2ac3cc6fc0360ede8f70d324a70819f' into ffi-ts-m…
smx-smx Apr 30, 2025
7e5697e
Merge remote-tracking branch 'origin/master' into ffi-ts-master2
smx-smx Apr 30, 2025
6f6a737
ffi: fix crash in callback (by using zend_fiber_resume_internal inste…
smx-smx May 5, 2025
9d2e24a
ffi: exceptions can now be thrown in PHP callbacks
smx-smx May 5, 2025
bd953ba
ffi: fix test gh16013
smx-smx May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ffi: fix deadlock when the callback invocation is from the main thread
  • Loading branch information
smx-smx committed Nov 29, 2023
commit 98eb07910a0b2e3beaf30e73bdf8fb15a7f491d6
31 changes: 20 additions & 11 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,12 @@ static void zend_ffi_callback_hash_dtor(zval *zv) /* {{{ */

static void (*orig_interrupt_function)(zend_execute_data *execute_data);

static void zend_ffi_interrupt_function(zend_execute_data *execute_data){ /* {{{ */
static void zend_ffi_dispatch_callback(void){ /* {{{ */
// this function must always run on the main thread
assert(pthread_self() == FFI_G(main_tid));

if (!zend_atomic_bool_load_ex(&FFI_G(callback_in_progress))) {
goto end;
return;
}

zend_ffi_callback_data *callback_data = FFI_G(callback_data).data;
Expand Down Expand Up @@ -998,8 +998,12 @@ static void zend_ffi_interrupt_function(zend_execute_data *execute_data){ /* {{{

zend_atomic_bool_store_ex(&FFI_G(callback_in_progress), false);
pthread_cond_broadcast(&FFI_G(vm_ack));
}
/* }}} */

static void zend_ffi_interrupt_function(zend_execute_data *execute_data){ /* {{{ */
zend_ffi_dispatch_callback();

end:
if (orig_interrupt_function) {
orig_interrupt_function(execute_data);
}
Expand Down Expand Up @@ -1038,18 +1042,23 @@ static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, v
.data = (zend_ffi_callback_data *)data
};
FFI_G(callback_data) = call_data;
FFI_G(requester_tid) = pthread_self();

if(pthread_self() == FFI_G(main_tid)){

}
bool is_main_thread = pthread_self() == FFI_G(main_tid);

// post interrupt request
zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
if(is_main_thread){
// dispatch the callback directly
zend_ffi_dispatch_callback();
} else {
// post interrupt request to acquire the main thread
zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
}

pthread_mutex_unlock(&FFI_G(vm_lock));

// wait for the request to complete before returning
zend_ffi_wait_request_barrier(true);
if(!is_main_thread){
// wait for the request to complete before returning
zend_ffi_wait_request_barrier(true);
}
}
/* }}} */

Expand Down
1 change: 0 additions & 1 deletion ext/ffi/php_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ ZEND_BEGIN_MODULE_GLOBALS(ffi)
pthread_mutex_t vm_lock;
pthread_cond_t vm_ack;
pthread_t main_tid;
pthread_t requester_tid;
zend_ffi_call_data callback_data;

/* preloading */
Expand Down