func (self *ThreadPool) Start(callback ThreadCallback, args ...interface{}) *C.pthread_t { /* If `args` has only one value, sometimes the runtime will be hanged, I can not understant why, so this is dirty hack. */ args = append(args, true) _callback := C.create_callback( self.ptr, *(*unsafe.Pointer)(unsafe.Pointer(&callback)), *(*unsafe.Pointer)(unsafe.Pointer(&args)), ) return C.start_thread(self.ptr, _callback) }
//export createCallback func createCallback(pid *C.pthread_t) { //runtime.LockOSThread() //defer runtime.UnlockOSThread() _gstate := C.start_thread() defer C.end_thread(_gstate) _func, _ok := callbacks.Get(pid) defer callbacks.Delete(pid) if !_ok { panic(fmt.Errorf("failed to found thread callback for `%v`", pid)) } _func() }
//export createThreadCallback func createThreadCallback(pid *C.pthread_t) { runtime.LockOSThread() defer runtime.UnlockOSThread() _gstate := C.start_thread() _cb, _r, _w, _ok := callbacks.Get(pid) defer callbacks.Delete(pid) if !_ok { panic(fmt.Errorf("failed to found thread callback for `%v`", pid)) } // TODO: add special headers for WSGI. _environ := GenerateEnviron(_r) _response := C.run_wsgi_application(_environ) // parse header for i := 0; i < int(C.PyList_Size(_response.headers)); i++ { _h := C.PyList_GetItem(_response.headers, C.Py_ssize_t(i)) _k := C.PyTuple_GetItem(_h, C.Py_ssize_t(0)) _v := C.PyTuple_GetItem(_h, C.Py_ssize_t(1)) _w.Header().Set( PyString_AsString(_k), PyString_AsString(_v), ) } _body := C.GoString(_response.body) if len(_body) < 1 { panic(fmt.Errorf("failed to import python wsgi module.")) } C.end_thread(_gstate) // write body _w.WriteHeader(int(_response.status)) _w.Write([]byte(_body)) _cb() }