File: msgfunc.go

package info (click to toggle)
golang-github-bmatsuo-lmdb-go 1.8.0%2Bgit20170215.a14b5a3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 840 kB
  • sloc: ansic: 8,247; makefile: 19
file content (82 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package lmdb

/*
#include "lmdbgo.h"
*/
import "C"
import (
	"sync"
	"sync/atomic"
)

// lmdbgoMDBMsgFuncBridge provides a static C function for handling MDB_msgfunc
// callbacks.  It performs string conversion and dynamic dispatch to a msgfunc
// provided to Env.ReaderList.  Any error returned by the msgfunc is cached and
// -1 is returned to terminate the iteration.

//export lmdbgoMDBMsgFuncBridge
func lmdbgoMDBMsgFuncBridge(cmsg C.lmdbgo_ConstCString, _ctx C.size_t) C.int {
	ctx := msgctx(_ctx).get()
	msg := C.GoString(cmsg.p)
	err := ctx.fn(msg)
	if err != nil {
		ctx.err = err
		return -1
	}
	return 0
}

type msgfunc func(string) error

// msgctx is the type used for context pointers passed to mdb_reader_list.  A
// msgctx stores its corresponding msgfunc, and any error encountered in an
// external map.  The corresponding function is called once for each
// mdb_reader_list entry using the msgctx.
//
// An External map is used because struct pointers passed to C functions must
// not contain pointers in their struct fields.  See the following language
// proposal which discusses the restrictions on passing pointers to C.
//
//		https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md
type msgctx uintptr
type _msgctx struct {
	fn  msgfunc
	err error
}

var msgctxn uint32
var msgctxm = map[msgctx]*_msgctx{}
var msgctxmlock sync.RWMutex

func nextctx() msgctx {
	return msgctx(atomic.AddUint32(&msgctxn, 1))
}

func newMsgFunc(fn msgfunc) (ctx msgctx, done func()) {
	ctx = nextctx()
	ctx.register(fn)
	return ctx, ctx.deregister
}

func (ctx msgctx) register(fn msgfunc) {
	msgctxmlock.Lock()
	if _, ok := msgctxm[ctx]; ok {
		msgctxmlock.Unlock()
		panic("msgfunc conflict")
	}
	msgctxm[ctx] = &_msgctx{fn: fn}
	msgctxmlock.Unlock()
}

func (ctx msgctx) deregister() {
	msgctxmlock.Lock()
	delete(msgctxm, ctx)
	msgctxmlock.Unlock()
}

func (ctx msgctx) get() *_msgctx {
	msgctxmlock.RLock()
	_ctx := msgctxm[ctx]
	msgctxmlock.RUnlock()
	return _ctx
}