php_thread.go 1.1 KB

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