phpthread.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package frankenphp
  2. // #include "frankenphp.h"
  3. import "C"
  4. import (
  5. "net/http"
  6. "runtime"
  7. "sync"
  8. "unsafe"
  9. "go.uber.org/zap"
  10. )
  11. // representation of the actual underlying PHP thread
  12. // identified by the index in the phpThreads slice
  13. type phpThread struct {
  14. runtime.Pinner
  15. threadIndex int
  16. requestChan chan *http.Request
  17. drainChan chan struct{}
  18. handlerMu sync.Mutex
  19. handler threadHandler
  20. state *threadState
  21. }
  22. // interface that defines how the callbacks from the C thread should be handled
  23. type threadHandler interface {
  24. name() string
  25. beforeScriptExecution() string
  26. afterScriptExecution(exitStatus int)
  27. getActiveRequest() *http.Request
  28. }
  29. func newPHPThread(threadIndex int) *phpThread {
  30. return &phpThread{
  31. threadIndex: threadIndex,
  32. requestChan: make(chan *http.Request),
  33. state: newThreadState(),
  34. }
  35. }
  36. // boot starts the underlying PHP thread
  37. func (thread *phpThread) boot() {
  38. // thread must be in reserved state to boot
  39. if !thread.state.compareAndSwap(stateReserved, stateBooting) && !thread.state.compareAndSwap(stateBootRequested, stateBooting) {
  40. logger.Panic("thread is not in reserved state: " + thread.state.name())
  41. return
  42. }
  43. // boot threads as inactive
  44. thread.handlerMu.Lock()
  45. thread.handler = &inactiveThread{thread: thread}
  46. thread.drainChan = make(chan struct{})
  47. thread.handlerMu.Unlock()
  48. // start the actual posix thread - TODO: try this with go threads instead
  49. if !C.frankenphp_new_php_thread(C.uintptr_t(thread.threadIndex)) {
  50. logger.Panic("unable to create thread", zap.Int("threadIndex", thread.threadIndex))
  51. }
  52. thread.state.waitFor(stateInactive)
  53. }
  54. // shutdown the underlying PHP thread
  55. func (thread *phpThread) shutdown() {
  56. if !thread.state.requestSafeStateChange(stateShuttingDown) {
  57. // already shutting down or done
  58. return
  59. }
  60. close(thread.drainChan)
  61. thread.state.waitFor(stateDone)
  62. thread.drainChan = make(chan struct{})
  63. // threads go back to the reserved state from which they can be booted again
  64. if mainThread.state.is(stateReady) {
  65. thread.state.set(stateReserved)
  66. }
  67. }
  68. // change the thread handler safely
  69. // must be called from outside the PHP thread
  70. func (thread *phpThread) setHandler(handler threadHandler) {
  71. thread.handlerMu.Lock()
  72. defer thread.handlerMu.Unlock()
  73. if !thread.state.requestSafeStateChange(stateTransitionRequested) {
  74. // no state change allowed == shutdown or done
  75. return
  76. }
  77. close(thread.drainChan)
  78. thread.state.waitFor(stateTransitionInProgress)
  79. thread.handler = handler
  80. thread.drainChan = make(chan struct{})
  81. thread.state.set(stateTransitionComplete)
  82. }
  83. // transition to a new handler safely
  84. // is triggered by setHandler and executed on the PHP thread
  85. func (thread *phpThread) transitionToNewHandler() string {
  86. thread.state.set(stateTransitionInProgress)
  87. thread.state.waitFor(stateTransitionComplete)
  88. // execute beforeScriptExecution of the new handler
  89. return thread.handler.beforeScriptExecution()
  90. }
  91. func (thread *phpThread) getActiveRequest() *http.Request {
  92. return thread.handler.getActiveRequest()
  93. }
  94. // Pin a string that is not null-terminated
  95. // PHP's zend_string may contain null-bytes
  96. func (thread *phpThread) pinString(s string) *C.char {
  97. sData := unsafe.StringData(s)
  98. if sData == nil {
  99. return nil
  100. }
  101. thread.Pin(sData)
  102. return (*C.char)(unsafe.Pointer(sData))
  103. }
  104. // C strings must be null-terminated
  105. func (thread *phpThread) pinCString(s string) *C.char {
  106. return thread.pinString(s + "\x00")
  107. }
  108. //export go_frankenphp_before_script_execution
  109. func go_frankenphp_before_script_execution(threadIndex C.uintptr_t) *C.char {
  110. thread := phpThreads[threadIndex]
  111. scriptName := thread.handler.beforeScriptExecution()
  112. // if no scriptName is passed, shut down
  113. if scriptName == "" {
  114. return nil
  115. }
  116. // return the name of the PHP script that should be executed
  117. return thread.pinCString(scriptName)
  118. }
  119. //export go_frankenphp_after_script_execution
  120. func go_frankenphp_after_script_execution(threadIndex C.uintptr_t, exitStatus C.int) {
  121. thread := phpThreads[threadIndex]
  122. if exitStatus < 0 {
  123. panic(ScriptExecutionError)
  124. }
  125. thread.handler.afterScriptExecution(int(exitStatus))
  126. // unpin all memory used during script execution
  127. thread.Unpin()
  128. }
  129. //export go_frankenphp_on_thread_shutdown
  130. func go_frankenphp_on_thread_shutdown(threadIndex C.uintptr_t) {
  131. thread := phpThreads[threadIndex]
  132. thread.Unpin()
  133. thread.state.set(stateDone)
  134. }