php_thread.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package frankenphp
  2. // #include <stdint.h>
  3. // #include <php_variables.h>
  4. import "C"
  5. import (
  6. "net/http"
  7. "runtime"
  8. "unsafe"
  9. )
  10. var phpThreads []*phpThread
  11. type phpThread struct {
  12. runtime.Pinner
  13. mainRequest *http.Request
  14. workerRequest *http.Request
  15. worker *worker
  16. requestChan chan *http.Request
  17. knownVariableKeys map[string]*C.zend_string
  18. }
  19. func initPHPThreads(numThreads int) {
  20. phpThreads = make([]*phpThread, 0, numThreads)
  21. for i := 0; i < numThreads; i++ {
  22. phpThreads = append(phpThreads, &phpThread{})
  23. }
  24. }
  25. func (thread phpThread) getActiveRequest() *http.Request {
  26. if thread.workerRequest != nil {
  27. return thread.workerRequest
  28. }
  29. return thread.mainRequest
  30. }
  31. // Pin a string that is not null-terminated
  32. // PHP's zend_string may contain null-bytes
  33. func (thread *phpThread) pinString(s string) *C.char {
  34. sData := unsafe.StringData(s)
  35. thread.Pin(sData)
  36. return (*C.char)(unsafe.Pointer(sData))
  37. }
  38. // C strings must be null-terminated
  39. func (thread *phpThread) pinCString(s string) *C.char {
  40. return thread.pinString(s+"\x00")
  41. }