]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add a new xShmBarrier method to the VFS - a shared-memory fence operation.
authordrh <drh@noemail.net>
Thu, 20 May 2010 23:51:06 +0000 (23:51 +0000)
committerdrh <drh@noemail.net>
Thu, 20 May 2010 23:51:06 +0000 (23:51 +0000)
Implement the same in both unix and win32.  Use it to make the WAL subsystem
more robust.

FossilOrigin-Name: 1bd011c9fed5ef29fb616b4d0a52df3b82221b1f

14 files changed:
manifest
manifest.uuid
src/os.c
src/os.h
src/os_unix.c
src/os_win.c
src/sqlite.h.in
src/test6.c
src/test_devsym.c
src/test_onefile.c
src/test_osinst.c
src/test_vfs.c
src/wal.c
test/wal2.test

index 52aed1db639398b42a6969f24fd1eeb2e56ab8de..4b9430e5b63f3ca86ac82c40b21823607fc5f173 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,8 +1,8 @@
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
-C Make\suse\sof\sthe\sextra\sinformation\sin\sthe\sWAL\sheader\sand\sframe\sheader\sto\nenhance\srobustness.
-D 2010-05-20T21:21:10
+C Add\sa\snew\sxShmBarrier\smethod\sto\sthe\sVFS\s-\sa\sshared-memory\sfence\soperation.\nImplement\sthe\ssame\sin\sboth\sunix\sand\swin32.\s\sUse\sit\sto\smake\sthe\sWAL\ssubsystem\nmore\srobust.
+D 2010-05-20T23:51:07
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -151,12 +151,12 @@ F src/mutex_os2.c 20477db50cf3817c2f1cd3eb61e5c177e50231db
 F src/mutex_unix.c becb8c4e07616abf84650d3687d62a1461d5d9cd
 F src/mutex_w32.c fb1cf87c5a88b56c7df0d9ddb796ed9641046c3d
 F src/notify.c cbfa66a836da3a51567209636e6a94059c137930
-F src/os.c b994b2f4c9a2c406a20d43b59c22ed39a1e704d1
-F src/os.h 207c26793c9b518aa670480a3a262250dd3c98b1
+F src/os.c 2285265f7e8035ba77e8e8ec93adf3c3c61fc60e
+F src/os.h efcc7f0072ae362b44eab8588b43a943da61504e
 F src/os_common.h a8f95b81eca8a1ab8593d23e94f8a35f35d4078f
 F src/os_os2.c 665876d5eec7585226b0a1cf5e18098de2b2da19
-F src/os_unix.c 6db01eb2c87856f9c9ea40d497941c54ae1b27ff
-F src/os_win.c 3c8cafd2444f26504cf7af0e5f51ea568f88a67d
+F src/os_unix.c 5c53b43c846ff417504e3000d331bd5326bb0b05
+F src/os_win.c 1e44ee84210b59db1e098bbbc66f6dee68e20d5f
 F src/pager.c d3284a6bbedeaa4ef3f5668af309d4381df97618
 F src/pager.h 76466c3a5af56943537f68b1f16567101a0cd1d0
 F src/parse.y ace5c7a125d9f2a410e431ee3209034105045f7e
@@ -171,7 +171,7 @@ F src/resolve.c ac5f1a713cd1ae77f08b83cc69581e11bf5ae6f9
 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697
 F src/select.c c03d8a0565febcde8c6a12c5d77d065fddae889b
 F src/shell.c fd4ccdb37c3b68de0623eb938a649e0990710714
-F src/sqlite.h.in ef766c813d377a0f3f5e6131d0a2667ecd6301c1
+F src/sqlite.h.in a7d351d5ae015179e7ef97a1060c7213b50efd9b
 F src/sqlite3ext.h 69dfb8116af51b84a029cddb3b35062354270c89
 F src/sqliteInt.h c1ca9bed7c963343f90edaf0ec31b8ff4b43fb01
 F src/sqliteLimit.h 196e2f83c3b444c4548fc1874f52f84fdbda40f3
