SQLite4
Check-in [1d06636492]
Not logged in

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

Overview
Comment:Modify the way read-locks are taken to avoid unnecessary SQLITE4_BUSY errors.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 1d066364920758641a8f653a9e451adafc79a793
User & Date: dan 2013-11-02 10:31:12
Context
2013-11-02
18:28
Fix a problem with skipping past a section of log still in use. check-in: f96d760355 user: dan tags: trunk
10:31
Modify the way read-locks are taken to avoid unnecessary SQLITE4_BUSY errors. check-in: 1d06636492 user: dan tags: trunk
2013-11-01
19:54
Use the log to store the page-size, database size and user cookie value instead of writing these directly to the database header. check-in: 37983095fd user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/bt_lock.c.

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
...
176
177
178
179
180
181
182










183
184
185
186
187
188
189
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
...
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
...
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
...
441
442
443
444
445
446
447


448
449
450
451
452

453
454
455
456
457
458
459
**   Each new connection is assigned a "debug-id". This contributes 
**   nothing to the operation of the library, but sometimes makes it 
**   easier to debug various problems.
*/
static struct SharedData {
  BtShared *pDatabase;            /* Linked list of all Database objects */
  int iDebugId;                   /* Next free debugging id */
} gShared = {0, 0};

struct BtFile {
  BtFile *pNext;
  bt_file *pFd;
};

struct BtShared {
................................................................................
    }

    sqlite4_mutex_leave(pShared->pClientMutex);
  }

  return rc;
}











/*
** Attempt to obtain the lock identified by the iLock and bExcl parameters.
** If successful, return SQLITE4_OK. If the lock cannot be obtained because 
** there exists some other conflicting lock, return SQLITE4_BUSY. If some 
** other error occurs, return an SQLite4 error code.
**
................................................................................
  int bBlock                      /* True for a blocking lock */
){
  int rc;
  while( 1 ){
    rc = btLockLockopNonblocking(p, iLock, eOp);
    if( rc!=SQLITE4_BUSY || bBlock==0 ) break;
    /* todo: Fix blocking locks */
    usleep(10000);
  }
  return rc;
}


