summary |
shortlog |
log |
commit | commitdiff |
review |
tree
raw |
patch |
inline | side by side (from parent 1:
4e16c71)
Sometimes there are situations when the callback is called during the processing of the disconnect.
To avoid problems with races (for example, when using race-detector), use atomic pointers.
Signed-off-by: Alexander Demidenko <hurd@yandex-team.ru>
Change-Id: Ic825872ac620d68a191bc88889a096b15ea57464
"os"
"path/filepath"
"reflect"
"os"
"path/filepath"
"reflect"
// global VPP binary API client, library vppapiclient only supports
// single connection at a time
// global VPP binary API client, library vppapiclient only supports
// single connection at a time
-var globalVppClient *vppClient
+var globalVppClient unsafe.Pointer
// stubVppClient is the default implementation of the VppAPI.
type vppClient struct {
// stubVppClient is the default implementation of the VppAPI.
type vppClient struct {
// Connect connects the process to VPP.
func (a *vppClient) Connect() error {
// Connect connects the process to VPP.
func (a *vppClient) Connect() error {
- if globalVppClient != nil {
+ h := (*vppClient)(atomic.LoadPointer(&globalVppClient))
+ if h != nil {
return fmt.Errorf("already connected to binary API, disconnect first")
}
return fmt.Errorf("already connected to binary API, disconnect first")
}
return fmt.Errorf("connecting to VPP binary API failed (rc=%v)", rc)
}
return fmt.Errorf("connecting to VPP binary API failed (rc=%v)", rc)
}
+ atomic.StorePointer(&globalVppClient, unsafe.Pointer(a))
return nil
}
// Disconnect disconnects the process from VPP.
func (a *vppClient) Disconnect() error {
return nil
}
// Disconnect disconnects the process from VPP.
func (a *vppClient) Disconnect() error {
- globalVppClient = nil
-
+ atomic.StorePointer(&globalVppClient, nil)
rc := C.govpp_disconnect()
if rc != 0 {
return fmt.Errorf("disconnecting from VPP binary API failed (rc=%v)", rc)
}
rc := C.govpp_disconnect()
if rc != 0 {
return fmt.Errorf("disconnecting from VPP binary API failed (rc=%v)", rc)
}
//export go_msg_callback
func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) {
//export go_msg_callback
func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) {
+ h := (*vppClient)(atomic.LoadPointer(&globalVppClient))
+ if h == nil {
+ return
+ }
// convert unsafe.Pointer to byte slice
sliceHeader := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)}
byteSlice := *(*[]byte)(unsafe.Pointer(sliceHeader))
// convert unsafe.Pointer to byte slice
sliceHeader := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)}
byteSlice := *(*[]byte)(unsafe.Pointer(sliceHeader))
-
- globalVppClient.msgCallback(uint16(msgID), byteSlice)
+ h.msgCallback(uint16(msgID), byteSlice)