-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
sqlite3_opt_unlock_notify.go
53 lines (43 loc) · 1.15 KB
/
sqlite3_opt_unlock_notify.go
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
// Copyright (C) 2018 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// +build cgo
// +build sqlite_unlock_notify
package sqlite3
/*
#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY
#include <sqlite3-binding.h>
extern void _unlock_notify_callback(void *arg, int argc);
*/
import "C"
import (
"sync"
"unsafe"
)
type unlockNotification struct {
notify chan struct{}
lock sync.Mutex
}
//export unlock_notify_callback
func unlock_notify_callback(pargv unsafe.Pointer, argc C.int) {
argv := *(*uintptr)(pargv)
v := (*[1 << 30]uintptr)(unsafe.Pointer(argv))
for i := 0; i < int(argc); i++ {
un := lookupHandle(v[i]).(unlockNotification)
un.notify <- struct{}{}
}
}
var notifyMutex sync.Mutex
//export unlock_notify_wait
func unlock_notify_wait(db *C.sqlite3) C.int {
var un unlockNotification
un.notify = make(chan struct{})
defer close(un.notify)
argv := [1]uintptr{newHandle(nil, un)}
if rv := C.sqlite3_unlock_notify(db, (*[0]byte)(C._unlock_notify_callback), unsafe.Pointer(&argv)); rv != C.SQLITE_OK {
return rv
}
<-un.notify
return C.SQLITE_OK
}