/*
** Connect to the database as a read/write connection. If recovery
................................................................................
  int nName;
  BtShared *pShared;

  zName = sqlite4BtPagerFilename((BtPager*)p, BT_PAGERFILE_DATABASE);
  nName = strlen(zName);

  btLockMutexEnter(p->pEnv);
  p->iDebugId = gShared.iDebugId++;
  for(pShared=gShared.pDatabase; pShared; pShared=pShared->pNext){
    if( pShared->nName==nName && 0==memcmp(zName, pShared->zName, nName) ){
      break;
    }
  }

  if( pShared==0 ){
    sqlite4_mutex *pMutex;
................................................................................
      pMutex = 0;
      rc = btErrorBkpt(SQLITE4_NOMEM);
    }else{
      memset(pShared, 0, sizeof(BtShared));
      pShared->nName = nName;
      pShared->zName = (char *)&pShared[1];
      memcpy(pShared->zName, zName, nName+1);
      pShared->pNext = gShared.pDatabase;
      pShared->pClientMutex = pMutex;
      gShared.pDatabase = pShared;
    }
  }

  if( rc==SQLITE4_OK ){
    pShared->nRef++;
  }
  btLockMutexLeave(p->pEnv);
................................................................................
  }

  btLockMutexEnter(p->pEnv);
  pShared->nRef--;
  if( pShared->nRef==0 ){
    int i;
    BtShared **ppS;
    for(ppS=&gShared.pDatabase; *ppS!=pShared; ppS=&(*ppS)->pNext);
    *ppS = (*ppS)->pNext;

    sqlite4_mutex_free(pShared->pClientMutex);
    for(i=0; i<pShared->nShmChunk; i++){
      sqlite4_free(p->pEnv, pShared->apShmChunk[i]);
    }
    sqlite4_free(p->pEnv, pShared->apShmChunk);
    sqlite4_free(p->pEnv, pShared);
  }
  btLockMutexLeave(p->pEnv);
  return rc;
}










/* 
** Obtain a READER lock. 
**
** Argument aLog points to an array of 6 frame addresses. These are the 
** first and last frames in each of log regions A, B and C. Argument 
** aLock points to the array of read-lock slots in shared memory.
................................................................................
  ** the snapshot header). If so, the caller will figure it out.  */
  if( (iFirst<aLog[0] || iFirst>aLog[1])
   && (iFirst<aLog[2] || iFirst>aLog[3])
   && (iFirst<aLog[4] || iFirst>aLog[5])
  ){
    iLast = 0;
  }





  if( iLast==0 ){
    rc = btLockLockop(pLock, BT_LOCK_READER_DBONLY, BT_LOCK_SHARED, 0);
  }else{

    int nAttempt = 100;           /* Remaining lock attempts */

    while( rc==SQLITE4_BUSY && (nAttempt--)>0 ){

      int iIdxFirst = sqlite4BtLogFrameToIdx(aLog, iFirst);
      int iIdxLast = sqlite4BtLogFrameToIdx(aLog, iLast);

      assert( iIdxFirst>=0 && iIdxLast>=0 );

      /* Try to find a slot populated with the values required. */
      for(i=0; i<BT_NREADER; i++){
        if( aSlot[i].iFirst==iFirst && aSlot[i].iLast==iLast ) break;



      }

      /* Or, if there is no slot with the required values - try to create one */
      if( i==BT_NREADER ){
        for(i=0; i<BT_NREADER; i++){
          rc = btLockLockop(pLock, BT_LOCK_READER0 + i, BT_LOCK_EXCL, 0);
          if( rc==SQLITE4_OK ){
................................................................................
        if( rc==SQLITE4_OK ){
          int iSF = sqlite4BtLogFrameToIdx(aLog, aSlot[i].iFirst);
          int iSL = sqlite4BtLogFrameToIdx(aLog, aSlot[i].iLast);
          if( iSF>iIdxFirst || iSL>iIdxLast || iSF<0 || iSL<0 ){
            btLockLockop(pLock, BT_LOCK_READER0 + i, BT_LOCK_UNLOCK, 0);
            rc = SQLITE4_BUSY;
          }


        }
      }
    }
  }


  return rc;
}

/*
** Release the READER lock currently held by connection pLock.
*/
int sqlite4BtLockReaderUnlock(BtLock *pLock){







|







 







>
>
>
>
>
>
>
>
>
>







 







|







 







|
|







 







|

|







 







|












>
>
>
>
>
>
>
>
>







 







>
>
>
>




>


|
>







|
>
>
>







 







>
>





>







46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
...
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
...
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
...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
**   Each new connection is assigned a "debug-id". This contributes 
**   nothing to the operation of the library, but sometimes makes it 
**   easier to debug various problems.
*/
static struct SharedData {
  BtShared *pDatabase;            /* Linked list of all Database objects */
  int iDebugId;                   /* Next free debugging id */
} gBtShared = {0, 0};

struct BtFile {
  BtFile *pNext;
  bt_file *pFd;
};

struct BtShared {
................................................................................
    }

    sqlite4_mutex_leave(pShared->pClientMutex);
  }

  return rc;
}

static void btLockDelay(void){
  usleep(10000);
#if 0
  static int nCall = 0;
  nCall++;
  fprintf(stderr, "%d delay\n", nCall);
  fflush(stderr);
#endif
}

/*
** Attempt to obtain the lock identified by the iLock and bExcl parameters.
** If successful, return SQLITE4_OK. If the lock cannot be obtained because 
** there exists some other conflicting lock, return SQLITE4_BUSY. If some 
** other error occurs, return an SQLite4 error code.
**
................................................................................
  int bBlock                      /* True for a blocking lock */
){
  int rc;
  while( 1 ){
    rc = btLockLockopNonblocking(p, iLock, eOp);
    if( rc!=SQLITE4_BUSY || bBlock==0 ) break;
    /* todo: Fix blocking locks */
    btLockDelay();
  }
  return rc;
}


