/ Check-in [04e0cb57]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Enhance this branch to support page-level-locking (without MVCC) for multi-process deployments.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | server-process-edition
Files: files | file ages | folders
SHA3-256:04e0cb571dbed00e269a890a755e252d7e8204d6d2ed5a7cfdb3d78d990a2876
User & Date: dan 2017-08-16 16:52:14
Context
2017-08-16
17:06
Update this branch with the latest changes from trunk. check-in: 380a7b7a user: dan tags: server-process-edition
16:52
Enhance this branch to support page-level-locking (without MVCC) for multi-process deployments. check-in: 04e0cb57 user: dan tags: server-process-edition
2017-08-14
07:16
Remove code for PLL support in wal mode from this branch. check-in: 8e1b28ed user: dan tags: server-process-edition
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

3783
3784
3785
3786
3787
3788
3789


3790
3791


3792
3793
3794
3795





























3796


3797
3798
3799
3800
3801
3802
3803
3804
3805
3806



































3807
3808


3809










3810


3811
3812
3813


































3814
3815


3816
3817
3818
3819














































3820
3821





















































3822
3823
3824
3825
3826
3827
3828
    pFile->ctrlFlags |= mask;
  }
}

/* Forward declaration */
static int unixGetTempname(int nBuf, char *zBuf);



/*
** Information and control of an open file handle.


*/
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
  unixFile *pFile = (unixFile*)id;
  switch( op ){





























    case SQLITE_FCNTL_SERVER_MODE: {


      int rc = SQLITE_OK;
      int eServer = 0;
      if( pFile->ctrlFlags | UNIXFILE_EXCL ){
        char *zJrnl = sqlite3_mprintf("%s-journal", pFile->zPath);
        if( zJrnl==0 ){
          rc = SQLITE_NOMEM;
        }else{
          struct stat buf;        /* Used to hold return values of stat() */
          if( osStat(zJrnl, &buf) ){
            rc = SQLITE_IOERR_FSTAT;



































          }else{
            eServer = ((buf.st_mode & S_IFDIR) ? 1 : 0);


          }










        }


        sqlite3_free(zJrnl);
      }
      *((int*)pArg) = eServer;


































      return rc;
    }


    case SQLITE_FCNTL_FILEID: {
      i64 *aId = (i64*)pArg;
      aId[0] = (i64)(pFile->pInode->fileId.dev);
      aId[1] = (i64)(pFile->pInode->fileId.ino);














































      return SQLITE_OK;
    }





















































#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
    case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
      int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
      return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
    }
    case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
      int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);







>
>

<
>
>

<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
|
<
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
|
>
>
>
>
>
>
>
>
>
>

>
>
|
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
|
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







3783
3784
3785
3786
3787
3788
3789
3790
3791
3792

3793
3794
3795



3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829

3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872

3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890

3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929



3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
    pFile->ctrlFlags |= mask;
  }
}

/* Forward declaration */
static int unixGetTempname(int nBuf, char *zBuf);

#ifdef SQLITE_SERVER_EDITION

/*

** Structure passed by SQLite through the (void*) argument to various 
** fcntl operations.
*/



struct UnixServerArg {
  void *h;                        /* Handle from SHMOPEN */
  void *p;                        /* Mapping */
  int i1;                         /* Integer value 1 */
  int i2;                         /* Integer value 2 */
};
typedef struct UnixServerArg UnixServerArg;

/*
** Structure used as a server-shm handle.
*/
struct UnixServerShm {
  void *pMap;                     /* Pointer to mapping */
  int nMap;                       /* Size of mapping in bytes */
  int fd;                         /* File descriptor open on *-hma file */
};
typedef struct UnixServerShm UnixServerShm;

/*
** Implementation of SQLITE_FCNTL_FILEID
*/
static int unixFcntlServerFileid(unixFile *pFile, void *pArg){
  i64 *aId = (i64*)pArg;
  aId[0] = (i64)(pFile->pInode->fileId.dev);
  aId[1] = (i64)(pFile->pInode->fileId.ino);
  return SQLITE_OK;
}

/*
** Implementation of SQLITE_FCNTL_SERVER_MODE
*/
static int unixFcntlServerMode(unixFile *pFile, void *pArg){
  int rc = SQLITE_OK;
  int eServer = 0;

  char *zJrnl = sqlite3_mprintf("%s-journal", pFile->zPath);
  if( zJrnl==0 ){
    rc = SQLITE_NOMEM;
  }else{
    struct stat buf;        /* Used to hold return values of stat() */
    if( osStat(zJrnl, &buf) ){
      rc = SQLITE_IOERR_FSTAT;
    }else if( buf.st_mode & S_IFDIR ){
      eServer = (pFile->ctrlFlags & UNIXFILE_EXCL) ? 1 : 2;
    }
  }
  sqlite3_free(zJrnl);
  *((int*)pArg) = eServer;
  return rc;
}

/*
** Implementation of SQLITE_FCNTL_SERVER_SHMOPEN.
**
** The (void*) argument passed to this file control should actually be
** a pointer to a UnixServerArg or equivalent structure. Arguments are
** interpreted as follows:
**
**   UnixServerArg.h  - OUT: New server shm handle.
**   UnixServerArg.p  - OUT: New server shm mapping.
**   UnixServerArg.i1 - Size of requested mapping in bytes.
**   UnixServerArg.i2 - OUT: True if journal rollback + SHMOPEN2 are required.
*/
static int unixFcntlServerShmopen(unixFile *pFd, void *pArg){
  int rc = SQLITE_OK;
  UnixServerArg *pSArg = (UnixServerArg*)pArg;
  UnixServerShm *p;
  char *zHma;

  p = sqlite3_malloc(sizeof(UnixServerShm));
  if( p==0 ) return SQLITE_NOMEM;
  memset(p, 0, sizeof(UnixServerShm));
  p->fd = -1;

  zHma = sqlite3_mprintf("%s-journal/hma", pFd->zPath);
  if( zHma==0 ){
    rc = SQLITE_NOMEM;
  }else{

    p->fd = osOpen(zHma, O_RDWR|O_CREAT, 0644);
    p->nMap = pSArg->i1;

    if( p->fd<0 ){
      rc = SQLITE_CANTOPEN;
    }else{
      int res = ftruncate(p->fd, p->nMap);
      if( res!=0 ){
        rc = SQLITE_IOERR_TRUNCATE;
      }else{
        p->pMap = osMmap(0, p->nMap, PROT_READ|PROT_WRITE, MAP_SHARED, p->fd,0);
        if( p->pMap==0 ){
          rc = SQLITE_IOERR_MMAP;
        }
      }
    }
    sqlite3_free(zHma);
  }


  if( rc==SQLITE_OK ){
    int res;
    struct flock lock;
    memset(&lock, 0, sizeof(struct flock));
    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = p->nMap;
    lock.l_len = 1;

    res = osFcntl(p->fd, F_SETLK, &lock);
    if( res==0 ){
      pSArg->i2 = 1;
      memset(p->pMap, 0, p->nMap);
    }else{
      pSArg->i2 = 0;
      lock.l_type = F_RDLCK;
      res = osFcntl(p->fd, F_SETLKW, &lock);
      if( res!=0 ){
        rc = SQLITE_IOERR_LOCK;
      }
    }
  }

  if( rc!=SQLITE_OK ){
    if( p->pMap ) osMunmap(p->pMap, p->nMap);
    if( p->fd>=0 ) close(p->fd);
    sqlite3_free(p);
    pSArg->h = pSArg->p = 0;
  }else{
    pSArg->h = (void*)p;
    pSArg->p = (void*)(p->pMap);
  }

  return rc;
}

/*
** Implementation of SQLITE_FCNTL_SERVER_SHMOPEN2.



**
** The (void*) argument passed to this file control should actually be
** a pointer to a UnixServerArg or equivalent structure. Arguments are
** interpreted as follows:
**
**   UnixServerArg.h  - Server shm handle (from SHMOPEN).
**   UnixServerArg.p  - unused.
**   UnixServerArg.i1 - unused.
**   UnixServerArg.i2 - unused.
*/
static int unixFcntlServerShmopen2(unixFile *pFd, void *pArg){
  UnixServerArg *pSArg = (UnixServerArg*)pArg;
  UnixServerShm *p = (UnixServerShm*)pSArg->h;
  int res;
  struct flock lock;

  memset(&lock, 0, sizeof(struct flock));
  lock.l_type = F_RDLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = p->nMap;
  lock.l_len = 1;
  res = osFcntl(p->fd, F_SETLK, &lock);

  return res ? SQLITE_IOERR_LOCK : SQLITE_OK;
}

/*
** Implementation of SQLITE_FCNTL_SERVER_SHMCLOSE.
**
** The (void*) argument passed to this file control should actually be
** a pointer to a UnixServerArg or equivalent structure. Arguments are
** interpreted as follows:
**
**   UnixServerArg.h  - Server shm handle (from SHMOPEN).
**   UnixServerArg.p  - unused.
**   UnixServerArg.i1 - unused.
**   UnixServerArg.i2 - unused.
*/
static int unixFcntlServerShmclose(unixFile *pFd, void *pArg){
  UnixServerArg *pSArg = (UnixServerArg*)pArg;
  UnixServerShm *p = (UnixServerShm*)pSArg->h;

  if( p->pMap ) osMunmap(p->pMap, p->nMap);
  if( p->fd>=0 ) close(p->fd);
  sqlite3_free(p);

  return SQLITE_OK;
}

/*
** Implementation of SQLITE_FCNTL_SERVER_SHMLOCK.
**
** The (void*) argument passed to this file control should actually be
** a pointer to a UnixServerArg or equivalent structure. Arguments are
** interpreted as follows:
**
**   UnixServerArg.h  - Server shm handle (from SHMOPEN).
**   UnixServerArg.p  - unused.
**   UnixServerArg.i1 - slot to lock.
**   UnixServerArg.i2 - true to take the lock, false to release it.
*/
static int unixFcntlServerShmlock(unixFile *pFd, void *pArg){
  UnixServerArg *pSArg = (UnixServerArg*)pArg;
  UnixServerShm *p = (UnixServerShm*)pSArg->h;
  int res;

  struct flock lock;
  memset(&lock, 0, sizeof(struct flock));
  lock.l_type = pSArg->i2 ? F_WRLCK : F_UNLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = p->nMap + pSArg->i1 + 1;
  lock.l_len = 1;

  res = osFcntl(p->fd, F_SETLK, &lock);

  return (res==0 ? SQLITE_OK : SQLITE_BUSY);
}
#endif

/*
** Information and control of an open file handle.
*/
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
  unixFile *pFile = (unixFile*)id;
  switch( op ){

#ifdef SQLITE_SERVER_EDITION
    case SQLITE_FCNTL_FILEID:
      return unixFcntlServerFileid(pFile, pArg);
    case SQLITE_FCNTL_SERVER_MODE: 
      return unixFcntlServerMode(pFile, pArg);
    case SQLITE_FCNTL_SERVER_SHMOPEN:
      return unixFcntlServerShmopen(pFile, pArg);
    case SQLITE_FCNTL_SERVER_SHMOPEN2:
      return unixFcntlServerShmopen2(pFile, pArg);
    case SQLITE_FCNTL_SERVER_SHMCLOSE:
      return unixFcntlServerShmclose(pFile, pArg);
    case SQLITE_FCNTL_SERVER_SHMLOCK:
      return unixFcntlServerShmlock(pFile, pArg);
#endif

#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
    case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
      int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
      return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
    }
    case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
      int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);

Changes to src/pager.c.

838
839
840
841
842
843
844

845
846
847
848
849
850
851
....
1803
1804
1805
1806
1807
1808
1809



1810
1811
1812
1813
1814
1815
1816
....
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
....
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
....
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
....
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
# define pagerWalFrames(v,w,x,y) 0
# define pagerOpenWalIfPresent(z) SQLITE_OK
# define pagerBeginReadTransaction(z) SQLITE_OK
#endif

#ifdef SQLITE_SERVER_EDITION
# define pagerIsServer(x) ((x)->pServer!=0)

#else
# define pagerIsServer(x) 0
#endif

#ifndef NDEBUG 
/*
** Usage:
................................................................................
      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
    }
  }
  return rc;
}

#ifdef SQLITE_SERVER_EDITION



static void pagerFreeServerPage(Pager *pPager){
  ServerPage *pPg;
  ServerPage *pNext;
  for(pPg=pPager->pServerPage; pPg; pPg=pNext){
    pNext = pPg->pNext;
    sqlite3_free(pPg);
  }
................................................................................
  /* This function is only called for rollback pagers in WRITER_DBMOD state. */
  assert( !pagerUseWal(pPager) );
  assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
  assert( pPager->eLock==EXCLUSIVE_LOCK );
  assert( isOpen(pPager->fd) || pList->pDirty==0 );

#ifdef SQLITE_SERVER_EDITION
  if( pagerIsServer(pPager) ){
    rc = sqlite3ServerPreCommit(pPager->pServer, pPager->pServerPage);
    pPager->pServerPage = 0;
    if( rc!=SQLITE_OK ) return rc;
  }
#endif

  /* If the file is a temp-file has not yet been opened, open it now. It
................................................................................
    }
  }

  return rc;
}

#ifdef SQLITE_SERVER_EDITION
static int pagerServerConnect(Pager *pPager){
  int rc = SQLITE_OK;
  if( pPager->tempFile==0 ){
    pPager->noLock = 1;
    pPager->journalMode = PAGER_JOURNALMODE_PERSIST;
    rc = sqlite3ServerConnect(pPager, &pPager->pServer);
  }
  return rc;
}

int sqlite3PagerRollbackJournal(Pager *pPager, sqlite3_file *pJfd){
  int rc;                         /* Return Code */
  sqlite3_file *saved_jfd = pPager->jfd;
................................................................................
          sqlite3OsUnfetch(pPager->fd, 0, 0);
        }
      }
    }

#ifdef SQLITE_SERVER_EDITION
    if( eServer ){
      rc = pagerServerConnect(pPager);
    }
#endif

    /* If there is a WAL file in the file-system, open this database in WAL
    ** mode. Otherwise, the following function call is a no-op.
    */
    if( rc==SQLITE_OK ){
................................................................................
  Pager *pPager = pPg->pPager;
  int rc;
  u32 cksum;
  char *pData2;
  i64 iOff = pPager->journalOff;

#ifdef SQLITE_SERVER_EDITION
  if( pagerIsServer(pPager) ){
    ServerPage *p = sqlite3ServerBuffer(pPager->pServer);
    if( p==0 ){
      int nByte = sizeof(ServerPage) + pPager->pageSize;
      p = (ServerPage*)sqlite3_malloc(nByte);
      if( !p ) return SQLITE_NOMEM_BKPT;
    }
    memset(p, 0, sizeof(ServerPage));







>







 







>
>
>







 







|







 







|




|







 







|







 







|







838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
....
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
....
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
....
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
....
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
....
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
# define pagerWalFrames(v,w,x,y) 0
# define pagerOpenWalIfPresent(z) SQLITE_OK
# define pagerBeginReadTransaction(z) SQLITE_OK
#endif

#ifdef SQLITE_SERVER_EDITION
# define pagerIsServer(x) ((x)->pServer!=0)
# define pagerIsProcessServer(x) sqlite3ServerIsSingleProcess((x)->pServer)
#else
# define pagerIsServer(x) 0
#endif

#ifndef NDEBUG 
/*
** Usage:
................................................................................
      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
    }
  }
  return rc;
}

#ifdef SQLITE_SERVER_EDITION
/*
** Free the linked list of ServerPage objects headed at Pager.pServerPage.
*/
static void pagerFreeServerPage(Pager *pPager){
  ServerPage *pPg;
  ServerPage *pNext;
  for(pPg=pPager->pServerPage; pPg; pPg=pNext){
    pNext = pPg->pNext;
    sqlite3_free(pPg);
  }
................................................................................
  /* This function is only called for rollback pagers in WRITER_DBMOD state. */
  assert( !pagerUseWal(pPager) );
  assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
  assert( pPager->eLock==EXCLUSIVE_LOCK );
  assert( isOpen(pPager->fd) || pList->pDirty==0 );

#ifdef SQLITE_SERVER_EDITION
  if( pagerIsProcessServer(pPager) ){
    rc = sqlite3ServerPreCommit(pPager->pServer, pPager->pServerPage);
    pPager->pServerPage = 0;
    if( rc!=SQLITE_OK ) return rc;
  }
#endif

  /* If the file is a temp-file has not yet been opened, open it now. It
................................................................................
    }
  }

  return rc;
}

#ifdef SQLITE_SERVER_EDITION
static int pagerServerConnect(Pager *pPager, int eServer){
  int rc = SQLITE_OK;
  if( pPager->tempFile==0 ){
    pPager->noLock = 1;
    pPager->journalMode = PAGER_JOURNALMODE_PERSIST;
    rc = sqlite3ServerConnect(pPager, eServer, &pPager->pServer);
  }
  return rc;
}

int sqlite3PagerRollbackJournal(Pager *pPager, sqlite3_file *pJfd){
  int rc;                         /* Return Code */
  sqlite3_file *saved_jfd = pPager->jfd;
................................................................................
          sqlite3OsUnfetch(pPager->fd, 0, 0);
        }
      }
    }

#ifdef SQLITE_SERVER_EDITION
    if( eServer ){
      rc = pagerServerConnect(pPager, eServer);
    }
