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