src: Add DMA localagent
[barometer.git] / src / dma / vendor / github.com / libvirt / libvirt-go / callbacks.go
1 /*
2  * This file is part of the libvirt-go project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  *
22  * Copyright (c) 2013 Alex Zorin
23  * Copyright (C) 2016 Red Hat, Inc.
24  *
25  */
26
27 package libvirt
28
29 // Helpers functions to register a Go callback function to a C
30 // function. For a simple example, look at how SetErrorFunc works in
31 // error.go.
32 //
33 // - Create a struct that will contain at least the Go callback to
34 //   invoke (errorContext).
35 //
36 // - Create an exported Golang function whose job will be to retrieve
37 //   the context and execute the callback in it
38 //   (connErrCallback). Such a function should receive a callback ID
39 //   and will use it to retrive the context.
40 //
41 // - Create a CGO function similar to the above function but with the
42 //   appropriate signature to be registered as a callback in C code
43 //   (connErrCallbackHelper). Notably, it will have a void* argument
44 //   that should be cast to long to retrieve the callback ID. It
45 //   should be just a thin wrapper to transform the opaque argument to
46 //   a callback ID.
47 //
48 // - Create a CGO function which will be a wrapper around the C
49 //   function to register the callback (virConnSetErrorFuncWrapper). Its
50 //   only role is to transform a callback ID (long) to an opaque (void*)
51 //   and call the C function.
52 //
53 // - When setting up a callback (SetErrorFunc), register the struct from first step
54 //   with registerCallbackId and invoke the CGO function from the
55 //   previous step with the appropriate ID.
56 //
57 // - When unregistering the callback, don't forget to call freecallbackId.
58 //
59 // If you need to associate some additional data with the connection,
60 // look at saveConnectionData, getConnectionData and
61 // releaseConnectionData.
62
63 import "C"
64
65 import (
66         "sync"
67 )
68
69 const firstGoCallbackId int = 100 // help catch some additional errors during test
70 var goCallbackLock sync.RWMutex
71 var goCallbacks = make(map[int]interface{})
72 var nextGoCallbackId int = firstGoCallbackId
73
74 //export freeCallbackId
75 func freeCallbackId(goCallbackId int) {
76         goCallbackLock.Lock()
77         delete(goCallbacks, goCallbackId)
78         goCallbackLock.Unlock()
79 }
80
81 func getCallbackId(goCallbackId int) interface{} {
82         goCallbackLock.RLock()
83         ctx := goCallbacks[goCallbackId]
84         goCallbackLock.RUnlock()
85         if ctx == nil {
86                 // If this happens there must be a bug in libvirt
87                 panic("Callback arrived after freeCallbackId was called")
88         }
89         return ctx
90 }
91
92 func registerCallbackId(ctx interface{}) int {
93         goCallbackLock.Lock()
94         goCallBackId := nextGoCallbackId
95         nextGoCallbackId++
96         for goCallbacks[nextGoCallbackId] != nil {
97                 nextGoCallbackId++
98         }
99         goCallbacks[goCallBackId] = ctx
100         goCallbackLock.Unlock()
101         return goCallBackId
102 }