Skip to content
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

refactor: decouple worker threads from non-worker threads #1137

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

Alliballibaba2
Copy link
Collaborator

This PR refactors how threads are started and is meant as a step towards scaling threads at runtime.

How worker threads are currently started:

Currently, worker threads are started from regular threads via sending a special request
to ServeHTTP. The disadvantage here is that between sending the special worker request
and receiving it, we are losing control over which thread becomes a worker thread.
Worker threads and regular threads are inevitable coupled to each other.

How worker threads are started with this PR:

This PR decouples worker threads from regular threads and makes the php_thread struct
a wrapper around the thread's lifetime.

A 'PHP thread' is currently just a pthread with its own TSRM storage (this doesn't
necessarily have to be tied to a real thread in the future as discussed in #1090).

The thread starts, does some work in a loop and then stops. This PR makes it possible
to configure these 3 lifetime hooks from the go side via the php_thread struct:

  • onStartup: Right before the thread is ready (once)
  • onWork: The actual work the thread does (in a loop)
  • onShutdown: Right before the thread is stopped (once)

This allows re-using the same mechanism for regular threads as well as worker threads.
It also makes it easier to create other potential types of threads in the future
(like 'scheduled workers' or 'task workers').

Additionally, it now would also be possible to grab an 'idle thread', exchange it's hooks and
turn it into a different type of thread at runtime without stopping the underlying thread.
(This PR doesn't go that far though)

@Alliballibaba2
Copy link
Collaborator Author

Hmm that segfault is interesting, It's probably not fully safe to execute a PHP script while calling (void)ts_resource(0);, I'll adjust the logic.

frankenphp.c Outdated Show resolved Hide resolved
frankenphp.c Outdated Show resolved Hide resolved
frankenphp.c Outdated Show resolved Hide resolved
frankenphp.go Outdated Show resolved Hide resolved
frankenphp.go Show resolved Hide resolved
php_thread.go Outdated Show resolved Hide resolved
php_thread.go Show resolved Hide resolved
php_threads_test.go Outdated Show resolved Hide resolved
testdata/sleep.php Outdated Show resolved Hide resolved
worker.go Outdated
// when frankenphp_finish_request() is directly called from PHP
//
//export go_frankenphp_finish_request_manually
func go_frankenphp_finish_request_manually(threadIndex C.uintptr_t) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func go_frankenphp_finish_request_manually(threadIndex C.uintptr_t) {
func go_frankenphp_php_finish_request(threadIndex C.uintptr_t) {

?

I'm not fond of the name I suggest, but "manually" sounds a bit weird.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess you're right 😅 . I changed it to go_frankenphp_finish_php_request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants