Skip to content

Commit b19bd28

Browse files
committed
Simplify lock timeouts.
1 parent e66bd51 commit b19bd28

File tree

7 files changed

+18
-44
lines changed

7 files changed

+18
-44
lines changed

embed/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Embeddable WASM build of SQLite
22

3-
This folder includes an embeddable WASM build of SQLite 3.43.0 for use with
3+
This folder includes an embeddable WASM build of SQLite 3.43.1 for use with
44
[`github.com/ncruces/go-sqlite3`](https://pkg.go.dev/github.com/ncruces/go-sqlite3).
55

66
The following optional features are compiled in:

vfs/file.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"path/filepath"
1010
"runtime"
1111
"syscall"
12-
"time"
1312
)
1413

1514
type vfsOS struct{}
@@ -124,11 +123,10 @@ func (vfsOS) OpenParams(name string, flags OpenFlag, params url.Values) (File, O
124123

125124
type vfsFile struct {
126125
*os.File
127-
lockTimeout time.Duration
128-
lock LockLevel
129-
psow bool
130-
syncDir bool
131-
readOnly bool
126+
lock LockLevel
127+
psow bool
128+
syncDir bool
129+
readOnly bool
132130
}
133131

134132
var (

vfs/lock.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package vfs
44

55
import (
66
"os"
7-
"time"
87

98
"github.com/ncruces/go-sqlite3/internal/util"
109
)
@@ -50,7 +49,7 @@ func (f *vfsFile) Lock(lock LockLevel) error {
5049
if f.lock != LOCK_NONE {
5150
panic(util.AssertErr())
5251
}
53-
if rc := osGetSharedLock(f.File, f.lockTimeout); rc != _OK {
52+
if rc := osGetSharedLock(f.File); rc != _OK {
5453
return rc
5554
}
5655
f.lock = LOCK_SHARED
@@ -61,7 +60,7 @@ func (f *vfsFile) Lock(lock LockLevel) error {
6160
if f.lock != LOCK_SHARED {
6261
panic(util.AssertErr())
6362
}
64-
if rc := osGetReservedLock(f.File, f.lockTimeout); rc != _OK {
63+
if rc := osGetReservedLock(f.File); rc != _OK {
6564
return rc
6665
}
6766
f.lock = LOCK_RESERVED
@@ -79,7 +78,7 @@ func (f *vfsFile) Lock(lock LockLevel) error {
7978
}
8079
f.lock = LOCK_PENDING
8180
}
82-
if rc := osGetExclusiveLock(f.File, f.lockTimeout); rc != _OK {
81+
if rc := osGetExclusiveLock(f.File); rc != _OK {
8382
return rc
8483
}
8584
f.lock = LOCK_EXCLUSIVE
@@ -136,9 +135,9 @@ func (f *vfsFile) CheckReservedLock() (bool, error) {
136135
return osCheckReservedLock(f.File)
137136
}
138137

139-
func osGetReservedLock(file *os.File, timeout time.Duration) _ErrorCode {
138+
func osGetReservedLock(file *os.File) _ErrorCode {
140139
// Acquire the RESERVED lock.
141-
return osWriteLock(file, _RESERVED_BYTE, 1, timeout)
140+
return osWriteLock(file, _RESERVED_BYTE, 1, 0)
142141
}
143142

144143
func osGetPendingLock(file *os.File) _ErrorCode {

vfs/lock_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,4 @@ func Test_vfsLock(t *testing.T) {
203203
if got := util.ReadUint32(mod, pOutput); got != uint32(LOCK_SHARED) {
204204
t.Error("invalid lock state", got)
205205
}
206-
207-
rc = vfsFileControl(ctx, mod, pFile1, _FCNTL_LOCK_TIMEOUT, 1)
208-
if rc != _OK {
209-
t.Fatal("returned", rc)
210-
}
211206
}

vfs/os_unix.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,18 @@ func osSetMode(file *os.File, modeof string) error {
3333
return nil
3434
}
3535

36-
func osGetSharedLock(file *os.File, timeout time.Duration) _ErrorCode {
36+
func osGetSharedLock(file *os.File) _ErrorCode {
3737
// Test the PENDING lock before acquiring a new SHARED lock.
3838
if pending, _ := osCheckLock(file, _PENDING_BYTE, 1); pending {
3939
return _BUSY
4040
}
4141
// Acquire the SHARED lock.
42-
return osReadLock(file, _SHARED_FIRST, _SHARED_SIZE, timeout)
42+
return osReadLock(file, _SHARED_FIRST, _SHARED_SIZE, 0)
4343
}
4444

45-
func osGetExclusiveLock(file *os.File, timeout time.Duration) _ErrorCode {
46-
if timeout == 0 {
47-
timeout = time.Millisecond
48-
}
49-
45+
func osGetExclusiveLock(file *os.File) _ErrorCode {
5046
// Acquire the EXCLUSIVE lock.
51-
return osWriteLock(file, _SHARED_FIRST, _SHARED_SIZE, timeout)
47+
return osWriteLock(file, _SHARED_FIRST, _SHARED_SIZE, time.Millisecond)
5248
}
5349

5450
func osDowngradeLock(file *os.File, state LockLevel) _ErrorCode {

vfs/os_windows.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ func osOpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
2525
return os.NewFile(uintptr(r), name), nil
2626
}
2727

28-
func osGetSharedLock(file *os.File, timeout time.Duration) _ErrorCode {
28+
func osGetSharedLock(file *os.File) _ErrorCode {
2929
// Acquire the PENDING lock temporarily before acquiring a new SHARED lock.
30-
rc := osReadLock(file, _PENDING_BYTE, 1, timeout)
30+
rc := osReadLock(file, _PENDING_BYTE, 1, 0)
3131

3232
if rc == _OK {
3333
// Acquire the SHARED lock.
@@ -39,16 +39,12 @@ func osGetSharedLock(file *os.File, timeout time.Duration) _ErrorCode {
3939
return rc
4040
}
4141

42-
func osGetExclusiveLock(file *os.File, timeout time.Duration) _ErrorCode {
43-
if timeout == 0 {
44-
timeout = time.Millisecond
45-
}
46-
42+
func osGetExclusiveLock(file *os.File) _ErrorCode {
4743
// Release the SHARED lock.
4844
osUnlock(file, _SHARED_FIRST, _SHARED_SIZE)
4945

5046
// Acquire the EXCLUSIVE lock.
51-
rc := osWriteLock(file, _SHARED_FIRST, _SHARED_SIZE, timeout)
47+
rc := osWriteLock(file, _SHARED_FIRST, _SHARED_SIZE, time.Millisecond)
5248

5349
if rc != _OK {
5450
// Reacquire the SHARED lock.

vfs/vfs.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,6 @@ func vfsFileControl(ctx context.Context, mod api.Module, pFile uint32, op _Fcntl
264264
return _OK
265265
}
266266

267-
case _FCNTL_LOCK_TIMEOUT:
268-
if file, ok := file.(*vfsFile); ok {
269-
millis := file.lockTimeout.Milliseconds()
270-
file.lockTimeout = time.Duration(util.ReadUint32(mod, pArg)) * time.Millisecond
271-
util.WriteUint32(mod, pArg, uint32(millis))
272-
return _OK
273-
}
274-
275267
case _FCNTL_POWERSAFE_OVERWRITE:
276268
if file, ok := file.(FilePowersafeOverwrite); ok {
277269
switch util.ReadUint32(mod, pArg) {
@@ -339,10 +331,8 @@ func vfsFileControl(ctx context.Context, mod api.Module, pFile uint32, op _Fcntl
339331
}
340332

341333
// Consider also implementing these opcodes (in use by SQLite):
342-
// _FCNTL_PDB
343334
// _FCNTL_BUSYHANDLER
344335
// _FCNTL_CHUNK_SIZE
345-
// _FCNTL_OVERWRITE
346336
// _FCNTL_PRAGMA
347337
// _FCNTL_SYNC
348338
return _NOTFOUND

0 commit comments

Comments
 (0)