Files
sqlite3/vfs/shm_dotlk.go

183 lines
3.4 KiB
Go
Raw Permalink Normal View History

2024-10-31 15:21:15 +00:00
//go:build sqlite3_dotlk
package vfs
import (
"context"
2024-11-05 17:30:10 +00:00
"errors"
"io/fs"
2024-10-31 15:21:15 +00:00
"sync"
"github.com/tetratelabs/wazero/api"
2024-11-27 11:42:25 +00:00
2024-12-17 14:21:56 +00:00
"github.com/ncruces/go-sqlite3/internal/dotlk"
2024-11-27 11:42:25 +00:00
"github.com/ncruces/go-sqlite3/internal/util"
2024-10-31 15:21:15 +00:00
)
2024-11-07 12:18:42 +00:00
type vfsShmParent struct {
2024-11-06 00:30:37 +00:00
shared [][_WALINDEX_PGSZ]byte
2024-11-07 12:18:42 +00:00
refs int // +checklocks:vfsShmListMtx
2024-10-31 15:21:15 +00:00
2024-12-18 16:21:24 +00:00
lock [_SHM_NLOCK]int8 // +checklocks:Mutex
2024-10-31 15:21:15 +00:00
sync.Mutex
}
var (
2024-11-07 12:18:42 +00:00
// +checklocks:vfsShmListMtx
vfsShmList = map[string]*vfsShmParent{}
vfsShmListMtx sync.Mutex
2024-10-31 15:21:15 +00:00
)
type vfsShm struct {
2024-11-07 12:18:42 +00:00
*vfsShmParent
2024-11-05 17:30:10 +00:00
mod api.Module
alloc api.Function
free api.Function
path string
2024-11-06 00:30:37 +00:00
shadow [][_WALINDEX_PGSZ]byte
2025-01-21 01:42:57 +00:00
ptrs []ptr_t
stack [1]stk_t
2024-11-05 17:30:10 +00:00
lock [_SHM_NLOCK]bool
2024-10-31 15:21:15 +00:00
}
func (s *vfsShm) Close() error {
2024-11-07 12:18:42 +00:00
if s.vfsShmParent == nil {
2024-10-31 15:21:15 +00:00
return nil
}
2024-11-07 12:18:42 +00:00
vfsShmListMtx.Lock()
defer vfsShmListMtx.Unlock()
2024-10-31 15:21:15 +00:00
// Unlock everything.
s.shmLock(0, _SHM_NLOCK, _SHM_UNLOCK)
// Decrease reference count.
2024-11-07 12:18:42 +00:00
if s.vfsShmParent.refs > 0 {
s.vfsShmParent.refs--
s.vfsShmParent = nil
2024-10-31 15:21:15 +00:00
return nil
}
2024-12-17 14:21:56 +00:00
if err := dotlk.Unlock(s.path); err != nil {
2025-10-15 16:22:36 +01:00
return sysError{err, _IOERR_UNLOCK}
2024-11-05 17:30:10 +00:00
}
2024-11-07 12:18:42 +00:00
delete(vfsShmList, s.path)
s.vfsShmParent = nil
2024-10-31 15:21:15 +00:00
return nil
}
2025-10-17 16:40:15 +01:00
func (s *vfsShm) shmOpen() error {
2024-11-07 12:18:42 +00:00
if s.vfsShmParent != nil {
2025-10-17 16:40:15 +01:00
return nil
2024-10-31 15:21:15 +00:00
}
2024-11-07 12:18:42 +00:00
vfsShmListMtx.Lock()
defer vfsShmListMtx.Unlock()
2024-10-31 15:21:15 +00:00
// Find a shared buffer, increase the reference count.
2024-11-07 12:18:42 +00:00
if g, ok := vfsShmList[s.path]; ok {
s.vfsShmParent = g
2024-10-31 15:21:15 +00:00
g.refs++
2025-10-17 16:40:15 +01:00
return nil
2024-11-05 17:30:10 +00:00
}
2024-12-17 14:21:56 +00:00
// Dead man's switch.
err := dotlk.LockShm(s.path)
2024-11-05 17:30:10 +00:00
if errors.Is(err, fs.ErrExist) {
return _BUSY
}
if err != nil {
2025-10-17 16:40:15 +01:00
return sysError{err, _IOERR_LOCK}
2024-10-31 15:21:15 +00:00
}
// Add the new shared buffer.
2024-11-07 12:18:42 +00:00
s.vfsShmParent = &vfsShmParent{}
vfsShmList[s.path] = s.vfsShmParent
2025-10-17 16:40:15 +01:00
return nil
2024-10-31 15:21:15 +00:00
}
2025-10-15 16:22:36 +01:00
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (ptr_t, error) {
2024-10-31 15:21:15 +00:00
if size != _WALINDEX_PGSZ {
return 0, _IOERR_SHMMAP
}
if s.mod == nil {
s.mod = mod
s.free = mod.ExportedFunction("sqlite3_free")
s.alloc = mod.ExportedFunction("sqlite3_malloc64")
}
2025-10-17 16:40:15 +01:00
if err := s.shmOpen(); err != nil {
return 0, err
2024-11-05 17:30:10 +00:00
}
2024-10-31 15:21:15 +00:00
s.Lock()
defer s.Unlock()
2024-11-08 13:02:19 +00:00
defer s.shmAcquire(nil)
2024-10-31 15:21:15 +00:00
2024-11-06 11:45:14 +00:00
// Extend shared memory.
2024-11-06 00:30:37 +00:00
if int(id) >= len(s.shared) {
2024-10-31 15:21:15 +00:00
if !extend {
2025-10-15 16:22:36 +01:00
return 0, nil
2024-10-31 15:21:15 +00:00
}
2024-11-06 00:30:37 +00:00
s.shared = append(s.shared, make([][_WALINDEX_PGSZ]byte, int(id)-len(s.shared)+1)...)
2024-10-31 15:21:15 +00:00
}
2024-11-06 11:45:14 +00:00
// Allocate shadow memory.
2024-11-06 00:30:37 +00:00
if int(id) >= len(s.shadow) {
s.shadow = append(s.shadow, make([][_WALINDEX_PGSZ]byte, int(id)-len(s.shadow)+1)...)
2024-10-31 15:21:15 +00:00
}
2024-11-06 11:45:14 +00:00
// Allocate local memory.
2024-10-31 15:21:15 +00:00
for int(id) >= len(s.ptrs) {
2025-01-21 01:42:57 +00:00
s.stack[0] = stk_t(size)
2024-10-31 15:21:15 +00:00
if err := s.alloc.CallWithStack(ctx, s.stack[:]); err != nil {
panic(err)
}
if s.stack[0] == 0 {
panic(util.OOMErr)
}
2025-01-21 01:42:57 +00:00
clear(util.View(s.mod, ptr_t(s.stack[0]), _WALINDEX_PGSZ))
s.ptrs = append(s.ptrs, ptr_t(s.stack[0]))
2024-10-31 15:21:15 +00:00
}
2024-11-08 13:02:19 +00:00
s.shadow[0][4] = 1
2025-10-15 16:22:36 +01:00
return s.ptrs[id], nil
2024-10-31 15:21:15 +00:00
}
2025-10-15 16:22:36 +01:00
func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) (err error) {
if s.vfsShmParent == nil {
return _IOERR_SHMLOCK
}
2024-10-31 15:21:15 +00:00
s.Lock()
defer s.Unlock()
2024-11-02 11:02:38 +00:00
switch {
case flags&_SHM_LOCK != 0:
2025-10-15 16:22:36 +01:00
defer s.shmAcquire(&err)
2024-11-02 11:02:38 +00:00
case flags&_SHM_EXCLUSIVE != 0:
2024-10-31 15:21:15 +00:00
s.shmRelease()
}
2024-11-07 12:18:42 +00:00
return s.shmMemLock(offset, n, flags)
2024-10-31 15:21:15 +00:00
}
func (s *vfsShm) shmUnmap(delete bool) {
2024-11-07 12:18:42 +00:00
if s.vfsShmParent == nil {
2024-10-31 15:21:15 +00:00
return
}
defer s.Close()
s.Lock()
s.shmRelease()
defer s.Unlock()
for _, p := range s.ptrs {
2025-01-21 01:42:57 +00:00
s.stack[0] = stk_t(p)
2024-10-31 15:21:15 +00:00
if err := s.free.CallWithStack(context.Background(), s.stack[:]); err != nil {
panic(err)
}
}
s.ptrs = nil
s.shadow = nil
}