-
Notifications
You must be signed in to change notification settings - Fork 17
/
exec.go
75 lines (64 loc) · 1.21 KB
/
exec.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
/*
#include "postgres.h"
#include "common.h"
typedef struct GoFdwExecutionState
{
uint tok;
} GoFdwExecutionState;
static inline GoFdwExecutionState* makeState(){
GoFdwExecutionState *s = (GoFdwExecutionState *) malloc(sizeof(GoFdwExecutionState));
return s;
}
static inline void freeState(GoFdwExecutionState * s){ if (s) free(s); }
*/
import "C"
import (
"sync"
"unsafe"
"github.com/turbot/steampipe-postgres-fdw/hub"
"github.com/turbot/steampipe-postgres-fdw/types"
)
type ExecState struct {
Rel *types.Relation
Opts map[string]string
Iter hub.Iterator
State *C.FdwExecState
}
var (
mu sync.RWMutex
si uint64
sess = make(map[uint64]*ExecState)
)
func SaveExecState(s *ExecState) unsafe.Pointer {
mu.Lock()
si++
i := si
sess[i] = s
mu.Unlock()
cs := C.makeState()
cs.tok = C.uint(i)
return unsafe.Pointer(cs)
}
func ClearExecState(p unsafe.Pointer) {
if p == nil {
return
}
cs := (*C.GoFdwExecutionState)(p)
i := uint64(cs.tok)
mu.Lock()
delete(sess, i)
mu.Unlock()
C.freeState(cs)
}
func GetExecState(p unsafe.Pointer) *ExecState {
if p == nil {
return nil
}
cs := (*C.GoFdwExecutionState)(p)
i := uint64(cs.tok)
mu.RLock()
s := sess[i]
mu.RUnlock()
return s
}