1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-27 20:41:58 +03:00

OPFS: if an op which needs a lock is called when no lock has been obtained, automatically lock it at the start of the op and unlock it at the end of that op. This is an attempt to alleviate the cross-tab contention described in [forum post 58a377083cd24a|forum:58a377083cd24a] but it increases speedtest1 run time by approximately 4x. Perhaps auto-lock can be combined with the older idle-time-based auto-unlock to unlock such locks (but not those from xLock()) to improve this?

FossilOrigin-Name: 46304ba057707c3b072b6e7bb8c4af774f653aa5814099f0866cd87b2b73abeb
This commit is contained in:
stephan
2022-11-10 11:35:10 +00:00
parent 838865c0bb
commit aafa022f5b
4 changed files with 39 additions and 10 deletions

View File

@ -214,6 +214,24 @@ const closeSyncHandle = async (fh)=>{
}
};
/**
A proxy for closeSyncHandle() which is guaranteed to not throw.
This function is part of a lock/unlock step in functions which
require a sync access handle but may be called without xLock()
having been called first. Such calls need to release that
handle to avoid locking the file for all of time. This is an
_attempt_ at reducing cross-tab contention but it may prove
to be more of a problem than a solution and may need to be
removed.
*/
const closeSyncHandleNoThrow = async (fh)=>{
try{await closeSyncHandle(fh)}
catch(e){
warn("closeSyncHandleNoThrow() ignoring:",e,fh);
}
};
/**
Stores the given value at state.sabOPView[state.opIds.rc] and then
Atomics.notify()'s it.
@ -403,6 +421,7 @@ const vfsAsyncImpls = {
mTimeStart('xFileSize');
const fh = __openFiles[fid];
let rc;
const hadLock = !!fh.syncHandle;
wTimeStart('xFileSize');
try{
affirmLocked('xFileSize',fh);
@ -413,6 +432,7 @@ const vfsAsyncImpls = {
state.s11n.storeException(2,e);
rc = state.sq3Codes.SQLITE_IOERR;
}
if(!hadLock) closeSyncHandleNoThrow(fh);
wTimeEnd();
storeAndNotify('xFileSize', rc);
mTimeEnd();
@ -483,6 +503,7 @@ const vfsAsyncImpls = {
mTimeStart('xRead');
let rc = 0, nRead;
const fh = __openFiles[fid];
const hadLock = !!fh.syncHandle;
try{
affirmLocked('xRead',fh);
wTimeStart('xRead');
@ -501,6 +522,7 @@ const vfsAsyncImpls = {
state.s11n.storeException(1,e);
rc = state.sq3Codes.SQLITE_IOERR_READ;
}
if(!hadLock) closeSyncHandleNoThrow(fh);
storeAndNotify('xRead',rc);
mTimeEnd();
},
@ -525,6 +547,7 @@ const vfsAsyncImpls = {
mTimeStart('xTruncate');
let rc = 0;
const fh = __openFiles[fid];
const hadLock = !!fh.syncHandle;
wTimeStart('xTruncate');
try{
affirmLocked('xTruncate',fh);
@ -535,6 +558,7 @@ const vfsAsyncImpls = {
state.s11n.storeException(2,e);
rc = state.sq3Codes.SQLITE_IOERR_TRUNCATE;
}
if(!hadLock) closeSyncHandleNoThrow(fh);
wTimeEnd();
storeAndNotify('xTruncate',rc);
mTimeEnd();
@ -561,6 +585,7 @@ const vfsAsyncImpls = {
mTimeStart('xWrite');
let rc;
const fh = __openFiles[fid];
const hadLock = !!fh.syncHandle;
wTimeStart('xWrite');
try{
affirmLocked('xWrite',fh);
@ -575,6 +600,7 @@ const vfsAsyncImpls = {
state.s11n.storeException(1,e);
rc = state.sq3Codes.SQLITE_IOERR_WRITE;
}
if(!hadLock) closeSyncHandleNoThrow(fh);
wTimeEnd();
storeAndNotify('xWrite',rc);
mTimeEnd();