2 * This file is part of the libvirt-go project
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
22 * Copyright (c) 2013 Alex Zorin
23 * Copyright (C) 2016 Red Hat, Inc.
29 // Helpers functions to register a Go callback function to a C
30 // function. For a simple example, look at how SetErrorFunc works in
33 // - Create a struct that will contain at least the Go callback to
34 // invoke (errorContext).
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.
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
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.
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.
57 // - When unregistering the callback, don't forget to call freecallbackId.
59 // If you need to associate some additional data with the connection,
60 // look at saveConnectionData, getConnectionData and
61 // releaseConnectionData.
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
74 //export freeCallbackId
75 func freeCallbackId(goCallbackId int) {
77 delete(goCallbacks, goCallbackId)
78 goCallbackLock.Unlock()
81 func getCallbackId(goCallbackId int) interface{} {
82 goCallbackLock.RLock()
83 ctx := goCallbacks[goCallbackId]
84 goCallbackLock.RUnlock()
86 // If this happens there must be a bug in libvirt
87 panic("Callback arrived after freeCallbackId was called")
92 func registerCallbackId(ctx interface{}) int {
94 goCallBackId := nextGoCallbackId
96 for goCallbacks[nextGoCallbackId] != nil {
99 goCallbacks[goCallBackId] = ctx
100 goCallbackLock.Unlock()