Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect global variables with locks #39

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions internal/bindings/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import (
"fmt"
"net"
"os"
"sync"
"time"
"unsafe"

Expand Down Expand Up @@ -197,6 +198,8 @@ func (s *Server) Close() {

// SetDialFunc configure a custom dial function.
func (s *Server) SetDialFunc(dial DialFunc) {
connectLock.Lock()
defer connectLock.Unlock()
server := (*C.dqlite)(unsafe.Pointer(s))
connectIndex++
// TODO: unregister when destroying the server.
Expand All @@ -206,6 +209,8 @@ func (s *Server) SetDialFunc(dial DialFunc) {

// SetLogFunc configure a custom log function.
func (s *Server) SetLogFunc(log LogFunc) {
logLock.Lock()
defer logLock.Unlock()
server := (*C.dqlite)(unsafe.Pointer(s))
logIndex++
// TODO: unregister when destroying the server.
Expand Down Expand Up @@ -364,6 +369,8 @@ func (s *Server) Stop() error {

//export connectWithDial
func connectWithDial(handle C.uintptr_t, server *C.dqlite_server, fd *C.int) C.int {
connectLock.Lock()
defer connectLock.Unlock()
dial := connectRegistry[handle]
// TODO: make timeout customizable.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand Down Expand Up @@ -404,21 +411,14 @@ func convertState(state C.int) int {
}
}

//export triggerWatch
func triggerWatch(handle C.uintptr_t, oldState C.int, newState C.int) {
watch := watchRegistry[handle]
watch(convertState(oldState), convertState(newState))
}

// Use handles to avoid passing Go pointers to C.
var connectRegistry = make(map[C.uintptr_t]DialFunc)
var connectIndex C.uintptr_t = 100
var connectLock = sync.Mutex{}

var logRegistry = make(map[C.uintptr_t]LogFunc)
var logIndex C.uintptr_t = 100

var watchRegistry = make(map[C.uintptr_t]WatchFunc)
var watchIndex C.uintptr_t = 100
var logLock = sync.Mutex{}

// ErrServerStopped is returned by Server.Handle() is the server was stopped.
var ErrServerStopped = fmt.Errorf("server was stopped")
Expand Down