mirror of
https://github.com/dunglas/frankenphp.git
synced 2026-04-23 00:37:20 +08:00
f592e0f47b
* Decouple workers. * Moves code to separate file. * Cleans up the exponential backoff. * Initial working implementation. * Refactors php threads to take callbacks. * Cleanup. * Cleanup. * Cleanup. * Cleanup. * Adjusts watcher logic. * Adjusts the watcher logic. * Fix opcache_reset race condition. * Fixing merge conflicts and formatting. * Prevents overlapping of TSRM reservation and script execution. * Adjustments as suggested by @dunglas. * Adds error assertions. * Adds comments. * Removes logs and explicitly compares to C.false. * Resets check. * Adds cast for safety. * Fixes waitgroup overflow. * Resolves waitgroup race condition on startup. * Moves worker request logic to worker.go. * Removes defer. * Removes call from go to c. * Fixes merge conflict. * Adds fibers test back in. * Refactors new thread loop approach. * Removes redundant check. * Adds compareAndSwap. * Refactor: removes global waitgroups and uses a 'thread state' abstraction instead. * Removes unnecessary method. * Updates comment. * Removes unnecessary booleans. * test * First state machine steps. * Splits threads. * Minimal working implementation with broken tests. * Fixes tests. * Refactoring. * Fixes merge conflicts. * Formatting * C formatting. * More cleanup. * Allows for clean state transitions. * Adds state tests. * Adds support for thread transitioning. * Fixes the testdata path. * Formatting. * Allows transitioning back to inactive state. * Fixes go linting. * Formatting. * Removes duplication. * Applies suggestions by @dunglas * Removes redundant check. * Locks the handler on restart. * Removes unnecessary log. * Changes Unpin() logic as suggested by @withinboredom * Adds suggestions by @dunglas and resolves TODO. * Makes restarts fully safe. * Will make the initial startup fail even if the watcher is enabled (as is currently the case) * Also adds compareAndSwap to the test. * Adds comment. * Prevents panic on initial watcher startup.
109 lines
2.4 KiB
Go
109 lines
2.4 KiB
Go
package frankenphp
|
|
|
|
// #include "frankenphp.h"
|
|
import "C"
|
|
import (
|
|
"sync"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// represents the main PHP thread
|
|
// the thread needs to keep running as long as all other threads are running
|
|
type phpMainThread struct {
|
|
state *threadState
|
|
done chan struct{}
|
|
numThreads int
|
|
}
|
|
|
|
var (
|
|
phpThreads []*phpThread
|
|
mainThread *phpMainThread
|
|
)
|
|
|
|
// reserve a fixed number of PHP threads on the Go side
|
|
func initPHPThreads(numThreads int) error {
|
|
mainThread = &phpMainThread{
|
|
state: newThreadState(),
|
|
done: make(chan struct{}),
|
|
numThreads: numThreads,
|
|
}
|
|
phpThreads = make([]*phpThread, numThreads)
|
|
|
|
if err := mainThread.start(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// initialize all threads as inactive
|
|
for i := 0; i < numThreads; i++ {
|
|
phpThreads[i] = newPHPThread(i)
|
|
convertToInactiveThread(phpThreads[i])
|
|
}
|
|
|
|
// start the underlying C threads
|
|
ready := sync.WaitGroup{}
|
|
ready.Add(numThreads)
|
|
for _, thread := range phpThreads {
|
|
go func() {
|
|
if !C.frankenphp_new_php_thread(C.uintptr_t(thread.threadIndex)) {
|
|
logger.Panic("unable to create thread", zap.Int("threadIndex", thread.threadIndex))
|
|
}
|
|
thread.state.waitFor(stateInactive)
|
|
ready.Done()
|
|
}()
|
|
}
|
|
ready.Wait()
|
|
|
|
return nil
|
|
}
|
|
|
|
func drainPHPThreads() {
|
|
doneWG := sync.WaitGroup{}
|
|
doneWG.Add(len(phpThreads))
|
|
for _, thread := range phpThreads {
|
|
thread.handlerMu.Lock()
|
|
_ = thread.state.requestSafeStateChange(stateShuttingDown)
|
|
close(thread.drainChan)
|
|
}
|
|
close(mainThread.done)
|
|
for _, thread := range phpThreads {
|
|
go func(thread *phpThread) {
|
|
thread.state.waitFor(stateDone)
|
|
thread.handlerMu.Unlock()
|
|
doneWG.Done()
|
|
}(thread)
|
|
}
|
|
doneWG.Wait()
|
|
mainThread.state.set(stateShuttingDown)
|
|
mainThread.state.waitFor(stateDone)
|
|
phpThreads = nil
|
|
}
|
|
|
|
func (mainThread *phpMainThread) start() error {
|
|
if C.frankenphp_new_main_thread(C.int(mainThread.numThreads)) != 0 {
|
|
return MainThreadCreationError
|
|
}
|
|
mainThread.state.waitFor(stateReady)
|
|
return nil
|
|
}
|
|
|
|
func getInactivePHPThread() *phpThread {
|
|
for _, thread := range phpThreads {
|
|
if thread.state.is(stateInactive) {
|
|
return thread
|
|
}
|
|
}
|
|
panic("not enough threads reserved")
|
|
}
|
|
|
|
//export go_frankenphp_main_thread_is_ready
|
|
func go_frankenphp_main_thread_is_ready() {
|
|
mainThread.state.set(stateReady)
|
|
mainThread.state.waitFor(stateShuttingDown)
|
|
}
|
|
|
|
//export go_frankenphp_shutdown_main_thread
|
|
func go_frankenphp_shutdown_main_thread() {
|
|
mainThread.state.set(stateDone)
|
|
}
|