diff options
author | Max Romanov <max.romanov@nginx.com> | 2017-12-06 12:16:02 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@nginx.com> | 2017-12-06 12:16:02 +0300 |
commit | d14c0774c7e6f372dfebcfcafdcac718b7e28789 (patch) | |
tree | 6111a740fb2d1898b0375a8f65235d3f319b160a /src/go/unit/unit.go | |
parent | 0db4d2531648796a3447a409f05bd82bcc91ff9f (diff) | |
download | unit-d14c0774c7e6f372dfebcfcafdcac718b7e28789.tar.gz unit-d14c0774c7e6f372dfebcfcafdcac718b7e28789.tar.bz2 |
Go: removing request registry.
Passing unsafe.Pointers (void *) from Go to C is complicated by an attempt
to make such pointers less unsafe.
A straightforward optimization is to replace 'unsafe.Pointer' with 'uintptr'
(thanks to Xin Huang for the idea: https://stackoverflow.com/a/44826533 ).
As a result, request registry with mutex is gone.
Diffstat (limited to 'src/go/unit/unit.go')
-rw-r--r-- | src/go/unit/unit.go | 52 |
1 files changed, 16 insertions, 36 deletions
diff --git a/src/go/unit/unit.go b/src/go/unit/unit.go index 74e53ecf..82a54ecc 100644 --- a/src/go/unit/unit.go +++ b/src/go/unit/unit.go @@ -20,32 +20,21 @@ import ( ) type cbuf struct { - b unsafe.Pointer + b C.uintptr_t s C.size_t - f bool } -func new_cbuf(buf []byte) *cbuf { +func buf_ref(buf []byte) C.uintptr_t { if len(buf) == 0 { - return nil + return 0 } - return &cbuf{ - getCBytes(buf), C.size_t(len(buf)), true, - } + return C.uintptr_t(uintptr(unsafe.Pointer(&buf[0]))) } -func (buf *cbuf) Close() { - if buf == nil { - return - } - - if buf.f && buf.s > 0 { - C.free(buf.b) - buf.f = false - buf.b = nil - buf.s = 0 - } +func (buf *cbuf) init(b []byte) { + buf.b = buf_ref(b) + buf.s = C.size_t(len(b)) } func (buf *cbuf) GoBytes() []byte { @@ -54,24 +43,7 @@ func (buf *cbuf) GoBytes() []byte { return b[:0] } - return C.GoBytes(buf.b, C.int(buf.s)) -} - -type cmsg struct { - buf cbuf - oob cbuf -} - -func new_cmsg(buf []byte, oob []byte) *cmsg { - return &cmsg{ - buf: cbuf{getCBytes(buf), C.size_t(len(buf)), true}, - oob: cbuf{getCBytes(oob), C.size_t(len(oob)), true}, - } -} - -func (msg *cmsg) Close() { - msg.buf.Close() - msg.oob.Close() + return C.GoBytes(unsafe.Pointer(uintptr(buf.b)), C.int(buf.s)) } var nxt_go_quit bool = false @@ -81,6 +53,14 @@ func nxt_go_set_quit() { nxt_go_quit = true } +func nxt_go_warn(format string, args ...interface{}) { + fmt.Fprintf(os.Stderr, "[go warn] " + format + "\n", args...) +} + +func nxt_go_debug(format string, args ...interface{}) { + // fmt.Fprintf(os.Stderr, "[go debug] " + format + "\n", args...) +} + func ListenAndServe(addr string, handler http.Handler) error { var read_port *port |