//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()
}
func wsgi_callback(r *http.Request, w http.ResponseWriter) {
	_environ := upgrade_header_to_wsgi(r)

	_body_request, _ := ioutil.ReadAll(r.Body)
	_body_c := C.CString(string(_body_request))
	defer C.free(unsafe.Pointer(_body_c))

	_response := C.run_wsgi_application(_body_c, _environ)
	defer func() {
		C.Py_DecRef(_response.body)
		C.Py_DecRef(_response.headers)
		C.Py_DecRef(_response.error)
	}()
	if _response.headers == nil || int(_response.status) == 0 {
		w.WriteHeader(500)
		log_wsgi.Error("failed to run python wsgi module.")
		return
	}

	// 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),
		)
	}

	// write body
	w.WriteHeader(int(_response.status))
	if _response.body == nil {
		return
	}
	write_response_body(w, _response.body)
}