#endif

    /* If there is a WAL file in the file-system, open this database in WAL
    ** mode. Otherwise, the following function call is a no-op.
    */
    if( rc==SQLITE_OK ){
................................................................................
  Pager *pPager = pPg->pPager;
  int rc;
  u32 cksum;
  char *pData2;
  i64 iOff = pPager->journalOff;

#ifdef SQLITE_SERVER_EDITION
  if( pagerIsProcessServer(pPager) ){
    ServerPage *p = sqlite3ServerBuffer(pPager->pServer);
    if( p==0 ){
      int nByte = sizeof(ServerPage) + pPager->pageSize;
      p = (ServerPage*)sqlite3_malloc(nByte);
      if( !p ) return SQLITE_NOMEM_BKPT;
    }
    memset(p, 0, sizeof(ServerPage));

Changes to src/server.c.

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
..
62
63
64
65
66
67
68
69
70


71
72

73
74









75
76
77
78
79
80
81
82
83
84
85
86
87
..
98
99
100
101
102
103
104


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
...
121
122
123
124
125
126
127
128
129
130
131








132
133
134
135
136
137
138
...
149
150
151
152
153
154
155






156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
...
198
199
200
201
202
203
204
205




206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222






223


224
225
226
227
228
229
230
231
232
233
234
235
236

237

238
239


240
241
242
243
244
245





















246
247
248
249
250
251
252
253
254
255
256
257


258
259
260
261
262
263

264
265
266
267









268
269
270
271
272
273
274
275

















276
277
278
279
280
281









282
283
284
285

286
287















288

289
290
291
292
293
294
295
...
301
302
303
304
305
306
307
308

309
310
311
312
313
314
315
...
320
321
322
323
324
325
326

327
328
329
















330
331
332
333
334
335
336





337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354




355
356
357
358
359
360
361
362
363
364
365
366
367
368


369
370
371
372
373
374
375
376
377
378
379
380


381
382
383
384

385
386
387
388
389
390
391
392
393




394
395
396
397

398
399
400
401




402
403
404
405

406
407

408
409
410
411
412

413
414
415
416

417
418
419
420



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489












490
491
492
493
494
495
496
497


498
499
500
501
502
503
504
...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

580
581
582
583
584
585
586
587
588
589
590



591
592
593




594
595
596
597
598
599
600
601

602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

618


619
620

621
622
623
624
625
626
627
628
...
639
640
641
642
643
644
645


646
647
648
649
650
651
652
...
667
668
669
670
671
672
673

674
675
676
677
678
679
680
681
682
683

684
685
686
687
688
689
690
...
696
697
698
699
700
701
702
703









704
*/
#define HMA_SLOT_RL_BITS 16       /* bits for Read Locks */
#define HMA_SLOT_WL_BITS 5        /* bits for Write Locks */
#define HMA_SLOT_TR_BITS 8        /* bits for Transient Reader locks */

#define HMA_SLOT_RLWL_BITS (HMA_SLOT_RL_BITS + HMA_SLOT_WL_BITS)


#define HMA_SLOT_RL_MASK ((1 << HMA_SLOT_RL_BITS)-1)
#define HMA_SLOT_WL_MASK (((1 << HMA_SLOT_WL_BITS)-1) << HMA_SLOT_RL_BITS)
#define HMA_SLOT_TR_MASK (((1 << HMA_SLOT_TR_BITS)-1) << HMA_SLOT_RLWL_BITS)


/* Number of page-locking slots */
#define HMA_PAGELOCK_SLOTS (256*1024)

/* Maximum concurrent read/write transactions */
#define HMA_MAX_TRANSACTIONID 16


#define HMA_HASH_SIZE 512

/*
** The argument to this macro is the value of a locking slot. It returns
** -1 if no client currently holds the write lock, or the transaction-id
** of the locker otherwise.
*/
................................................................................
** The argument to this macro is the value of a locking slot. This macro
** returns the current number of slow reader clients reading the page.
*/
#define slotGetSlowReaders(v) (((v) & HMA_SLOT_TR_MASK) >> HMA_SLOT_RLWL_BITS)

#define slotReaderMask(v) ((v) & HMA_SLOT_RL_MASK)

#include "unistd.h"
#include "fcntl.h"


#include "sys/mman.h"
#include "sys/types.h"

#include "sys/stat.h"
#include "errno.h"










typedef struct ServerDb ServerDb;
typedef struct ServerJournal ServerJournal;

struct ServerGlobal {
  ServerDb *pDb;                  /* Linked list of all ServerHMA objects */
};
static struct ServerGlobal g_server;

struct ServerJournal {
  char *zJournal;
  sqlite3_file *jfd;
};
................................................................................
  u32 *aSlot;                     /* Array of page locking slots */
  i64 aFileId[2];                 /* Opaque VFS file-id */
  ServerDb *pNext;                /* Next db in this process */

  sqlite3_vfs *pVfs;
  ServerJournal aJrnl[HMA_MAX_TRANSACTIONID];
  u8 *aJrnlFdSpace;



  int iNextCommit;                /* Commit id for next pre-commit call */ 
  Server *pCommit;                /* List of connections currently commiting */
  Server *pReader;                /* Connections in slower-reader transaction */
  ServerPage *pPgFirst;           /* First (oldest) in list of pages */
  ServerPage *pPgLast;            /* Last (newest) in list of pages */
  ServerPage *apPg[HMA_HASH_SIZE];

  ServerPage *pFree;              /* List of free page buffers */
};

/*
** Once instance for each client connection open on a server mode database
** in this process.
*/
................................................................................
  ServerDb *pDb;                  /* Database object */
  Pager *pPager;                  /* Associated pager object */
  int eTrans;                     /* One of the SERVER_TRANS_xxx values */ 
  int iTransId;                   /* Current transaction id (or -1) */
  int iCommitId;                  /* Current commit id (or 0) */
  int nAlloc;                     /* Allocated size of aLock[] array */
  int nLock;                      /* Number of entries in aLock[] */
  u32 *aLock;                     /* Mapped lock file */
  Server *pNext;                  /* Next in pCommit or pReader list */
};









/*
** Possible values for Server.eTrans.
*/
#define SERVER_TRANS_NONE      0
#define SERVER_TRANS_READONLY  1
#define SERVER_TRANS_READWRITE 2

................................................................................
static void serverLeaveMutex(void){
  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_APP1));
}
static void serverAssertMutexHeld(void){
  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_APP1)) );
}







