-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make FFI callbacks thread safe #12823
Draft
smx-smx
wants to merge
21
commits into
php:master
Choose a base branch
from
smx-smx:ffi-ts-master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+479
−209
Draft
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
16be72b
ffi: thread safe callbacks (preliminary)
smx-smx 1de9669
ffi: make sure there are no in progress requests before posting a new…
smx-smx 56c24d4
code style
smx-smx 6423d3a
ffi: trace the requester and main thread IDs
smx-smx 98eb079
ffi: fix deadlock when the callback invocation is from the main thread
smx-smx d762cfe
ffi: add tests/callback_threads
smx-smx 29d6550
ffi: fix mutex unlock before zend_error_noreturn (fixes bug79177.phpt)
smx-smx ba483e6
ffi: remove wrongly placed restore of interrupt handler
smx-smx bce091d
ffi: have callbacks be handled by the thread that invoked them
smx-smx 083cc9e
ffi: fix bug79177 once again
smx-smx 34353aa
ffi: cleanup
smx-smx 9164b16
ffi: initialize stack info for the new thread
smx-smx 20dd9b1
ffi: fix vm_ack <-> vm_unlock deadlock
smx-smx e20a564
ffi: add missing includes for php_ffi.h
smx-smx af12a99
enable TSRM mutex APIs outside of ZTS
smx-smx 261a1d3
tsrm: add cond API (POSIX only for now)
smx-smx 2f71fa3
zend_globals_macros.h: add missing include
smx-smx 391ca94
ffi: first version using fibers for callbacks
smx-smx e8b2b5f
ffi: fix tests
smx-smx e2fce83
tsrm: implement win32 cond API
smx-smx dc5496f
fix build errors
smx-smx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
#ifndef PHP_FFI_H | ||
#define PHP_FFI_H | ||
|
||
#include <ffi.h> | ||
#include <pthread.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows build is broken. There are no <pthread.h> there. |
||
|
||
extern zend_module_entry ffi_module_entry; | ||
#define phpext_ffi_ptr &ffi_module_entry | ||
|
||
|
@@ -27,6 +30,15 @@ typedef enum _zend_ffi_api_restriction { | |
} zend_ffi_api_restriction; | ||
|
||
typedef struct _zend_ffi_type zend_ffi_type; | ||
typedef struct _zend_ffi_callback_data zend_ffi_callback_data; | ||
|
||
|
||
typedef struct _zend_ffi_call_data { | ||
ffi_cif* cif; | ||
void* ret; | ||
void** args; | ||
zend_ffi_callback_data* data; | ||
} zend_ffi_call_data; | ||
|
||
ZEND_BEGIN_MODULE_GLOBALS(ffi) | ||
zend_ffi_api_restriction restriction; | ||
|
@@ -35,6 +47,12 @@ ZEND_BEGIN_MODULE_GLOBALS(ffi) | |
/* predefined ffi_types */ | ||
HashTable types; | ||
|
||
zend_atomic_bool callback_in_progress; | ||
pthread_mutex_t vm_lock; | ||
pthread_cond_t vm_ack; | ||
pthread_t main_tid; | ||
zend_ffi_call_data callback_data; | ||
|
||
/* preloading */ | ||
char *preload; | ||
HashTable *scopes; /* list of preloaded scopes */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--TEST-- | ||
FFI Thread safe callbacks | ||
--EXTENSIONS-- | ||
ffi | ||
--SKIPIF-- | ||
<?php | ||
try { | ||
$libc = FFI::cdef(' | ||
int pthread_create(void *restrict thread, | ||
const void *restrict attr, | ||
void *(*start_routine)(void *), | ||
void *arg); | ||
', 'libc.so.6'); | ||
} catch(Throwable $_){ | ||
die('skip libc.so.6 not available'); | ||
} | ||
?> | ||
--INI-- | ||
ffi.enable=1 | ||
--FILE-- | ||
<?php | ||
$libc = FFI::cdef(' | ||
typedef uint64_t pthread_t; | ||
int pthread_create(pthread_t *thread, | ||
const void* attr, | ||
void *(*start_routine)(void *), | ||
void *arg); | ||
int pthread_detach(pthread_t thread); | ||
', 'libc.so.6'); | ||
|
||
$tid = $libc->new($libc->type('pthread_t')); | ||
$accum = 0; | ||
$thread_func = function($arg) use($libc, &$accum){ | ||
//$v = $libc->cast('int *', $arg)[0]; | ||
FFI::free($arg); | ||
usleep(10 * 1000); | ||
$accum++; | ||
//print("."); | ||
}; | ||
|
||
for($i=0; $i<100; $i++){ | ||
$arg = $libc->new('int', false); | ||
$arg->cdata = $i; | ||
|
||
$libc->pthread_create( | ||
FFI::addr($tid), NULL, | ||
$thread_func, FFI::addr($arg) | ||
); | ||
$libc->pthread_detach($tid->cdata); | ||
} | ||
|
||
while($accum != 100){ | ||
//print("w"); | ||
usleep(1000); | ||
} | ||
print($accum); | ||
?> | ||
--EXPECT-- | ||
100 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may a regular in-main-thread callback, that doesn't require any locks.
You should check
FFI_G(callback_tid) == FFI_G(main_tid)
first.Ideally, we should make distinct between regular and "thread" callback