Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions caddy/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package caddy
import (
"encoding/json"
"fmt"
"net/http"

"github.com/caddyserver/caddy/v2"
"github.com/dunglas/frankenphp"
"net/http"
)

type FrankenPHPAdmin struct{}
type FrankenPHPAdmin struct {
}

// if the id starts with "admin.api" the module will register AdminRoutes via module.Routes()
func (FrankenPHPAdmin) CaddyModule() caddy.ModuleInfo {
Expand Down
6 changes: 4 additions & 2 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ func (ts *ThreadState) notifySubscribers(nextState State) {
ts.subscribers = ts.subscribers[:n]
}

// block until the thread reaches a certain state
// WaitFor blocks until the thread reaches a certain state
func (ts *ThreadState) WaitFor(states ...State) {
ts.mu.Lock()
if slices.Contains(states, ts.currentState) {
ts.mu.Unlock()

return
}

sub := stateSubscriber{
states: states,
ch: make(chan struct{}),
Expand All @@ -157,7 +159,7 @@ func (ts *ThreadState) WaitFor(states ...State) {
<-sub.ch
}

// safely request a state change from a different goroutine
// RequestSafeStateChange safely requests a state change from a different goroutine
func (ts *ThreadState) RequestSafeStateChange(nextState State) bool {
ts.mu.Lock()
switch ts.currentState {
Expand Down
9 changes: 7 additions & 2 deletions phpthread.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type phpThread struct {
sandboxedEnv map[string]*C.zend_string
}

// interface that defines how the callbacks from the C thread should be handled
// threadHandler defines how the callbacks from the C thread should be handled
type threadHandler interface {
name() string
beforeScriptExecution() string
Expand Down Expand Up @@ -68,6 +68,7 @@ func (thread *phpThread) shutdown() {
if !thread.state.RequestSafeStateChange(state.ShuttingDown) {
// already shutting down or done, wait for the C thread to finish
thread.state.WaitFor(state.Done, state.Reserved)

return
}

Expand All @@ -81,17 +82,19 @@ func (thread *phpThread) shutdown() {
}
}

// change the thread handler safely
// setHandler changes the thread handler safely
// must be called from outside the PHP thread
func (thread *phpThread) setHandler(handler threadHandler) {
thread.handlerMu.Lock()
defer thread.handlerMu.Unlock()

if !thread.state.RequestSafeStateChange(state.TransitionRequested) {
// no state change allowed == shutdown or done
return
}

close(thread.drainChan)

thread.state.WaitFor(state.TransitionInProgress)
thread.handler = handler
thread.drainChan = make(chan struct{})
Expand Down Expand Up @@ -125,6 +128,7 @@ func (thread *phpThread) name() string {
thread.handlerMu.RLock()
name := thread.handler.name()
thread.handlerMu.RUnlock()

return name
}

Expand All @@ -135,6 +139,7 @@ func (thread *phpThread) pinString(s string) *C.char {
if sData == nil {
return nil
}

thread.Pin(sData)

return (*C.char)(unsafe.Pointer(sData))
Expand Down
Loading