@@ -183,7 +183,7 @@ F src/test2.c 31f1b9d076b4774a22d2605d0af1f34e14a9a7bd
 F src/test3.c 4c21700c73a890a47fc685c1097bfb661346ac94
 F src/test4.c ad03bb987ddedce928f4258c1e7fa4109a73497d
 F src/test5.c cc55900118fa4add8ec9cf69fc4225a4662f76b1
-F src/test6.c 21f6ec70b5142c37846364e7da0580f7a05b0fc0
+F src/test6.c e524e36b10c4cd8fa08899f6245194045e78edbe
 F src/test7.c 3f2d63e4ccf97f8c2cf1a7fa0a3c8e2e2a354e6e
 F src/test8.c f959db9a22d882013b64c92753fa793b2ce3bdea
 F src/test9.c bea1e8cf52aa93695487badedd6e1886c321ea60
@@ -193,7 +193,7 @@ F src/test_backup.c c129c91127e9b46e335715ae2e75756e25ba27de
 F src/test_btree.c 47cd771250f09cdc6e12dda5bc71bc0b3abc96e2
 F src/test_config.c 6210f501d358bde619ae761f06f123529c6ba24f
 F src/test_demovfs.c da81a5f7785bb352bda7911c332a983ec4f17f27
-F src/test_devsym.c 2c5221c047183e081da523bae16ae5d792c4caaf
+F src/test_devsym.c 265e0c6a196e4b9cf880946b031483446a8033c3
 F src/test_func.c 13b582345fb1185a93e46c53310fae8547dcce20
 F src/test_hexio.c 1237f000ec7a491009b1233f5c626ea71bce1ea2
 F src/test_init.c 5d624ffd0409d424cf9adbfe1f056b200270077c
@@ -203,14 +203,14 @@ F src/test_journal.c 51da4dd6118ee843349592fde29429fab84a6243
 F src/test_loadext.c df586c27176e3c2cb2e099c78da67bf14379a56e
 F src/test_malloc.c 2842c922b8e8d992aba722214952204ca025b411
 F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3
-F src/test_onefile.c df4d7858b5cd1dffe92d36ec9dbad11f0037ffd1
-F src/test_osinst.c 33207e761effffc31a867f9d4a8bb1f52fe6ee92
+F src/test_onefile.c 4ce8c753c0240f010f0f2af89604875967d20945
+F src/test_osinst.c 9cac3f764c065a0ef9d341fffae0c29d6373dc82
 F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8
 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0
 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6
 F src/test_tclvar.c f4dc67d5f780707210d6bb0eb6016a431c04c7fa
 F src/test_thread.c aa9919c885a1fe53eafc73492f0898ee6c0a0726
-F src/test_vfs.c 7fc397aa5a52e2bf64fa0b46f3b9f40767150289
+F src/test_vfs.c a2d320ea9afc0d520b68eb4998f789b4f8007bfc
 F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9
 F src/tokenize.c 25ceb0f0a746ea1d0f9553787f3f0a56853cfaeb
 F src/trigger.c 8927588cb9e6d47f933b53bfe74200fbb504100d
@@ -227,7 +227,7 @@ F src/vdbeblob.c 5327132a42a91e8b7acfb60b9d2c3b1c5c863e0e
 F src/vdbemem.c 2a82f455f6ca6f78b59fb312f96054c04ae0ead1
 F src/vdbetrace.c 864cef96919323482ebd9986f2132435115e9cc2
 F src/vtab.c a0f8a40274e4261696ef57aa806de2776ab72cda
-F src/wal.c fd4377f7fe58cd5063b547b7f0f9a220d68335de
+F src/wal.c 2352dd3a2bb271e5c76c9c4407f6590ae5e3768c
 F src/wal.h 434f76f51225bb614e43ccb6bd2341541ba6a06e
 F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f
 F src/where.c 75fee9e255b62f773fcadd1d1f25b6f63ac7a356
@@ -765,7 +765,7 @@ F test/vtab_alter.test 9e374885248f69e251bdaacf480b04a197f125e5
 F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8
 F test/vtab_shared.test 0eff9ce4f19facbe0a3e693f6c14b80711a4222d
 F test/wal.test 90afd254ece957a716751b1c35fac02d6353c2a7
