aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/sqlite3-opfs-async-proxy.js
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-11-10 11:35:10 +0000
committerstephan <stephan@noemail.net>2022-11-10 11:35:10 +0000
commitaafa022f5b5371d83ee3478c17e0c7f2faf1ce62 (patch)
treeee1cfed1ad1cd869c8d5561f4a92e3df68521456 /ext/wasm/api/sqlite3-opfs-async-proxy.js
parent838865c0bb3531a4105567dfc1f6623c6aa1cd5f (diff)
downloadsqlite-aafa022f5b5371d83ee3478c17e0c7f2faf1ce62.tar.gz
sqlite-aafa022f5b5371d83ee3478c17e0c7f2faf1ce62.zip
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
Diffstat (limited to 'ext/wasm/api/sqlite3-opfs-async-proxy.js')
-rw-r--r--ext/wasm/api/sqlite3-opfs-async-proxy.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/ext/wasm/api/sqlite3-opfs-async-proxy.js b/ext/wasm/api/sqlite3-opfs-async-proxy.js
index 09c56ff1d..9c0bf4cbe 100644
--- a/ext/wasm/api/sqlite3-opfs-async-proxy.js
+++ b/ext/wasm/api/sqlite3-opfs-async-proxy.js
@@ -215,6 +215,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();