Commit 72e17467 authored by Daniel P. Berrange's avatar Daniel P. Berrange

Expose the 'Ref' methods on objects

Since Go code is responsible for manually releasing the C object
by calling Free, there is a need for acquire extra references
to cope with the scenario that multiple go threads are using the
same object
Signed-off-by: 's avatarDaniel P. Berrange <berrange@redhat.com>
parent ea3aa116
......@@ -138,19 +138,6 @@ var (
"virEventUpdateHandleFunc",
"virEventUpdateTimeoutFunc",
/* Only needed at C level */
"virConnectRef",
"virDomainRef",
"virDomainSnapshotRef",
"virInterfaceRef",
"virNWFilterRef",
"virNetworkRef",
"virNodeDeviceRef",
"virSecretRef",
"virStoragePoolRef",
"virStorageVolRef",
"virStreamRef",
/* Typedefs that don't need exposing as is */
"virStreamSinkFunc",
"virStreamSourceFunc",
......
......@@ -423,6 +423,14 @@ func (c *Connect) Close() (int, error) {
return result, nil
}
func (c *Connect) Ref() error {
ret := C.virConnectRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
type CloseCallback func(conn *Connect, reason ConnectCloseReason)
// Register a close callback for the given destination. Only one
......
......@@ -52,6 +52,12 @@
// }
// defer dom.Free()
//
// If multiple goroutines are using the same libvirt object struct, it may
// not be possible to determine which goroutine should call 'Free'. In such
// scenarios each new goroutine should call 'Ref' to obtain a private reference
// on the underlying C struct. All goroutines can call 'Free' unconditionally
// with the final one causing the release of the C object.
//
// For methods, the 'vir' prefix and object name prefix are remove from the name.
// The C functions become methods with an object receiver. e.g.
// 'virDomainScreenshot' in C becomes 'Screenshot' with a 'Domain *' receiver.
......
......@@ -868,6 +868,14 @@ func (d *Domain) Free() error {
return nil
}
func (c *Domain) Ref() error {
ret := C.virDomainRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (d *Domain) Create() error {
result := C.virDomainCreate(d.ptr)
if result == -1 {
......
......@@ -97,6 +97,14 @@ func (s *DomainSnapshot) Free() error {
return nil
}
func (c *DomainSnapshot) Ref() error {
ret := C.virDomainSnapshotRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (s *DomainSnapshot) Delete(flags DomainSnapshotDeleteFlags) error {
result := C.virDomainSnapshotDelete(s.ptr, C.uint(flags))
if result != 0 {
......
......@@ -118,3 +118,11 @@ func (n *Interface) Free() error {
n.ptr = nil
return nil
}
func (c *Interface) Ref() error {
ret := C.virInterfaceRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
......@@ -129,6 +129,14 @@ func (n *Network) Free() error {
return nil
}
func (c *Network) Ref() error {
ret := C.virNetworkRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (n *Network) Create() error {
result := C.virNetworkCreate(n.ptr)
if result == -1 {
......
......@@ -65,6 +65,14 @@ func (n *NodeDevice) Free() error {
return nil
}
func (c *NodeDevice) Ref() error {
ret := C.virNodeDeviceRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (n *NodeDevice) Destroy() error {
result := C.virNodeDeviceDestroy(n.ptr)
if result == -1 {
......
......@@ -50,6 +50,14 @@ func (f *NWFilter) Free() error {
return nil
}
func (c *NWFilter) Ref() error {
ret := C.virNWFilterRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (f *NWFilter) GetName() (string, error) {
name := C.virNWFilterGetName(f.ptr)
if name == nil {
......
......@@ -75,6 +75,14 @@ func (s *Secret) Free() error {
return nil
}
func (c *Secret) Ref() error {
ret := C.virSecretRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (s *Secret) Undefine() error {
result := C.virSecretUndefine(s.ptr)
if result == -1 {
......
......@@ -143,6 +143,14 @@ func (p *StoragePool) Free() error {
return nil
}
func (c *StoragePool) Ref() error {
ret := C.virStoragePoolRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (p *StoragePool) GetAutostart() (bool, error) {
var out C.int
result := C.virStoragePoolGetAutostart(p.ptr, (*C.int)(unsafe.Pointer(&out)))
......
......@@ -127,6 +127,14 @@ func (v *StorageVol) Free() error {
return nil
}
func (c *StorageVol) Ref() error {
ret := C.virStorageVolRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (v *StorageVol) GetInfo() (*StorageVolInfo, error) {
var cinfo C.virStorageVolInfo
result := C.virStorageVolGetInfo(v.ptr, &cinfo)
......
......@@ -85,6 +85,14 @@ func (v *Stream) Free() error {
return nil
}
func (c *Stream) Ref() error {
ret := C.virStreamRef(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}
func (v *Stream) Recv(p []byte) (int, error) {
n := C.virStreamRecv(v.ptr, (*C.char)(unsafe.Pointer(&p[0])), C.size_t(len(p)))
if n < 0 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment