diff --git a/vfs/api.go b/vfs/api.go index e133e8b..3d4a2ad 100644 --- a/vfs/api.go +++ b/vfs/api.go @@ -171,5 +171,11 @@ type SharedMemory interface { shmMap(context.Context, api.Module, int32, int32, bool) (uint32, _ErrorCode) shmLock(int32, int32, _ShmFlag) _ErrorCode shmUnmap(bool) + shmBarrier() io.Closer } + +type blockingSharedMemory interface { + SharedMemory + shmEnableBlocking(block bool) +} diff --git a/vfs/shm.go b/vfs/shm.go index 88306ef..c8163cd 100644 --- a/vfs/shm.go +++ b/vfs/shm.go @@ -6,6 +6,7 @@ import ( "context" "io" "os" + "sync" "time" "github.com/ncruces/go-sqlite3/internal/util" @@ -45,12 +46,15 @@ func NewSharedMemory(path string, flags OpenFlag) SharedMemory { } } +var _ blockingSharedMemory = &vfsShm{} + type vfsShm struct { *os.File path string regions []*util.MappedRegion readOnly bool blocking bool + sync.Mutex } func (s *vfsShm) shmOpen() _ErrorCode { @@ -196,6 +200,11 @@ func (s *vfsShm) shmUnmap(delete bool) { s.File = nil } +func (s *vfsShm) shmBarrier() { + s.Lock() + s.Unlock() +} + func (s *vfsShm) shmEnableBlocking(block bool) { s.blocking = block } diff --git a/vfs/shm_bsd.go b/vfs/shm_bsd.go index 8849cc7..dde90db 100644 --- a/vfs/shm_bsd.go +++ b/vfs/shm_bsd.go @@ -269,3 +269,8 @@ func (s *vfsShm) shmUnmap(delete bool) { } s.Close() } + +func (s *vfsShm) shmBarrier() { + s.lockMtx.Lock() + s.lockMtx.Unlock() +} diff --git a/vfs/vfs.go b/vfs/vfs.go index eb606bf..8235490 100644 --- a/vfs/vfs.go +++ b/vfs/vfs.go @@ -5,7 +5,6 @@ import ( "crypto/rand" "io" "reflect" - "sync" "time" "github.com/ncruces/go-sqlite3/internal/util" @@ -245,10 +244,9 @@ func vfsFileControl(ctx context.Context, mod api.Module, pFile uint32, op _Fcntl case _FCNTL_LOCK_TIMEOUT: if file, ok := file.(FileSharedMemory); ok { - if iface, ok := file.SharedMemory().(interface{ shmEnableBlocking(bool) }); ok { - if i := util.ReadUint32(mod, pArg); i == 0 || i == 1 { - iface.shmEnableBlocking(i != 0) - } + if shm, ok := file.SharedMemory().(blockingSharedMemory); ok { + shm.shmEnableBlocking(util.ReadUint32(mod, pArg) != 0) + return _OK } } @@ -385,11 +383,9 @@ func vfsDeviceCharacteristics(ctx context.Context, mod api.Module, pFile uint32) return file.DeviceCharacteristics() } -var shmBarrier sync.Mutex - func vfsShmBarrier(ctx context.Context, mod api.Module, pFile uint32) { - shmBarrier.Lock() - defer shmBarrier.Unlock() + shm := vfsFileGet(ctx, mod, pFile).(FileSharedMemory).SharedMemory() + shm.shmBarrier() } func vfsShmMap(ctx context.Context, mod api.Module, pFile uint32, iRegion, szRegion int32, bExtend, pp uint32) _ErrorCode {