/*
** Connect to the database as a read/write connection. If recovery
................................................................................
  int nName;
  BtShared *pShared;

  zName = sqlite4BtPagerFilename((BtPager*)p, BT_PAGERFILE_DATABASE);
  nName = strlen(zName);

  btLockMutexEnter(p->pEnv);
  p->iDebugId = gBtShared.iDebugId++;
  for(pShared=gBtShared.pDatabase; pShared; pShared=pShared->pNext){
    if( pShared->nName==nName && 0==memcmp(zName, pShared->zName, nName) ){
      break;
    }
  }

  if( pShared==0 ){
    sqlite4_mutex *pMutex;
................................................................................
      pMutex = 0;
      rc = btErrorBkpt(SQLITE4_NOMEM);
    }else{
      memset(pShared, 0, sizeof(BtShared));
      pShared->nName = nName;
      pShared->zName = (char *)&pShared[1];
      memcpy(pShared->zName, zName, nName+1);
      pShared->pNext = gBtShared.pDatabase;
      pShared->pClientMutex = pMutex;
      gBtShared.pDatabase = pShared;
    }
  }

  if( rc==SQLITE4_OK ){
    pShared->nRef++;
  }
  btLockMutexLeave(p->pEnv);
................................................................................
  }

  btLockMutexEnter(p->pEnv);
  pShared->nRef--;
  if( pShared->nRef==0 ){
    int i;
    BtShared **ppS;
    for(ppS=&gBtShared.pDatabase; *ppS!=pShared; ppS=&(*ppS)->pNext);
    *ppS = (*ppS)->pNext;

    sqlite4_mutex_free(pShared->pClientMutex);
    for(i=0; i<pShared->nShmChunk; i++){
      sqlite4_free(p->pEnv, pShared->apShmChunk[i]);
    }
    sqlite4_free(p->pEnv, pShared->apShmChunk);
    sqlite4_free(p->pEnv, pShared);
  }
  btLockMutexLeave(p->pEnv);
  return rc;
}

#ifndef NDEBUG
static void assertNoLockedSlots(BtLock *pLock){
  u32 mask = (1 << (BT_LOCK_READER0+BT_NREADER+1)) - (1 << BT_LOCK_READER0);
  assert( (pLock->mExclLock & mask)==0 );
}
#else
# define assertNoLockedSlots(x)
#endif

/* 
** Obtain a READER lock. 
**
** Argument aLog points to an array of 6 frame addresses. These are the 
** first and last frames in each of log regions A, B and C. Argument 
** aLock points to the array of read-lock slots in shared memory.
................................................................................
  ** the snapshot header). If so, the caller will figure it out.  */
  if( (iFirst<aLog[0] || iFirst>aLog[1])
   && (iFirst<aLog[2] || iFirst>aLog[3])
   && (iFirst<aLog[4] || iFirst>aLog[5])
  ){
    iLast = 0;
  }

BtReadSlot ss[BT_NREADER];
memcpy(ss, aSlot, BT_NREADER*sizeof(BtReadSlot));
int iOk = -1;

  if( iLast==0 ){
    rc = btLockLockop(pLock, BT_LOCK_READER_DBONLY, BT_LOCK_SHARED, 0);
  }else{
    const int nMaxRetry = 100;
    int nAttempt = 100;           /* Remaining lock attempts */

    for(nAttempt=0; rc==SQLITE4_BUSY && nAttempt<nMaxRetry; nAttempt++){

      int iIdxFirst = sqlite4BtLogFrameToIdx(aLog, iFirst);
      int iIdxLast = sqlite4BtLogFrameToIdx(aLog, iLast);

      assert( iIdxFirst>=0 && iIdxLast>=0 );

      /* Try to find a slot populated with the values required. */
      for(i=0; i<BT_NREADER; i++){
        if( aSlot[i].iFirst==iFirst && aSlot[i].iLast==iLast ){
          iOk = i;
          break;
        }
      }

      /* Or, if there is no slot with the required values - try to create one */
      if( i==BT_NREADER ){
        for(i=0; i<BT_NREADER; i++){
          rc = btLockLockop(pLock, BT_LOCK_READER0 + i, BT_LOCK_EXCL, 0);
          if( rc==SQLITE4_OK ){
................................................................................
        if( rc==SQLITE4_OK ){
          int iSF = sqlite4BtLogFrameToIdx(aLog, aSlot[i].iFirst);
          int iSL = sqlite4BtLogFrameToIdx(aLog, aSlot[i].iLast);
          if( iSF>iIdxFirst || iSL>iIdxLast || iSF<0 || iSL<0 ){
            btLockLockop(pLock, BT_LOCK_READER0 + i, BT_LOCK_UNLOCK, 0);
            rc = SQLITE4_BUSY;
          }
        }else if( rc==SQLITE4_BUSY && nAttempt>(nMaxRetry/2) ){
          btLockDelay();
        }
      }
    }
  }

  assertNoLockedSlots(pLock);
  return rc;
}

/*
** Release the READER lock currently held by connection pLock.
*/
int sqlite4BtLockReaderUnlock(BtLock *pLock){