-F test/wal2.test ed0d505726343408b8f88b281dadc95ba0a00ba2
+F test/wal2.test c9f470f7e4f09274a7a4d35dbbae8587115c6e19
 F test/walbak.test e7650a26eb4b8abeca9b145b1af1e63026dde432
 F test/walcrash.test f6d5fb2bb108876f04848720a488065d9deef69f
 F test/walfault.test 98df47444944a6db2161eed5cef71d6c00bcb8c3
@@ -816,14 +816,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P 669706431f186f92fdc0856a6206419a1e843f46
-R 7ce0bc2b62787cca31ab42b34ac1e9ef
+P 9580ecb7e3beb1949a71784a3dcd1823a88e4a9d
+R 6169e39cb99c7a86e9c9e7505a7068ac
 U drh
-Z 335bcf01b684b4b622e461b91e26e309
+Z b1a49e07405386f7203bad3d085615ae
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.6 (GNU/Linux)
 
-iD8DBQFL9afIoxKgR168RlERAma7AJ9zhPYbudAO43Ry1pxCArWXbiP7QwCffSB2
-UDhUaZ/h1EszzPxcYgfA5bo=
-=RESX
+iD8DBQFL9cruoxKgR168RlERAkAFAJ9xRzU75gC9SA4GXt3kPWkhCqvfpACdF9xG
+lxuM43sIwrUxctG6rY11InI=
+=HHWB
 -----END PGP SIGNATURE-----
index ba5b221dc8c196e451b7cbc2f3ed021c94c1d912..767cd5e0f2a5f18bd92d7919aae1c5552df14996 100644 (file)
@@ -1 +1 @@
-9580ecb7e3beb1949a71784a3dcd1823a88e4a9d
\ No newline at end of file
+1bd011c9fed5ef29fb616b4d0a52df3b82221b1f
\ No newline at end of file
index 44848fdbb88e67c3cbbc636b242291c329ae8f00..0593654007bd4f200e14d2b12c11d964e6307274 100644 (file)
--- a/src/os.c
+++ b/src/os.c
@@ -113,6 +113,9 @@ int sqlite3OsShmRelease(sqlite3_file *id){
 int sqlite3OsShmLock(sqlite3_file *id, int desiredLock, int *pGotLock){
   return id->pMethods->xShmLock(id, desiredLock, pGotLock);
 }
+void sqlite3OsShmBarrier(sqlite3_file *id){
+  id->pMethods->xShmBarrier(id);
+}
 int sqlite3OsShmClose(sqlite3_file *id, int deleteFlag){
   return id->pMethods->xShmClose(id, deleteFlag);
 }
index 8a08acc72102918835b969f84cdf6fde5f2de782..ba00c44ed223545827278bd68f25610f59a447db 100644 (file)
--- a/src/os.h
+++ b/src/os.h
@@ -248,6 +248,7 @@ int sqlite3OsShmSize(sqlite3_file *id, int, int*);
 int sqlite3OsShmGet(sqlite3_file *id, int, int*, void volatile**);
 int sqlite3OsShmRelease(sqlite3_file *id);
 int sqlite3OsShmLock(sqlite3_file *id, int, int*);
+void sqlite3OsShmBarrier(sqlite3_file *id);
 int sqlite3OsShmClose(sqlite3_file *id, int);
 
 /* 
index ecdd261d25b16ed07ec2ce3c373389fa682616af..0a302a455c2899b52cfbbfcbcab7b8b846196baf 100644 (file)
@@ -3883,12 +3883,31 @@ static int unixShmLock(
   return rc;
 }
 
+/*
+** Implement a memory barrier or memory fence on shared memory.  
+**
+** All loads and stores begun before the barrier must complete before
+** any load or store begun after the barrier.
+*/
+static void unixShmBarrier(
+  sqlite3_file *fd           /* Database file holding the shared memory */
+){
+#ifdef __GNUC__
+  __sync_synchronize();
+#else
+  unixMutexEnter();
+  unixMutexLeave();
+#endif
+}
+
+
 #else
 # define unixShmOpen    0
 # define unixShmSize    0
 # define unixShmGet     0
 # define unixShmRelease 0
 # define unixShmLock    0
+# define unixShmBarrier 0
 # define unixShmClose   0
 #endif /* #ifndef SQLITE_OMIT_WAL */
 
@@ -3952,6 +3971,7 @@ static const sqlite3_io_methods METHOD = {                                   \
    unixShmGet,                 /* xShmGet */                                 \
    unixShmRelease,             /* xShmRelease */                             \
    unixShmLock,                /* xShmLock */                                \
+   unixShmBarrier,             /* xShmBarrier */                             \
    unixShmClose                /* xShmClose */                               \
 };                                                                           \
 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
index ac82c8d6e6f884804013f3a9da274105a104288b..6e908c38e35bc85a10d66d8b1a313fd234f3e536 100644 (file)
@@ -2078,12 +2078,27 @@ static int winShmLock(
   return rc;
 }
 
+/*
+** Implement a memory barrier or memory fence on shared memory.  
+**
+** All loads and stores begun before the barrier must complete before
+** any load or store begun after the barrier.
+*/
+static void winShmBarrier(
+  sqlite3_file *fd          /* Database holding the shared memory */
+){
+  /* MemoryBarrier(); // does not work -- do not know why not */
+  winShmEnterMutex();
+  winShmLeaveMutex();
+}
+
 #else
 # define winShmOpen    0
 # define winShmSize    0
 # define winShmGet     0
 # define winShmRelease 0
 # define winShmLock    0
+# define winShmBarrier 0
 # define winShmClose   0
 #endif /* #ifndef SQLITE_OMIT_WAL */
 /*
@@ -2113,6 +2128,7 @@ static const sqlite3_io_methods winIoMethod = {
   winShmGet,               /* xShmGet */
   winShmRelease,           /* xShmRelease */
   winShmLock,              /* xShmLock */
+  winShmBarrier,           /* xShmBarrier */
   winShmClose              /* xShmClose */
 };
 
