Skip to content

Commit

Permalink
Merge pull request #152 from dyedgreen/149-use-unstable-locking-api
Browse files Browse the repository at this point in the history
Add file locking to deno VFS
  • Loading branch information
dyedgreen authored Nov 11, 2021
2 parents f366245 + d9fbf4f commit b33cbd1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ jobs:
make clean
- name: Run tests
run: /home/runner/.deno/bin/deno test --allow-read --allow-write test.ts
- name: Run tests (unstable)
run: /home/runner/.deno/bin/deno test --unstable --allow-read --allow-write test.ts
- name: Run benchmarks
run: /home/runner/.deno/bin/deno run bench.ts
12 changes: 6 additions & 6 deletions build/size.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sqlite.d.ts ➜ 2058 bytes
sqlite.js ➜ 894808 bytes
sqlite.js.br ➜ 299743 bytes
sqlite.js.gz ➜ 364032 bytes
sqlite.wasm ➜ 670595 bytes
sqlite.wasm.br ➜ 237161 bytes
sqlite.wasm.gz ➜ 278362 bytes
sqlite.js ➜ 894972 bytes
sqlite.js.br ➜ 300076 bytes
sqlite.js.gz ➜ 364271 bytes
sqlite.wasm ➜ 670718 bytes
sqlite.wasm.br ➜ 237016 bytes
sqlite.wasm.gz ➜ 278993 bytes
2 changes: 1 addition & 1 deletion build/sqlite.js

Large diffs are not rendered by default.

Binary file modified build/sqlite.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions build/src/imports.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extern int js_write(int, const char*, double, int);
extern void js_truncate(int, double);
extern void js_sync(int);
extern double js_size(int);
extern void js_lock(int, int);
extern void js_unlock(int);
extern double js_time();
extern int js_timezone();
extern int js_exists(const char*);
Expand Down
33 changes: 29 additions & 4 deletions build/src/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,42 @@ static int denoFileSize(sqlite3_file *pFile, sqlite_int64 *pSize) {
return SQLITE_OK;
}

// Deno does not support file locks.
// File locking
static int denoLock(sqlite3_file *pFile, int eLock) {
debug_printf("no-op call to lock");
DenoFile *p = (DenoFile*)pFile;
switch (eLock) {
case SQLITE_LOCK_NONE:
// no op
break;
case SQLITE_LOCK_SHARED:
case SQLITE_LOCK_RESERVED: // one WASM process <-> one open database
js_lock(p->rid, 0);
break;
case SQLITE_LOCK_PENDING:
case SQLITE_LOCK_EXCLUSIVE:
js_lock(p->rid, 1);
break;
}
return SQLITE_OK;
}

static int denoUnlock(sqlite3_file *pFile, int eLock) {
debug_printf("no-op call to unlock");
DenoFile *p = (DenoFile*)pFile;
switch (eLock) {
case SQLITE_LOCK_NONE:
// no op
break;
case SQLITE_LOCK_SHARED:
case SQLITE_LOCK_RESERVED:
case SQLITE_LOCK_PENDING:
case SQLITE_LOCK_EXCLUSIVE:
js_unlock(p->rid);
break;
}
return SQLITE_OK;
}

static int denoCheckReservedLock(sqlite3_file *pFile, int *pResOut) {
debug_printf("no-op call to check reserved lock");
*pResOut = 0;
return SQLITE_OK;
}
Expand Down
10 changes: 10 additions & 0 deletions build/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ export default function env(inst) {
js_size: (rid) => {
return Deno.fstatSync(rid).size;
},
// Acquire a SHARED or EXCLUSIVE file lock
js_lock: (rid, exclusive) => {
// this is unstable ...
if (Deno.flockSync) Deno.flockSync(rid, exclusive !== 0);
},
// Release a file lock
js_unlock: (rid) => {
// this is unstable ...
if (Deno.funlockSync) Deno.funlockSync(rid);
},
// Return current time in ms since UNIX epoch
js_time: () => {
return Date.now();
Expand Down
2 changes: 2 additions & 0 deletions build/vfs.syms
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ js_write
js_truncate
js_sync
js_size
js_lock
js_unlock
js_time
js_timezone
js_exists
Expand Down

0 comments on commit b33cbd1

Please sign in to comment.