diff options
author | Jan Mercl <0xjnml@gmail.com> | 2019-12-19 15:58:58 +0100 |
---|---|---|
committer | Jan Mercl <0xjnml@gmail.com> | 2019-12-19 15:58:58 +0100 |
commit | 58026126c2d182c9a75f5b98d263f1733ddd9dd3 (patch) | |
tree | a014f3258f1b07721a7016222fd3fc0e99672bbd /sqlite.go | |
parent | 179c16d747dbaad875f20b52af14a4f95357003c (diff) |
support SQLite's multi-thread model
Diffstat (limited to 'sqlite.go')
-rw-r--r-- | sqlite.go | 20 |
1 files changed, 15 insertions, 5 deletions
@@ -40,10 +40,24 @@ const ( func init() { tls := crt.NewTLS() - if bin.Xsqlite3_threadsafe(tls) != 0 { //TODO implement mutexes + if bin.Xsqlite3_threadsafe(tls) == 0 { panic(fmt.Errorf("sqlite: thread safety configuration error")) } + varArgs := crt.Xmalloc(tls, crt.Intptr(ptrSize)) + if varArgs == 0 { + panic(fmt.Errorf("cannot allocate memory")) + } + + *(*uintptr)(unsafe.Pointer(uintptr(varArgs))) = uintptr(unsafe.Pointer(&mutexMethods)) + // int sqlite3_config(int, ...); + if rc := bin.Xsqlite3_config(tls, bin.DSQLITE_CONFIG_MUTEX, uintptr(varArgs)); rc != bin.DSQLITE_OK { + p := bin.Xsqlite3_errstr(tls, rc) + str := crt.GoString(p) + panic(fmt.Errorf("sqlite: failed to configure mutex methods: %v", str)) + } + + crt.Xfree(tls, varArgs) sql.Register(driverName, newDrv()) } @@ -726,10 +740,6 @@ func newConn(s *Driver, name string) (_ *conn, err error) { } }() - c.Lock() - - defer c.Unlock() - c.tls = crt.NewTLS() if err = c.openV2( name, |