index 4c149a684537f3ebe536fece94975d8fd2237ccc..02563990377cf8bb63b19b1575fe894c9ce4686c 100644 (file)
@@ -659,6 +659,7 @@ struct sqlite3_io_methods {
   int (*xShmGet)(sqlite3_file*, int reqSize, int *pSize, void volatile**);
   int (*xShmRelease)(sqlite3_file*);
   int (*xShmLock)(sqlite3_file*, int desiredLock, int *gotLock);
+  void (*xShmBarrier)(sqlite3_file*);
   int (*xShmClose)(sqlite3_file*, int deleteFlag);
   /* Methods above are valid for version 2 */
   /* Additional methods may be added in future releases */
index 98db74aabbf1f2bf4353c9c4426aa080066582a5..743e7ca0167cb1d7c476cdf9f5edaea40a989f6b 100644 (file)
@@ -543,6 +543,9 @@ static int cfShmRelease(sqlite3_file *pFile){
 static int cfShmLock(sqlite3_file *pFile, int desired, int *pGot){
   return sqlite3OsShmLock(((CrashFile*)pFile)->pRealFile, desired, pGot);
 }
+static void cfShmBarrier(sqlite3_file *pFile){
+  sqlite3OsShmBarrier(((CrashFile*)pFile)->pRealFile);
+}
 static int cfShmClose(sqlite3_file *pFile, int delFlag){
   return sqlite3OsShmClose(((CrashFile*)pFile)->pRealFile, delFlag);
 }
@@ -567,6 +570,7 @@ static const sqlite3_io_methods CrashFileVtab = {
   cfShmGet,                     /* xShmGet */
   cfShmRelease,                 /* xShmRelease */
   cfShmLock,                    /* xShmLock */
+  cfShmBarrier,                 /* xShmBarrier */
   cfShmClose                    /* xShmClose */
 };
 
index 3dab67abe9ca9913e089c51041886971ac8c0ac1..196dccf286cbf92c28c84019b86a4fb4e7c6e779 100644 (file)
@@ -55,6 +55,7 @@ static int devsymShmSize(sqlite3_file*,int,int*);
 static int devsymShmGet(sqlite3_file*,int,int*,volatile void**);
 static int devsymShmRelease(sqlite3_file*);
 static int devsymShmLock(sqlite3_file*,int,int*);
+static void devsymShmBarrier(sqlite3_file*);
 static int devsymShmClose(sqlite3_file*,int);
 
 /*
@@ -123,6 +124,7 @@ static sqlite3_io_methods devsym_io_methods = {
   devsymShmGet,                     /* xShmGet */
   devsymShmRelease,                 /* xShmRelease */
   devsymShmLock,                    /* xShmLock */
+  devsymShmBarrier,                 /* xShmBarrier */
   devsymShmClose                    /* xShmClose */
 };
 
@@ -265,6 +267,10 @@ static int devsymShmLock(sqlite3_file *pFile, int desired, int *pGot){
   devsym_file *p = (devsym_file *)pFile;
   return sqlite3OsShmLock(p->pReal, desired, pGot);
 }
+static void devsymShmBarrier(sqlite3_file *pFile){
+  devsym_file *p = (devsym_file *)pFile;
+  sqlite3OsShmBarrier(p->pReal);
+}
 static int devsymShmClose(sqlite3_file *pFile, int delFlag){
   devsym_file *p = (devsym_file *)pFile;
   return sqlite3OsShmClose(p->pReal, delFlag);
index 520dffcb7178b99a7ce76249d73d961c40cfd2b9..ea6c9726033ac7708f27dea3791337838fdcf020 100644 (file)
@@ -223,8 +223,8 @@ static sqlite3_io_methods fs_io_methods = {
   0,                            /* xShmOpen */
   0,                            /* xShmSize */
   0,                            /* xShmLock */
-  0,                            /* xShmClose */
-  0,                            /* xShmDelete */
+  0,                            /* xShmBarrier */
+  0                             /* xShmClose */
 };
 
 
@@ -245,8 +245,8 @@ static sqlite3_io_methods tmp_io_methods = {
   0,                            /* xShmOpen */
   0,                            /* xShmSize */
   0,                            /* xShmLock */
-  0,                            /* xShmClose */
-  0,                            /* xShmDelete */
+  0,                            /* xShmBarrier */
+  0                             /* xShmClose */
 };
 
 /* Useful macros used in several places */
index 0c8d311e38901b458aad9f67ad84a5720a855abe..f5e541c2d651c1a7ac3690eaf1e8c125b6e65958 100644 (file)
 #define OS_SHMGET            23
 #define OS_SHMRELEASE        24
 #define OS_SHMLOCK           25
-#define OS_SHMSIZE           26
-#define OS_ANNOTATE          27
+#define OS_SHMBARRIER        26
+#define OS_SHMSIZE           27
+#define OS_ANNOTATE          28
 
-#define OS_NUMEVENTS         28
+#define OS_NUMEVENTS         29
 
 #define VFSLOG_BUFFERSIZE 8192
 
@@ -150,6 +151,7 @@ static int vfslogShmSize(sqlite3_file *pFile, int reqSize, int *pNewSize);
 static int vfslogShmGet(sqlite3_file *pFile, int,int*,volatile void **);
 static int vfslogShmRelease(sqlite3_file *pFile);
 static int vfslogShmLock(sqlite3_file *pFile, int desiredLock, int *gotLock);
+static void vfslogShmBarrier(sqlite3_file*);
 static int vfslogShmClose(sqlite3_file *pFile, int deleteFlag);
 
 /*
@@ -206,6 +208,7 @@ static sqlite3_io_methods vfslog_io_methods = {
   vfslogShmGet,                   /* xShmGet */
   vfslogShmRelease,               /* xShmRelease */
   vfslogShmLock,                  /* xShmLock */
+  vfslogShmBarrier,               /* xShmBarrier */
   vfslogShmClose                  /* xShmClose */
 };
 
@@ -455,6 +458,14 @@ static int vfslogShmLock(sqlite3_file *pFile, int desiredLock, int *gotLock){
   vfslog_call(p->pVfslog, OS_SHMLOCK, p->iFileId, t, rc, 0, 0);
   return rc;
 }
+static void vfslogShmBarrier(sqlite3_file *pFile){
+  sqlite3_uint64 t;
+  VfslogFile *p = (VfslogFile *)pFile;
+  t = vfslog_time();
+  p->pReal->pMethods->xShmBarrier(p->pReal);
+  t = vfslog_time() - t;
+  vfslog_call(p->pVfslog, OS_SHMBARRIER, p->iFileId, t, SQLITE_OK, 0, 0);
+}
 static int vfslogShmClose(sqlite3_file *pFile, int deleteFlag){
   int rc;
   sqlite3_uint64 t;
@@ -781,6 +792,7 @@ static const char *vfslog_eventname(int eEvent){
     case OS_SHMSIZE:           zEvent = "xShmSize"; break;
     case OS_SHMRELEASE:        zEvent = "xShmRelease"; break;
     case OS_SHMLOCK:           zEvent = "xShmLock"; break;
+    case OS_SHMBARRIER:        zEvent = "xShmBarrier"; break;
 
     case OS_ANNOTATE:          zEvent = "annotation"; break;
   }
index f4dec595110ef7f1aeedbc6149f89bab1aa9f81b..f95587931eaa3440f2c5d9e8499c9ac72e8497aa 100644 (file)
@@ -103,6 +103,7 @@ static int tvfsShmSize(sqlite3_file*, int , int *);
 static int tvfsShmGet(sqlite3_file*, int , int *, volatile void **);
 static int tvfsShmRelease(sqlite3_file*);
 static int tvfsShmLock(sqlite3_file*, int , int *);
+static void tvfsShmBarrier(sqlite3_file*);
 static int tvfsShmClose(sqlite3_file*, int);
 
 static sqlite3_io_methods tvfs_io_methods = {
@@ -124,6 +125,7 @@ static sqlite3_io_methods tvfs_io_methods = {
   tvfsShmGet,                     /* xShmGet */
   tvfsShmRelease,                 /* xShmRelease */
   tvfsShmLock,                    /* xShmLock */
+  tvfsShmBarrier,                 /* xShmBarrier */
   tvfsShmClose                    /* xShmClose */
 };
 
@@ -269,6 +271,7 @@ static int tvfsOpen(
       pMethods->xShmRelease = 0;
       pMethods->xShmClose = 0;
       pMethods->xShmLock = 0;
+      pMethods->xShmBarrier = 0;
     }
     pFile->pMethods = pMethods;
   }
@@ -569,6 +572,17 @@ static int tvfsShmLock(
   return rc;
 }
 
+static void tvfsShmBarrier(sqlite3_file *pFile){
+  int rc = SQLITE_OK;
+  TestvfsFile *pFd = (TestvfsFile *)pFile;
+  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
+
+  tvfsExecTcl(p, "xShmBarrier", 
+      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
+  );
+  tvfsResultCode(p, &rc);
+}
+
 static int tvfsShmClose(
   sqlite3_file *pFile,
   int deleteFlag
index 5e4c927a77ac485d81c2b846ac446bbbc154568e..65c711d8f1204a278a1d5bfca167137a672a5e39 100644 (file)
--- a/src/wal.c
+++ b/src/wal.c
@@ -207,7 +207,10 @@ typedef struct WalIterator WalIterator;
 
 
 /*
-** The following object holds an exact copy of the wal-index header.
+** The following object holds a copy of the wal-index header content.
+**
+** The actual header in the wal-index consists of two copies of this
+** object.
 */
 struct WalIndexHdr {
   u32 iChange;      /* Counter incremented each transaction */
@@ -223,7 +226,7 @@ struct WalIndexHdr {
 ** only support mandatory file-locks, we do not read or write data
 ** from the region of the file on which locks are applied.
 */
-#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr))
+#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2)
 #define WALINDEX_LOCK_RESERVED 8
 
 /* Size of header before each frame in wal */
@@ -306,13 +309,13 @@ static void walChecksumBytes(
   u32 *aOut        /* OUT: Final checksum value output */
 ){
   u32 s1, s2;
+  u8 *aEnd = (u8*)&a[nByte];
   if( aIn ){
     s1 = aIn[0];
     s2 = aIn[1];
   }else{
     s1 = s2 = 0;
   }
-  u8 *aEnd = (u8*)&a[nByte];
 
   assert( nByte>=8 );
   assert( (nByte&0x00000003)==0 );
@@ -355,10 +358,14 @@ static int walSetLock(Wal *pWal, int desiredStatus){
 ** The checksum on pWal->hdr is updated before it is written.
 */
 static void walIndexWriteHdr(Wal *pWal){
+  WalIndexHdr *aHdr;
   walChecksumBytes((u8*)&pWal->hdr,
                    sizeof(pWal->hdr) - sizeof(pWal->hdr.aCksum),
                    0, pWal->hdr.aCksum);
-  memcpy((void*)pWal->pWiData, &pWal->hdr, sizeof(pWal->hdr));
+  aHdr = (WalIndexHdr*)pWal->pWiData;
+  memcpy(&aHdr[1], &pWal->hdr, sizeof(pWal->hdr));
+  sqlite3OsShmBarrier(pWal->pDbFd);
+  memcpy(&aHdr[0], &pWal->hdr, sizeof(pWal->hdr));
 }
 
 /*
@@ -1118,8 +1125,9 @@ int sqlite3WalClose(
 ** is read successfully and the checksum verified, return zero.
 */
 int walIndexTryHdr(Wal *pWal, int *pChanged){
-  u32 aCksum[2];
-  WalIndexHdr hdr;
+  u32 aCksum[2];               /* Checksum on the header content */
+  WalIndexHdr hdr1, hdr2;      /* Two copies of the header content */
+  WalIndexHdr *aHdr;           /* Header in shared memory */
 
   assert( pWal->pWiData );
   if( pWal->szWIndex==0 ){
@@ -1135,15 +1143,24 @@ int walIndexTryHdr(Wal *pWal, int *pChanged){
   ** file, meaning it is possible that an inconsistent snapshot is read
   ** from the file. If this happens, return non-zero.
   */
-  memcpy(&hdr, (void*)pWal->pWiData, sizeof(hdr));
-  walChecksumBytes((u8*)&hdr, sizeof(hdr)-sizeof(hdr.aCksum), 0, aCksum);
-  if( aCksum[0]!=hdr.aCksum[0] || aCksum[1]!=hdr.aCksum[1] ){
+  aHdr = (WalIndexHdr*)pWal->pWiData;
+  memcpy(&hdr1, &aHdr[0], sizeof(hdr1));
+  sqlite3OsShmBarrier(pWal->pDbFd);
+  memcpy(&hdr2, &aHdr[1], sizeof(hdr2));
+
+  if( memcmp(&hdr1, &hdr2, sizeof(hdr1))!=0 ){
+    /* Dirty read */
+    return 1;
+  }  
+  walChecksumBytes((u8*)&hdr1, sizeof(hdr1)-sizeof(hdr1.aCksum), 0, aCksum);
+  if( aCksum[0]!=hdr1.aCksum[0] || aCksum[1]!=hdr1.aCksum[1] ){
+    /* Malformed header */
     return 1;
   }
 
-  if( memcmp(&pWal->hdr, &hdr, sizeof(WalIndexHdr)) ){
+  if( memcmp(&pWal->hdr, &hdr1, sizeof(WalIndexHdr)) ){
     *pChanged = 1;
-    memcpy(&pWal->hdr, &hdr, sizeof(WalIndexHdr));
+    memcpy(&pWal->hdr, &hdr1, sizeof(WalIndexHdr));
     pWal->szPage = pWal->hdr.szPage;
   }
 
index 3248bdbc7e75486511993790b7a91afc37521127..ead8cbbb172f2d25234cf9556ca492850a830021 100644 (file)
@@ -25,11 +25,11 @@ proc set_tvfs_hdr {file args} {
 
   set blob [tvfs shm $file]
   if {[llength $args]} {
-    set blob [binary format i8a* [lindex $args 0] [string range $blob 32 end]]
+    set blob [binary format i16a* [lindex $args 0] [string range $blob 64 end]]
     tvfs shm $file $blob
   }
 
-  binary scan $blob i8 ints
+  binary scan $blob i16 ints
   return $ints
 }