static int serverFindDatabase(Server *pNew, i64 *aFileId){
  ServerDb *p;
  int rc = SQLITE_OK;
  serverEnterMutex();
  for(p=g_server.pDb; p; p=p->pNext){
    if( p->aFileId[0]==aFileId[0] && p->aFileId[1]==aFileId[1] ){
      break;
    }
  }
  if( p==0 ){
    p = (ServerDb*)sqlite3MallocZero(sizeof(ServerDb));
    if( p ){
      p->aSlot = (u32*)sqlite3MallocZero(sizeof(u32)*HMA_PAGELOCK_SLOTS);
      if( p->aSlot==0 ){
        rc = SQLITE_NOMEM_BKPT;
      }else{
        p->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
#if SQLITE_THREADSAFE!=0
        if( p->mutex==0 ) rc = SQLITE_NOMEM_BKPT;
#endif
      }

      if( rc==SQLITE_NOMEM ){
        sqlite3_free(p->aSlot);
        sqlite3_free(p);
        p = 0;
      }else{
        p->nClient = 1;
        p->iNextCommit = 1;
        p->aFileId[0] = aFileId[0];
        p->aFileId[1] = aFileId[1];
................................................................................
  return rc;
}

/*
** Free all resources allocated by serverInitDatabase() associated with the
** object passed as the only argument.
*/
static void serverShutdownDatabase(ServerDb *pDb){




  int i;

  for(i=0; i<HMA_MAX_TRANSACTIONID; i++){
    ServerJournal *pJ = &pDb->aJrnl[i];
    if( pJ->jfd ){
      sqlite3OsClose(pJ->jfd);
      sqlite3OsDelete(pDb->pVfs, pJ->zJournal, 0);
    }
    sqlite3_free(pJ->zJournal);
  }
  memset(pDb->aJrnl, 0, sizeof(ServerJournal)*HMA_MAX_TRANSACTIONID);

  if( pDb->aJrnlFdSpace ){
    sqlite3_free(pDb->aJrnlFdSpace);
    pDb->aJrnlFdSpace = 0;
  }







  sqlite3_free(pDb->aSlot);


  pDb->bInit = 0;
}

/*
** This function is called when the very first connection to a database
** is established. It is responsible for rolling back any hot journal
** files found in the file-system.
*/
static int serverInitDatabase(Server *pNew){
  int nByte;
  int rc = SQLITE_OK;
  ServerDb *pDb = pNew->pDb;
  sqlite3_vfs *pVfs;

  const char *zFilename = sqlite3PagerFilename(pNew->pPager, 0);


  assert( zFilename );


  pVfs = pDb->pVfs = sqlite3PagerVfs(pNew->pPager);
  nByte = ROUND8(pVfs->szOsFile) * HMA_MAX_TRANSACTIONID;
  pDb->aJrnlFdSpace = (u8*)sqlite3MallocZero(nByte);
  if( pDb->aJrnlFdSpace==0 ){
    rc = SQLITE_NOMEM_BKPT;
  }else{





















    u8 *a = pDb->aJrnlFdSpace;
    int i;
    for(i=0; rc==SQLITE_OK && i<HMA_MAX_TRANSACTIONID; i++){
      int bExists = 0;
      ServerJournal *pJ = &pDb->aJrnl[i];
      pJ->jfd = (sqlite3_file*)&a[ROUND8(pVfs->szOsFile)*i];
      pJ->zJournal = sqlite3_mprintf("%s-journal/%d-journal", zFilename, i);
      if( pJ->zJournal==0 ){
        rc = SQLITE_NOMEM_BKPT;
        break;
      }



      rc = sqlite3OsAccess(pVfs, pJ->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
      if( rc==SQLITE_OK && bExists ){
        int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
        rc = sqlite3OsOpen(pVfs, pJ->zJournal, pJ->jfd, flags, &flags);
        if( rc==SQLITE_OK ){
          rc = sqlite3PagerRollbackJournal(pNew->pPager, pJ->jfd);

        }
      }
    }
  }










  if( rc==SQLITE_OK ){
    pDb->bInit = 1;
  }else{
    serverShutdownDatabase(pNew->pDb);
  }
  return rc;
}


















/*
** Close the connection.
*/
void sqlite3ServerDisconnect(Server *p, sqlite3_file *dbfd){
  ServerDb *pDb = p->pDb;










  serverEnterMutex();
  pDb->nClient--;
  if( pDb->nClient==0 ){

    ServerPage *pFree;
    ServerDb **pp;















    serverShutdownDatabase(pDb);

    for(pp=&g_server.pDb; *pp!=pDb; pp=&((*pp)->pNext));
    *pp = pDb->pNext;
    sqlite3_mutex_free(pDb->mutex);
    while( (pFree = pDb->pFree) ){
      pDb->pFree = pFree->pNext;
      sqlite3_free(pFree);
    }
................................................................................
  sqlite3_free(p);
}

/*
** Connect to the system.
*/
int sqlite3ServerConnect(
  Pager *pPager,

  Server **ppOut                  /* OUT: Server handle */
){
  Server *pNew = 0;
  sqlite3_file *dbfd = sqlite3PagerFile(pPager);
  i64 aFileId[2];
  int rc;

................................................................................
      pNew->pPager = pPager;
      pNew->iTransId = -1;
      rc = serverFindDatabase(pNew, aFileId);
      if( rc!=SQLITE_OK ){
        sqlite3_free(pNew);
        pNew = 0;
      }else{

        sqlite3_mutex_enter(pNew->pDb->mutex);
        if( pNew->pDb->bInit==0 ){
          rc = serverInitDatabase(pNew);
















        }
        sqlite3_mutex_leave(pNew->pDb->mutex);
      }
    }else{
      rc = SQLITE_NOMEM_BKPT;
    }
  }






  *ppOut = pNew;
  return rc;
}

/*
** Begin a transaction.
*/
int sqlite3ServerBegin(Server *p, int bReadonly){
  int rc = SQLITE_OK;

  if( p->eTrans==SERVER_TRANS_NONE ){
    int id;
    ServerDb *pDb = p->pDb;
    u32 t;

    assert( p->iTransId<0 );
    assert( p->pNext==0 );




    sqlite3_mutex_enter(pDb->mutex);

    if( bReadonly ){
      Server *pIter;
      p->iCommitId = pDb->iNextCommit;
      for(pIter=pDb->pCommit; pIter; pIter=pIter->pNext){
        if( pIter->iCommitId<p->iCommitId ){
          p->iCommitId = pIter->iCommitId;
        }
      }
      p->pNext = pDb->pReader;
      pDb->pReader = p;
      p->eTrans = SERVER_TRANS_READONLY;
    }else{


      /* Find a transaction id to use */
      rc = SQLITE_BUSY;
      t = pDb->transmask;
      for(id=0; id<HMA_MAX_TRANSACTIONID; id++){
        if( (t & (1 << id))==0 ){
          t = t | (1 << id);
          rc = SQLITE_OK;
          break;
        }
      }
      pDb->transmask = t;
      p->eTrans = SERVER_TRANS_READWRITE;


    }

    sqlite3_mutex_leave(pDb->mutex);


    if( rc==SQLITE_OK && bReadonly==0 ){
      ServerJournal *pJrnl = &pDb->aJrnl[id];
      sqlite3PagerServerJournal(p->pPager, pJrnl->jfd, pJrnl->zJournal);
      p->iTransId = id;
    }
  }

  return rc;
}





static void serverReleaseLocks(Server *p){
  ServerDb *pDb = p->pDb;
  int i;

  assert( sqlite3_mutex_held(pDb->mutex) );

  for(i=0; i<p->nLock; i++){
    u32 *pSlot = &pDb->aSlot[p->aLock[i] % HMA_PAGELOCK_SLOTS];




    if( slotGetWriter(*pSlot)==p->iTransId ){
      *pSlot -= ((p->iTransId + 1) << HMA_MAX_TRANSACTIONID);
    }
    *pSlot &= ~((u32)1 << p->iTransId);

  }


  p->nLock = 0;
}

/*
** End a transaction (and release all locks).

*/
int sqlite3ServerEnd(Server *p){
  int rc = SQLITE_OK;
  if( p->eTrans!=SERVER_TRANS_NONE ){

    Server **pp;
    ServerDb *pDb = p->pDb;
    ServerPage *pPg = 0;




    sqlite3_mutex_enter(pDb->mutex);

    if( p->eTrans==SERVER_TRANS_READONLY ){
      /* Remove the connection from the readers list */
      for(pp=&pDb->pReader; *pp!=p; pp = &((*pp)->pNext));
      *pp = p->pNext;
    }else{
      serverReleaseLocks(p);

      /* Clear the bit in the transaction mask. */
      pDb->transmask &= ~((u32)1 << p->iTransId);

      /* If this connection is in the committers list, remove it. */
      for(pp=&pDb->pCommit; *pp; pp = &((*pp)->pNext)){
        if( *pp==p ){
          *pp = p->pNext;
          break;
        }
      }
    }

    /* See if it is possible to free any ServerPage records. If so, remove
    ** them from the linked list and hash table, but do not call sqlite3_free()
    ** on them until the mutex has been released.  */
    if( pDb->pPgFirst ){
      ServerPage *pLast = 0;
      Server *pIter;
      int iOldest = 0x7FFFFFFF;
      for(pIter=pDb->pReader; pIter; pIter=pIter->pNext){
        iOldest = MIN(iOldest, pIter->iCommitId);
      }
      for(pIter=pDb->pCommit; pIter; pIter=pIter->pNext){
        iOldest = MIN(iOldest, pIter->iCommitId);
      }

      for(pPg=pDb->pPgFirst; pPg && pPg->iCommitId<iOldest; pPg=pPg->pNext){
        if( pPg->pHashPrev ){
          pPg->pHashPrev->pHashNext = pPg->pHashNext;
        }else{
          int iHash = pPg->pgno % HMA_HASH_SIZE;
          assert( pDb->apPg[iHash]==pPg );
          pDb->apPg[iHash] = pPg->pHashNext;
        }
        if( pPg->pHashNext ){
          pPg->pHashNext->pHashPrev = pPg->pHashPrev;
        }
        pLast = pPg;
      }

      if( pLast ){
        assert( pLast->pNext==pPg );
        pLast->pNext = pDb->pFree;
        pDb->pFree = pDb->pPgFirst;
      }

      if( pPg==0 ){
        pDb->pPgFirst = pDb->pPgLast = 0;
      }else{
        pDb->pPgFirst = pPg;
      }
    }

    sqlite3_mutex_leave(pDb->mutex);

    p->pNext = 0;
    p->eTrans = SERVER_TRANS_NONE;
    p->iTransId = -1;
    p->iCommitId = 0;
  }












  return rc;
}

int sqlite3ServerPreCommit(Server *p, ServerPage *pPg){
  ServerDb *pDb = p->pDb;
  int rc = SQLITE_OK;
  ServerPage *pIter;



  if( pPg==0 ) return SQLITE_OK;

  sqlite3_mutex_enter(pDb->mutex);

  /* Assign a commit id to this transaction */
  assert( p->iCommitId==0 );
  assert( p->eTrans==SERVER_TRANS_READWRITE );
................................................................................
** If parameter bBlock is non-zero, then make this a blocking lock if
** possible.
*/
int sqlite3ServerLock(Server *p, Pgno pgno, int bWrite, int bBlock){
  int rc = SQLITE_OK;

  assert( p->eTrans==SERVER_TRANS_READWRITE 
       || p->eTrans==SERVER_TRANS_READONLY 
  );
  if( p->eTrans==SERVER_TRANS_READWRITE ){
    ServerDb *pDb = p->pDb;
    int iWriter;
    int bSkip = 0;
    u32 *pSlot;


    assert( p->iTransId>=0 );
    assert( p->nLock<=p->nAlloc );
    if( p->nLock==p->nAlloc ){
      int nNew = p->nLock ? p->nLock*2 : 256;
      u32 *aNew = sqlite3_realloc(p->aLock, nNew*sizeof(u32));
      if( aNew==0 ) return SQLITE_NOMEM_BKPT;
      memset(&aNew[p->nLock], 0, sizeof(u32) * (nNew - p->nLock));
      p->nAlloc = nNew;
      p->aLock = aNew;
    }




    sqlite3_mutex_enter(pDb->mutex);

    pSlot = &pDb->aSlot[pgno % HMA_PAGELOCK_SLOTS];




    assert( slotGetWriter(*pSlot)<0 
        || slotReaderMask(*pSlot)==0 
        || slotReaderMask(*pSlot)==(1 << slotGetWriter(*pSlot))
        );

    iWriter = slotGetWriter(*pSlot);
    if( iWriter==p->iTransId || (bWrite==0 && (*pSlot & (1<<p->iTransId))) ){
      bSkip = 1;

    }else if( iWriter>=0 ){
      rc = SQLITE_BUSY_DEADLOCK;
    }else if( bWrite ){
      if( (slotReaderMask(*pSlot) & ~(1 << p->iTransId))==0 ){
        *pSlot += ((p->iTransId + 1) << HMA_MAX_TRANSACTIONID);
      }else{
        rc = SQLITE_BUSY_DEADLOCK;
      }
    }else{
      *pSlot |= (1 << p->iTransId);
    }

    assert( slotGetWriter(*pSlot)<0 
        || slotReaderMask(*pSlot)==0 
        || slotReaderMask(*pSlot)==(1 << slotGetWriter(*pSlot))
        );




    sqlite3_mutex_leave(pDb->mutex);


    if( bSkip==0 ){
      p->aLock[p->nLock++] = pgno;
    }
  }

  return rc;
}

................................................................................
void sqlite3ServerReadPage(Server *p, Pgno pgno, u8 **ppData){
  if( p->eTrans==SERVER_TRANS_READONLY ){
    ServerDb *pDb = p->pDb;
    ServerPage *pIter;
    ServerPage *pBest = 0;
    int iHash = pgno % HMA_HASH_SIZE;



    sqlite3_mutex_enter(pDb->mutex);

    /* Search the hash table for the oldest version of page pgno with
    ** a commit-id greater than or equal to Server.iCommitId.  */
    for(pIter=pDb->apPg[iHash]; pIter; pIter=pIter->pHashNext){
      if( pIter->pgno==pgno 
       && pIter->iCommitId>=p->iCommitId 
................................................................................
  }
}

void sqlite3ServerEndReadPage(Server *p, Pgno pgno){
  if( p->eTrans==SERVER_TRANS_READONLY ){
    ServerDb *pDb = p->pDb;
    u32 *pSlot = &pDb->aSlot[pgno % HMA_PAGELOCK_SLOTS];

    sqlite3_mutex_enter(pDb->mutex);
    serverIncrSlowReader(pSlot, -1);
    assert( slotGetSlowReaders(*pSlot)>=0 );
    sqlite3_mutex_leave(pDb->mutex);
  }
}

ServerPage *sqlite3ServerBuffer(Server *p){
  ServerDb *pDb = p->pDb;
  ServerPage *pRet = 0;

  sqlite3_mutex_enter(pDb->mutex);
  if( pDb->pFree ){
    pRet = pDb->pFree;
    pDb->pFree = pRet->pNext;
    pRet->pNext = 0;
  }
  sqlite3_mutex_leave(pDb->mutex);
................................................................................
** currently has an open readonly transaction (one started with BEGIN
** READONLY). Return false if the argument is NULL, if there is no open
** transaction, or if the open transaction is read/write.
*/
int sqlite3ServerIsReadonly(Server *p){
  return (p && p->eTrans==SERVER_TRANS_READONLY);
}










#endif /* ifdef SQLITE_SERVER_EDITION */







<











|







 







<
<
>
>
|
<
>
|
|
>
>
>
>
>
>
>
>
>





|







 







>
>






|
<







 







|



>
>
>
>
>
>
>
>







 







>
>
>
>
>
>












<
<
<
<
|

|

<
<

<







 







|
>
>
>
>






|










>
>
>
>
>
>
|
>
>








|




>

>


>
>






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<








>
>
|
|
|
|
|
|
>




>
>
>
>
>
>
>
>
>




|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






>
>
>
>
>
>
>
>
>




>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>







 







|
>







 







>

|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
>
>
>
>












<



<

>
>
>
>
|
<
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
|
|
|
>
|
|

<





>
>
>
>




>
|


<
>
>
>
>
|
|
|
<
>
|
|
>




|
>

<
<
<
>
|
|
|

>
>
>
|

|
|
|
|
|
|

|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|

|
|
|
|
|
|

|

|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
|







>
>







 







|







>











>
>
>
|

<
>
>
>
>
|
|
|
|

|
|
|
>
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
>
|
>
>
|
|
>
|







 







>
>







 







>










>







 








>
>
>
>
>
>
>
>
>

32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
..
61
62
63
64
65
66
67


68
69
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

122
123
124
125
126
127
128
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190




191
192
193
194


195

196
197
198
199
200
201
202
...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
...
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
...
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

479
480
481

482
483
484
485
486
487

488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540

541
542
543
544
545
546
547

548
549
550
551
552
553
554
555
556
557
558



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756

757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
...
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
...
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
...
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
*/
#define HMA_SLOT_RL_BITS 16       /* bits for Read Locks */
#define HMA_SLOT_WL_BITS 5        /* bits for Write Locks */
#define HMA_SLOT_TR_BITS 8        /* bits for Transient Reader locks */

#define HMA_SLOT_RLWL_BITS (HMA_SLOT_RL_BITS + HMA_SLOT_WL_BITS)


#define HMA_SLOT_RL_MASK ((1 << HMA_SLOT_RL_BITS)-1)
#define HMA_SLOT_WL_MASK (((1 << HMA_SLOT_WL_BITS)-1) << HMA_SLOT_RL_BITS)
#define HMA_SLOT_TR_MASK (((1 << HMA_SLOT_TR_BITS)-1) << HMA_SLOT_RLWL_BITS)


/* Number of page-locking slots */
#define HMA_PAGELOCK_SLOTS (256*1024)

/* Maximum concurrent read/write transactions */
#define HMA_MAX_TRANSACTIONID 16

/* Number of buckets in hash table used for MVCC in single-process mode */
#define HMA_HASH_SIZE 512

/*
** The argument to this macro is the value of a locking slot. It returns
** -1 if no client currently holds the write lock, or the transaction-id
** of the locker otherwise.
*/
................................................................................
** The argument to this macro is the value of a locking slot. This macro
** returns the current number of slow reader clients reading the page.
*/
#define slotGetSlowReaders(v) (((v) & HMA_SLOT_TR_MASK) >> HMA_SLOT_RLWL_BITS)

#define slotReaderMask(v) ((v) & HMA_SLOT_RL_MASK)




/* 
** Atomic CAS primitive used in multi-process mode. Equivalent to:

**
**   int serverCompareAndSwap(u32 *ptr, u32 oldval, u32 newval){
**     if( *ptr==oldval ){
**       *ptr = newval;
**       return 1;
**     }
**     return 0;
**   }
*/
#define serverCompareAndSwap(ptr,oldval,newval) \
  __sync_bool_compare_and_swap(ptr,oldval,newval)


typedef struct ServerDb ServerDb;
typedef struct ServerJournal ServerJournal;

struct ServerGlobal {
  ServerDb *pDb;                  /* Linked list of all ServerDb objects */
};
static struct ServerGlobal g_server;

struct ServerJournal {
  char *zJournal;
  sqlite3_file *jfd;
};
................................................................................
  u32 *aSlot;                     /* Array of page locking slots */
  i64 aFileId[2];                 /* Opaque VFS file-id */
  ServerDb *pNext;                /* Next db in this process */

  sqlite3_vfs *pVfs;
  ServerJournal aJrnl[HMA_MAX_TRANSACTIONID];
  u8 *aJrnlFdSpace;

  void *pServerShm;

  int iNextCommit;                /* Commit id for next pre-commit call */ 
  Server *pCommit;                /* List of connections currently commiting */
  Server *pReader;                /* Connections in slower-reader transaction */
  ServerPage *pPgFirst;           /* First (oldest) in list of pages */
  ServerPage *pPgLast;            /* Last (newest) in list of pages */
  ServerPage *apPg[HMA_HASH_SIZE];/* Hash table of "old" page data */

  ServerPage *pFree;              /* List of free page buffers */
};

/*
** Once instance for each client connection open on a server mode database
** in this process.
*/
................................................................................
  ServerDb *pDb;                  /* Database object */
  Pager *pPager;                  /* Associated pager object */
  int eTrans;                     /* One of the SERVER_TRANS_xxx values */ 
  int iTransId;                   /* Current transaction id (or -1) */
  int iCommitId;                  /* Current commit id (or 0) */
  int nAlloc;                     /* Allocated size of aLock[] array */
  int nLock;                      /* Number of entries in aLock[] */
  u32 *aLock;                     /* Array of held locks */
  Server *pNext;                  /* Next in pCommit or pReader list */
};

struct ServerFcntlArg {
  void *h;                        /* Handle from SHMOPEN */
  void *p;                        /* Mapping */
  int i1;                         /* Integer value 1 */
  int i2;                         /* Integer value 2 */
};
typedef struct ServerFcntlArg ServerFcntlArg;

/*
** Possible values for Server.eTrans.
*/
#define SERVER_TRANS_NONE      0
#define SERVER_TRANS_READONLY  1
#define SERVER_TRANS_READWRITE 2

................................................................................
static void serverLeaveMutex(void){
  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_APP1));
}
static void serverAssertMutexHeld(void){
  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_APP1)) );
}

/*
** Locate the ServerDb object shared by all connections to the db identified
** by aFileId[2], increment its ref count and set pNew->pDb to point to it. 
** In this context "locate" may mean to find an existing object or to
** allocate a new one.
*/
static int serverFindDatabase(Server *pNew, i64 *aFileId){
  ServerDb *p;
  int rc = SQLITE_OK;
  serverEnterMutex();
  for(p=g_server.pDb; p; p=p->pNext){
    if( p->aFileId[0]==aFileId[0] && p->aFileId[1]==aFileId[1] ){
      break;
    }
  }
  if( p==0 ){
    p = (ServerDb*)sqlite3MallocZero(sizeof(ServerDb));
    if( p ){




      p->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
#if SQLITE_THREADSAFE!=0
      if( p->mutex==0 ) rc = SQLITE_NOMEM_BKPT;
#endif


      if( rc==SQLITE_NOMEM ){

        sqlite3_free(p);
        p = 0;
      }else{
        p->nClient = 1;
        p->iNextCommit = 1;
        p->aFileId[0] = aFileId[0];
        p->aFileId[1] = aFileId[1];
................................................................................
  return rc;
}

/*
** Free all resources allocated by serverInitDatabase() associated with the
** object passed as the only argument.
*/
static void serverShutdownDatabase(
  ServerDb *pDb, 
  sqlite3_file *dbfd, 
  int bDelete
){
  int i;

  for(i=0; i<HMA_MAX_TRANSACTIONID; i++){
    ServerJournal *pJ = &pDb->aJrnl[i];
    if( pJ->jfd ){
      sqlite3OsClose(pJ->jfd);
      if( bDelete ) sqlite3OsDelete(pDb->pVfs, pJ->zJournal, 0);
    }
    sqlite3_free(pJ->zJournal);
  }
  memset(pDb->aJrnl, 0, sizeof(ServerJournal)*HMA_MAX_TRANSACTIONID);

  if( pDb->aJrnlFdSpace ){
    sqlite3_free(pDb->aJrnlFdSpace);
    pDb->aJrnlFdSpace = 0;
  }

  if( pDb->pServerShm ){
    ServerFcntlArg arg;
    memset(&arg, 0, sizeof(ServerFcntlArg));
    arg.h = pDb->pServerShm;
    sqlite3OsFileControl(dbfd, SQLITE_FCNTL_SERVER_SHMCLOSE, (void*)&arg);
  }else{
    sqlite3_free(pDb->aSlot);
  }
  pDb->aSlot = 0;
  pDb->bInit = 0;
}

/*
** This function is called when the very first connection to a database
** is established. It is responsible for rolling back any hot journal
** files found in the file-system.
*/
static int serverInitDatabase(Server *pNew, int eServer){
  int nByte;
  int rc = SQLITE_OK;
  ServerDb *pDb = pNew->pDb;
  sqlite3_vfs *pVfs;
  sqlite3_file *dbfd = sqlite3PagerFile(pNew->pPager);
  const char *zFilename = sqlite3PagerFilename(pNew->pPager, 0);
  int bRollback = 0;

  assert( zFilename );
  assert( eServer==1 || eServer==2 );

  pVfs = pDb->pVfs = sqlite3PagerVfs(pNew->pPager);
  nByte = ROUND8(pVfs->szOsFile) * HMA_MAX_TRANSACTIONID;
  pDb->aJrnlFdSpace = (u8*)sqlite3MallocZero(nByte);
  if( pDb->aJrnlFdSpace==0 ){
    rc = SQLITE_NOMEM_BKPT;
  }else{
    if( eServer==2 ){
      ServerFcntlArg arg;
      arg.h = 0;
      arg.p = 0;
      arg.i1 = sizeof(u32)*HMA_PAGELOCK_SLOTS;
      arg.i2 = 0;

      rc = sqlite3OsFileControl(dbfd, SQLITE_FCNTL_SERVER_SHMOPEN, (void*)&arg);
      if( rc==SQLITE_OK ){
        pDb->aSlot = (u32*)arg.p;
        pDb->pServerShm = arg.h;
        bRollback = arg.i2;
      }
    }else{
      pDb->aSlot = (u32*)sqlite3MallocZero(sizeof(u32)*HMA_PAGELOCK_SLOTS);
      if( pDb->aSlot==0 ) rc = SQLITE_NOMEM_BKPT;
      bRollback = 1;
    }
  }

  if( rc==SQLITE_OK ){
    u8 *a = pDb->aJrnlFdSpace;
    int i;
    for(i=0; rc==SQLITE_OK && i<HMA_MAX_TRANSACTIONID; i++){

      ServerJournal *pJ = &pDb->aJrnl[i];
      pJ->jfd = (sqlite3_file*)&a[ROUND8(pVfs->szOsFile)*i];
      pJ->zJournal = sqlite3_mprintf("%s-journal/%d-journal", zFilename, i);
      if( pJ->zJournal==0 ){
        rc = SQLITE_NOMEM_BKPT;
        break;
      }

      if( bRollback ){
        int bExist = 0;
        rc = sqlite3OsAccess(pVfs, pJ->zJournal, SQLITE_ACCESS_EXISTS, &bExist);
        if( rc==SQLITE_OK && bExist ){
          int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
          rc = sqlite3OsOpen(pVfs, pJ->zJournal, pJ->jfd, flags, &flags);
          if( rc==SQLITE_OK ){
            rc = sqlite3PagerRollbackJournal(pNew->pPager, pJ->jfd);
          }
        }
      }
    }
  }

  if( rc==SQLITE_OK && pDb->pServerShm && bRollback ){
    ServerFcntlArg arg;
    arg.h = pDb->pServerShm;
    arg.p = 0;
    arg.p = 0;
    arg.i2 = 0;
    rc = sqlite3OsFileControl(dbfd, SQLITE_FCNTL_SERVER_SHMOPEN2, (void*)&arg);
  }

  if( rc==SQLITE_OK ){
    pDb->bInit = 1;
  }else{
    serverShutdownDatabase(pNew->pDb, dbfd, eServer==1);
  }
  return rc;
}

/*
** Take (bLock==1) or release (bLock==0) a server shmlock on slot iSlot.
** Return SQLITE_OK if successful, or SQLITE_BUSY if the lock cannot be
** obtained. 
*/
static int serverFcntlLock(Server *p, int iSlot, int bLock){
  sqlite3_file *dbfd = sqlite3PagerFile(p->pPager);
  int rc;
  ServerFcntlArg arg;
  arg.h = p->pDb->pServerShm;
  arg.p = 0;
  arg.i1 = iSlot;
  arg.i2 = bLock;
  rc = sqlite3OsFileControl(dbfd, SQLITE_FCNTL_SERVER_SHMLOCK, (void*)&arg);
  return rc;
}

/*
** Close the connection.
*/
void sqlite3ServerDisconnect(Server *p, sqlite3_file *dbfd){
  ServerDb *pDb = p->pDb;

  /* In a multi-process setup, release the lock on the client slot and
  ** clear the bit in the ServerDb.transmask bitmask. */
  if( pDb->pServerShm && p->iTransId>=0 ){
    sqlite3_mutex_enter(pDb->mutex);
    pDb->transmask &= ~((u32)1 << p->iTransId);
    sqlite3_mutex_leave(pDb->mutex);
    serverFcntlLock(p, p->iTransId, 0);
  }

  serverEnterMutex();
  pDb->nClient--;
  if( pDb->nClient==0 ){
    sqlite3_file *dbfd = sqlite3PagerFile(p->pPager);
    ServerPage *pFree;
    ServerDb **pp;

    /* Delete the journal files on shutdown if an EXCLUSIVE lock is already
    ** held (single process mode) or can be obtained (multi process mode)
    ** on the database file. 
    **
    ** TODO: Need to account for disk-full errors and the like here. It
    ** is not necessarily safe to delete journal files here. */
    int bDelete = 0;
    if( pDb->pServerShm ){
      int res;
      res = sqlite3OsLock(dbfd, EXCLUSIVE_LOCK);
      if( res==SQLITE_OK ) bDelete = 1;
    }else{
      bDelete = 1;
    }
    serverShutdownDatabase(pDb, dbfd, bDelete);

    for(pp=&g_server.pDb; *pp!=pDb; pp=&((*pp)->pNext));
    *pp = pDb->pNext;
    sqlite3_mutex_free(pDb->mutex);
    while( (pFree = pDb->pFree) ){
      pDb->pFree = pFree->pNext;
      sqlite3_free(pFree);
    }
................................................................................
  sqlite3_free(p);
}

/*
** Connect to the system.
*/
int sqlite3ServerConnect(
  Pager *pPager,                  /* Pager object */
  int eServer,                    /* 1 -> single process, 2 -> multi process */
  Server **ppOut                  /* OUT: Server handle */
){
  Server *pNew = 0;
  sqlite3_file *dbfd = sqlite3PagerFile(pPager);
  i64 aFileId[2];
  int rc;

................................................................................
      pNew->pPager = pPager;
      pNew->iTransId = -1;
      rc = serverFindDatabase(pNew, aFileId);
      if( rc!=SQLITE_OK ){
        sqlite3_free(pNew);
        pNew = 0;
      }else{
        ServerDb *pDb = pNew->pDb;
        sqlite3_mutex_enter(pNew->pDb->mutex);
        if( pDb->bInit==0 ){
          rc = serverInitDatabase(pNew, eServer);
        }

        /* If this is a multi-process connection, need to lock a 
        ** client locking-slot before continuing. */
        if( rc==SQLITE_OK && pDb->pServerShm ){
          int i;
          rc = SQLITE_BUSY;
          for(i=0; rc==SQLITE_BUSY && i<HMA_MAX_TRANSACTIONID; i++){
            if( 0==(pDb->transmask & ((u32)1 << i)) ){
              rc = serverFcntlLock(pNew, i, 1);
              if( rc==SQLITE_OK ){
                pNew->iTransId = i;
                pDb->transmask |= ((u32)1 << i);
              }
            }
          }
        }
        sqlite3_mutex_leave(pNew->pDb->mutex);
      }
    }else{
      rc = SQLITE_NOMEM_BKPT;
    }
  }

  if( rc!=SQLITE_OK && pNew ){
    sqlite3ServerDisconnect(pNew, dbfd);
    pNew = 0;
  }

  *ppOut = pNew;
  return rc;
}

/*
** Begin a transaction.
*/
int sqlite3ServerBegin(Server *p, int bReadonly){
  int rc = SQLITE_OK;

  if( p->eTrans==SERVER_TRANS_NONE ){

    ServerDb *pDb = p->pDb;
    u32 t;


    assert( p->pNext==0 );
    if( pDb->pServerShm ){
      p->eTrans = SERVER_TRANS_READWRITE;
    }else{
      assert( p->iTransId<0 );
      sqlite3_mutex_enter(pDb->mutex);

      if( bReadonly ){
        Server *pIter;
        p->iCommitId = pDb->iNextCommit;
        for(pIter=pDb->pCommit; pIter; pIter=pIter->pNext){
          if( pIter->iCommitId<p->iCommitId ){
            p->iCommitId = pIter->iCommitId;
          }
        }
        p->pNext = pDb->pReader;
        pDb->pReader = p;
        p->eTrans = SERVER_TRANS_READONLY;
      }else{
        int id;

        /* Find a transaction id to use */
        rc = SQLITE_BUSY;
        t = pDb->transmask;
        for(id=0; id<HMA_MAX_TRANSACTIONID; id++){
          if( (t & (1 << id))==0 ){
            t = t | (1 << id);
            rc = SQLITE_OK;
            break;
          }
        }
        pDb->transmask = t;
        p->eTrans = SERVER_TRANS_READWRITE;
        if( rc==SQLITE_OK ){
          p->iTransId = id;
        }
      }
      sqlite3_mutex_leave(pDb->mutex);
    }

    if( rc==SQLITE_OK && p->eTrans==SERVER_TRANS_READWRITE ){
      ServerJournal *pJrnl = &pDb->aJrnl[p->iTransId];
      sqlite3PagerServerJournal(p->pPager, pJrnl->jfd, pJrnl->zJournal);

    }
  }

  return rc;
}

static u32 *serverLockingSlot(ServerDb *pDb, u32 pgno){
  return &pDb->aSlot[pgno % HMA_PAGELOCK_SLOTS];
}

static void serverReleaseLocks(Server *p){
  ServerDb *pDb = p->pDb;
  int i;

  assert( pDb->pServerShm || sqlite3_mutex_held(pDb->mutex) );

  for(i=0; i<p->nLock; i++){

    while( 1 ){
      u32 *pSlot = serverLockingSlot(pDb, p->aLock[i]);
      u32 o = *pSlot;
      u32 n = o & ~((u32)1 << p->iTransId);
      if( slotGetWriter(n)==p->iTransId ){
        n -= ((p->iTransId + 1) << HMA_MAX_TRANSACTIONID);
      }

      if( serverCompareAndSwap(pSlot, o, n) ) break;
    }
  }

  p->nLock = 0;
}

/*
** End a transaction (and release all locks). This version runs in
** single process mode only.
*/



static void serverEndSingle(Server *p){
  Server **pp;
  ServerDb *pDb = p->pDb;
  ServerPage *pPg = 0;

  assert( p->eTrans!=SERVER_TRANS_NONE );
  assert( pDb->pServerShm==0 );

  sqlite3_mutex_enter(pDb->mutex);

  if( p->eTrans==SERVER_TRANS_READONLY ){
    /* Remove the connection from the readers list */
    for(pp=&pDb->pReader; *pp!=p; pp = &((*pp)->pNext));
    *pp = p->pNext;
  }else{
    serverReleaseLocks(p);

    /* Clear the bit in the transaction mask. */
    pDb->transmask &= ~((u32)1 << p->iTransId);

    /* If this connection is in the committers list, remove it. */
    for(pp=&pDb->pCommit; *pp; pp = &((*pp)->pNext)){
      if( *pp==p ){
        *pp = p->pNext;
        break;
      }
    }
  }

  /* See if it is possible to free any ServerPage records. If so, remove
  ** them from the linked list and hash table, but do not call sqlite3_free()
  ** on them until the mutex has been released.  */
  if( pDb->pPgFirst ){
    ServerPage *pLast = 0;
    Server *pIter;
    int iOldest = 0x7FFFFFFF;
    for(pIter=pDb->pReader; pIter; pIter=pIter->pNext){
      iOldest = MIN(iOldest, pIter->iCommitId);
    }
    for(pIter=pDb->pCommit; pIter; pIter=pIter->pNext){
      iOldest = MIN(iOldest, pIter->iCommitId);
    }

    for(pPg=pDb->pPgFirst; pPg && pPg->iCommitId<iOldest; pPg=pPg->pNext){
      if( pPg->pHashPrev ){
        pPg->pHashPrev->pHashNext = pPg->pHashNext;
      }else{
        int iHash = pPg->pgno % HMA_HASH_SIZE;
        assert( pDb->apPg[iHash]==pPg );
        pDb->apPg[iHash] = pPg->pHashNext;
      }
      if( pPg->pHashNext ){
        pPg->pHashNext->pHashPrev = pPg->pHashPrev;
      }
      pLast = pPg;
    }

    if( pLast ){
      assert( pLast->pNext==pPg );
      pLast->pNext = pDb->pFree;
      pDb->pFree = pDb->pPgFirst;
    }

    if( pPg==0 ){
      pDb->pPgFirst = pDb->pPgLast = 0;
    }else{
      pDb->pPgFirst = pPg;
    }
  }

  sqlite3_mutex_leave(pDb->mutex);

  p->pNext = 0;
  p->eTrans = SERVER_TRANS_NONE;
  p->iTransId = -1;
  p->iCommitId = 0;
}

/*
** End a transaction (and release all locks).
*/
int sqlite3ServerEnd(Server *p){
  if( p->eTrans!=SERVER_TRANS_NONE ){
    if( p->pDb->pServerShm ){
      serverReleaseLocks(p);
    }else{
      serverEndSingle(p);
    }
  }
  return SQLITE_OK;
}

int sqlite3ServerPreCommit(Server *p, ServerPage *pPg){
  ServerDb *pDb = p->pDb;
  int rc = SQLITE_OK;
  ServerPage *pIter;

  /* This should never be called in multi-process mode */
  assert( pDb->pServerShm==0 );
  if( pPg==0 ) return SQLITE_OK;

  sqlite3_mutex_enter(pDb->mutex);

  /* Assign a commit id to this transaction */
  assert( p->iCommitId==0 );
  assert( p->eTrans==SERVER_TRANS_READWRITE );
................................................................................
** If parameter bBlock is non-zero, then make this a blocking lock if
** possible.
*/
int sqlite3ServerLock(Server *p, Pgno pgno, int bWrite, int bBlock){
  int rc = SQLITE_OK;

  assert( p->eTrans==SERVER_TRANS_READWRITE 
       || (p->eTrans==SERVER_TRANS_READONLY && p->pDb->pServerShm==0)
  );
  if( p->eTrans==SERVER_TRANS_READWRITE ){
    ServerDb *pDb = p->pDb;
    int iWriter;
    int bSkip = 0;
    u32 *pSlot;

    /* Grow the aLock[] array if required */
    assert( p->iTransId>=0 );
    assert( p->nLock<=p->nAlloc );
    if( p->nLock==p->nAlloc ){
      int nNew = p->nLock ? p->nLock*2 : 256;
      u32 *aNew = sqlite3_realloc(p->aLock, nNew*sizeof(u32));
      if( aNew==0 ) return SQLITE_NOMEM_BKPT;
      memset(&aNew[p->nLock], 0, sizeof(u32) * (nNew - p->nLock));
      p->nAlloc = nNew;
      p->aLock = aNew;
    }

    /* Find the locking slot for the page in question */
    pSlot = serverLockingSlot(pDb, pgno);

    if( pDb->pServerShm==0 ) sqlite3_mutex_enter(pDb->mutex);


    while( 1 ){
      u32 o = *pSlot;
      u32 n = o;

      assert( slotGetWriter(o)<0 
          || slotReaderMask(o)==0 
          || slotReaderMask(o)==(1 << slotGetWriter(o))
      );

      iWriter = slotGetWriter(o);
      if( iWriter==p->iTransId || (bWrite==0 && (o & (1<<p->iTransId))) ){
        bSkip = 1;
        break;
      }else if( iWriter>=0 ){
        rc = SQLITE_BUSY_DEADLOCK;
      }else if( bWrite ){
        if( (slotReaderMask(o) & ~(1 << p->iTransId))==0 ){
          n += ((p->iTransId + 1) << HMA_MAX_TRANSACTIONID);
        }else{
          rc = SQLITE_BUSY_DEADLOCK;
        }
      }else{
        n |= (1 << p->iTransId);
      }

      assert( slotGetWriter(n)<0 
          || slotReaderMask(n)==0 
          || slotReaderMask(n)==(1 << slotGetWriter(n))
      );
      if( rc!=SQLITE_OK || serverCompareAndSwap(pSlot, o, n) ) break;
    }

    if( pDb->pServerShm==0 ){
      sqlite3_mutex_leave(pDb->mutex);
    }

    if( bSkip==0 && rc==SQLITE_OK ){
      p->aLock[p->nLock++] = pgno;
    }
  }

  return rc;
}

................................................................................
void sqlite3ServerReadPage(Server *p, Pgno pgno, u8 **ppData){
  if( p->eTrans==SERVER_TRANS_READONLY ){
    ServerDb *pDb = p->pDb;
    ServerPage *pIter;
    ServerPage *pBest = 0;
    int iHash = pgno % HMA_HASH_SIZE;

    /* There are no READONLY transactions in a multi process system */
    assert( pDb->pServerShm==0 );
    sqlite3_mutex_enter(pDb->mutex);

    /* Search the hash table for the oldest version of page pgno with
    ** a commit-id greater than or equal to Server.iCommitId.  */
    for(pIter=pDb->apPg[iHash]; pIter; pIter=pIter->pHashNext){
      if( pIter->pgno==pgno 
       && pIter->iCommitId>=p->iCommitId 
................................................................................
  }
}

void sqlite3ServerEndReadPage(Server *p, Pgno pgno){
  if( p->eTrans==SERVER_TRANS_READONLY ){
    ServerDb *pDb = p->pDb;
    u32 *pSlot = &pDb->aSlot[pgno % HMA_PAGELOCK_SLOTS];
    assert( pDb->pServerShm==0 );
    sqlite3_mutex_enter(pDb->mutex);
    serverIncrSlowReader(pSlot, -1);
    assert( slotGetSlowReaders(*pSlot)>=0 );
    sqlite3_mutex_leave(pDb->mutex);
  }
}

ServerPage *sqlite3ServerBuffer(Server *p){
  ServerDb *pDb = p->pDb;
  ServerPage *pRet = 0;
  assert( pDb->pServerShm==0 );
  sqlite3_mutex_enter(pDb->mutex);
  if( pDb->pFree ){
    pRet = pDb->pFree;
    pDb->pFree = pRet->pNext;
    pRet->pNext = 0;
  }
  sqlite3_mutex_leave(pDb->mutex);
................................................................................
** currently has an open readonly transaction (one started with BEGIN
** READONLY). Return false if the argument is NULL, if there is no open
** transaction, or if the open transaction is read/write.
*/
int sqlite3ServerIsReadonly(Server *p){
  return (p && p->eTrans==SERVER_TRANS_READONLY);
}

/*
** Return true if the argument is non-NULL and connects to a single-process
** server system. Return false if the argument is NULL or the system supports
** multiple processes.
*/
int sqlite3ServerIsSingleProcess(Server *p){
  return (p && p->pDb->pServerShm==0);
}

#endif /* ifdef SQLITE_SERVER_EDITION */

Changes to src/server.h.

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


48
49
50
51
52
53
54
55
56
  ServerPage *pNext;

  int iCommitId;
  ServerPage *pHashNext;
  ServerPage *pHashPrev;
};

int sqlite3ServerConnect(Pager *pPager, Server **ppOut);
void sqlite3ServerDisconnect(Server *p, sqlite3_file *dbfd);

int sqlite3ServerBegin(Server *p, int bReadonly);
int sqlite3ServerPreCommit(Server*, ServerPage*);
int sqlite3ServerEnd(Server *p);

int sqlite3ServerReleaseWriteLocks(Server *p);

int sqlite3ServerLock(Server *p, Pgno pgno, int bWrite, int bBlock);

int sqlite3ServerHasLock(Server *p, Pgno pgno, int bWrite);

ServerPage *sqlite3ServerBuffer(Server*);



/* For "BEGIN READONLY" clients. */
int sqlite3ServerIsReadonly(Server*);
void sqlite3ServerReadPage(Server*, Pgno, u8**);
void sqlite3ServerEndReadPage(Server*, Pgno);

#endif /* SQLITE_SERVER_H */
#endif /* SQLITE_SERVER_EDITION */








|













>
>









27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  ServerPage *pNext;

  int iCommitId;
  ServerPage *pHashNext;
  ServerPage *pHashPrev;
};

int sqlite3ServerConnect(Pager *pPager, int eServer, Server **ppOut);
void sqlite3ServerDisconnect(Server *p, sqlite3_file *dbfd);

int sqlite3ServerBegin(Server *p, int bReadonly);
int sqlite3ServerPreCommit(Server*, ServerPage*);
int sqlite3ServerEnd(Server *p);

int sqlite3ServerReleaseWriteLocks(Server *p);

int sqlite3ServerLock(Server *p, Pgno pgno, int bWrite, int bBlock);

int sqlite3ServerHasLock(Server *p, Pgno pgno, int bWrite);

ServerPage *sqlite3ServerBuffer(Server*);

int sqlite3ServerIsSingleProcess(Server*);

/* For "BEGIN READONLY" clients. */
int sqlite3ServerIsReadonly(Server*);
void sqlite3ServerReadPage(Server*, Pgno, u8**);
void sqlite3ServerEndReadPage(Server*, Pgno);

#endif /* SQLITE_SERVER_H */
#endif /* SQLITE_SERVER_EDITION */

Changes to src/sqlite.h.in.

1089
1090
1091
1092
1093
1094
1095




1096
1097
1098
1099
1100
1101
1102
#define SQLITE_FCNTL_WIN32_GET_HANDLE       29
#define SQLITE_FCNTL_PDB                    30
#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
#define SQLITE_FCNTL_FILEID                 34
#define SQLITE_FCNTL_SERVER_MODE            35





/* deprecated names */
#define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
#define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
#define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO









>
>
>
>







1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
#define SQLITE_FCNTL_WIN32_GET_HANDLE       29
#define SQLITE_FCNTL_PDB                    30
#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
#define SQLITE_FCNTL_FILEID                 34
#define SQLITE_FCNTL_SERVER_MODE            35
#define SQLITE_FCNTL_SERVER_SHMOPEN         36
#define SQLITE_FCNTL_SERVER_SHMOPEN2        37
#define SQLITE_FCNTL_SERVER_SHMLOCK         38
#define SQLITE_FCNTL_SERVER_SHMCLOSE        39

/* deprecated names */
#define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
#define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
#define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO


Changes to src/wal.h.

140
141
142
143
144
145
146
147
148
149
150
151
152
*/
int sqlite3WalFramesize(Wal *pWal);
#endif

/* Return the sqlite3_file object for the WAL file */
sqlite3_file *sqlite3WalFile(Wal *pWal);

#ifdef SQLITE_SERVER_EDITION
int sqlite3WalServer(Wal *pWal, Server *pServer);
#endif

#endif /* ifndef SQLITE_OMIT_WAL */
#endif /* SQLITE_WAL_H */







<
<
<
<


140
141
142
143
144
145
146




147
148
*/
int sqlite3WalFramesize(Wal *pWal);
#endif

/* Return the sqlite3_file object for the WAL file */
sqlite3_file *sqlite3WalFile(Wal *pWal);





#endif /* ifndef SQLITE_OMIT_WAL */
#endif /* SQLITE_WAL_H */

Changes to test/server2.test.

15
16
17
18
19
20
21
22
23




24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141


142
143

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix server2

source $testdir/server_common.tcl
return_if_no_server

db close




foreach f [glob -nocomplain test.db*] {
  forcedelete $f
}

#-------------------------------------------------------------------------
# Check that the *-journal* files are deleted correctly.
#
server_reset_db
do_execsql_test 1.0 {
  CREATE TABLE t1(a, b);
} {}

do_test 1.1 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal}

do_test 1.2 {
  db close
  lsort [glob -nocomplain test.db-journal/*]
} {}

server_sqlite3 db test.db
do_execsql_test 1.3 {
  CREATE TABLE t2(a, b);
} {}

server_sqlite3 db2 test.db
do_test 1.4 {
  db eval {
    BEGIN;
      INSERT INTO t1 VALUES(1, 2);
  }
  db2 eval {
    BEGIN;
      INSERT INTO t2 VALUES(3, 4);
  }
} {}

do_test 1.5 {
  db2 eval COMMIT
  db eval COMMIT
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}

do_test 1.6 {
  db close
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}

do_test 1.7 {
  db2 close
  lsort [glob -nocomplain test.db-journal/*]
} {}

#-------------------------------------------------------------------------
#
server_reset_db
server_sqlite3 db2 test.db

do_execsql_test 2.0 {
  CREATE TABLE t1(a, b);
  CREATE TABLE t2(c, d);
}

# Two concurrent transactions committed.
#
do_test 2.1 {
  db eval {
    BEGIN;
      INSERT INTO t1 VALUES(1, 2);
  }
  db2 eval {
    BEGIN;
      INSERT INTO t2 VALUES(3, 4);
  }
} {}
do_test 2.2 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}
do_test 2.3.1 { db eval COMMIT  } {}
do_test 2.3.2 { db2 eval COMMIT } {}
do_execsql_test 2.4 {SELECT * FROM t1, t2} {1 2 3 4}
do_test 2.5 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal test.db-journal/1-journal}

do_test 2.6 {
  execsql {BEGIN}
  execsql {INSERT INTO t1 VALUES(5, 6)}

  execsql {BEGIN} db2
  catchsql {INSERT INTO t1 VALUES(7, 8)} db2
} {1 {database is locked}}
do_test 2.7 {
  # Transaction is automatically rolled back in this case.
  sqlite3_get_autocommit db2
} {1}
do_test 2.8 {
  execsql COMMIT
  execsql { SELECT * FROM t1 } db2
} {1 2 5 6}
db2 close

#-------------------------------------------------------------------------
#
server_reset_db
do_execsql_test 3.0 {
  CREATE TABLE t1(a, b);
}

do_test 3.1 {
  lsort [glob -nocomplain test.db-journal/*]
} {test.db-journal/0-journal}

do_test 3.2 {
  db close
  lsort [glob -nocomplain test.db-journal/*]
} {}


finish_test








<

>
>
>
>
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>


15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix server2

source $testdir/server_common.tcl
return_if_no_server

db close

foreach {tn vfs} {1 unix-excl 2 unix} {
  server_set_vfs $vfs

  foreach f [glob -nocomplain test.db*] {
    forcedelete $f
  }

  #-------------------------------------------------------------------------
  # Check that the *-journal* files are deleted correctly.
  #
  server_reset_db
  do_execsql_test 1.0 {
    CREATE TABLE t1(a, b);
  } {}
  
  do_test $tn.1.1 {
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {test.db-journal/0-journal}
  
  do_test $tn.1.2 {
    db close
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {}
  
  server_sqlite3 db test.db
  do_execsql_test $tn.1.3 {
    CREATE TABLE t2(a, b);
  } {}
  
  server_sqlite3 db2 test.db
  do_test $tn.1.4 {
    db eval {
      BEGIN;
        INSERT INTO t1 VALUES(1, 2);
    }
    db2 eval {
      BEGIN;
        INSERT INTO t2 VALUES(3, 4);
    }
  } {}
  
  do_test $tn.1.5 {
    db2 eval COMMIT
    db eval COMMIT
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {test.db-journal/0-journal test.db-journal/1-journal}
  
  do_test $tn.1.6 {
    db close
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {test.db-journal/0-journal test.db-journal/1-journal}
  
  do_test $tn.1.7 {
    db2 close
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {}
  
  #-------------------------------------------------------------------------
  #
  server_reset_db
  server_sqlite3 db2 test.db
  
  do_execsql_test $tn.2.0 {
    CREATE TABLE t1(a, b);
    CREATE TABLE t2(c, d);
  }
  
  # Two concurrent transactions committed.
  #
  do_test $tn.2.1 {
    db eval {
      BEGIN;
        INSERT INTO t1 VALUES(1, 2);
    }
    db2 eval {
      BEGIN;
        INSERT INTO t2 VALUES(3, 4);
    }
  } {}
  do_test $tn.2.2 {
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {test.db-journal/0-journal test.db-journal/1-journal}
  do_test $tn.2.3.1 { db eval COMMIT  } {}
  do_test $tn.2.3.2 { db2 eval COMMIT } {}
  do_execsql_test 2.4 {SELECT * FROM t1, t2} {1 2 3 4}
  do_test $tn.2.5 {
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {test.db-journal/0-journal test.db-journal/1-journal}
  
  do_test $tn.2.6 {
    execsql {BEGIN}
    execsql {INSERT INTO t1 VALUES(5, 6)}
  
    execsql {BEGIN} db2
    catchsql {INSERT INTO t1 VALUES(7, 8)} db2
  } {1 {database is locked}}
  do_test $tn.2.7 {
    # Transaction is automatically rolled back in this case.
    sqlite3_get_autocommit db2
  } {1}
  do_test $tn.2.8 {
    execsql COMMIT
    execsql { SELECT * FROM t1 } db2
  } {1 2 5 6}
  db2 close
  
  #-------------------------------------------------------------------------
  #
  server_reset_db
  do_execsql_test $tn.3.0 {
    CREATE TABLE t1(a, b);
  }
  
  do_test $tn.3.1 {
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {test.db-journal/0-journal}
  
  do_test $tn.3.2 {
    db close
    lsort [glob -nocomplain test.db-journal/*-journal]
  } {}
}

finish_test

Changes to test/server3.test.

17
18
19
20
21
22
23



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


51
52
source $testdir/tester.tcl
source $testdir/lock_common.tcl
set testprefix server3

source $testdir/server_common.tcl
return_if_no_server




server_reset_db
server_sqlite3 db2 test.db

do_test 1.1 {
  db eval { CREATE TABLE t1(a, b) }
  db2 eval { CREATE TABLE t2(a, b) }
} {}

do_test 1.2 {
  db eval {
    INSERT INTO t2 VALUES(1, 2);
    BEGIN;
    INSERT INTO t1 VALUES(1, 2);
  }
} {}

do_test 1.3 { 
  list [catch { db2 eval { SELECT * FROM t1 } } msg] $msg
} {1 {database is locked}}
do_test 1.4 { 
  list [catch { db2 eval { SELECT * FROM t1 } } msg] $msg
} {1 {database is locked}}

do_test 1.4 { 
  db2 eval { SELECT * FROM t2 }
} {1 2}



finish_test








>
>
>
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
source $testdir/tester.tcl
source $testdir/lock_common.tcl
set testprefix server3

source $testdir/server_common.tcl
return_if_no_server

foreach {tn vfs} {1 unix-excl 2 unix} {
  server_set_vfs $vfs

  server_reset_db
  server_sqlite3 db2 test.db

  do_test 1.1 {
    db eval { CREATE TABLE t1(a, b) }
    db2 eval { CREATE TABLE t2(a, b) }
  } {}
  
  do_test 1.2 {
    db eval {
      INSERT INTO t2 VALUES(1, 2);
      BEGIN;
      INSERT INTO t1 VALUES(1, 2);
    }
  } {}
  
  do_test 1.3 { 
    list [catch { db2 eval { SELECT * FROM t1 } } msg] $msg
  } {1 {database is locked}}
  do_test 1.4 { 
    list [catch { db2 eval { SELECT * FROM t1 } } msg] $msg
  } {1 {database is locked}}
  
  do_test 1.4 { 
    db2 eval { SELECT * FROM t2 }
  } {1 2}
}


finish_test

Changes to test/server_common.tcl.

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34



35

  }
  return
} else {
  proc return_if_no_server {} {}
}

proc server_sqlite3 {cmd file} {
  sqlite3 $cmd $file -vfs unix-excl
}

proc server_reset_db {} {
  catch {db close}
  forcedelete test.db test.db-journal test.db-wal
  file mkdir test.db-journal
  server_sqlite3 db test.db 
}













|









>
>
>
|
>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  }
  return
} else {
  proc return_if_no_server {} {}
}

proc server_sqlite3 {cmd file} {
  sqlite3 $cmd $file -vfs $::server_vfs
}

proc server_reset_db {} {
  catch {db close}
  forcedelete test.db test.db-journal test.db-wal
  file mkdir test.db-journal
  server_sqlite3 db test.db 
}

set ::server_vfs unix-excl
proc server_set_vfs {vfs} {
  set ::server_vfs $vfs
}