SQLite

Check-in [83add34f64]
Login

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

Overview
Comment:fix for locking in Windows (CVS 760)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 83add34f64895a4b465881213eba82f3b1f5c964
User & Date: mike 2002-10-17 00:38:54.000
Context
2002-10-17
09:01
Minor coding fix in getting the windows platform version, when multithreading (CVS 761) (check-in: b53ab71d07 user: mike tags: trunk)
00:38
fix for locking in Windows (CVS 760) (check-in: 83add34f64 user: mike tags: trunk)
2002-10-12
13:44
Remove the call to srand() and add better comments to the sqliteOsRandomSeed() routine. Ticket #163. (CVS 759) (check-in: d87a886d8f user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/os.c.
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#endif
#if OS_WIN
  DWORD got;
  SimulateIOError(SQLITE_IOERR);
  if( !ReadFile(id->h, pBuf, amt, &got, 0) ){
    got = 0;
  }
  if( got==amt ){
    return SQLITE_OK;
  }else{
    return SQLITE_IOERR;
  }
#endif
}








|







488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#endif
#if OS_WIN
  DWORD got;
  SimulateIOError(SQLITE_IOERR);
  if( !ReadFile(id->h, pBuf, amt, &got, 0) ){
    got = 0;
  }
  if( (int)got==amt ){
    return SQLITE_OK;
  }else{
    return SQLITE_IOERR;
  }
#endif
}

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
** A lock is obtained on byte 0 before acquiring either a read lock or
** a write lock.  This prevents two processes from attempting to get a
** lock at a same time.  The semantics of sqliteOsReadLock() require that
** if there is already a write lock, that lock is converted into a read
** lock atomically.  The lock on byte 0 allows us to drop the old write
** lock and get the read lock without another process jumping into the
** middle and messing us up.  The same argument applies to sqliteOsWriteLock().
**




** There are a finite number of read locks under windows.  That number
** is determined by the following variable:
*/

#define MX_LOCKBYTE 10240


















































/*
** Change the status of the lock on the file "id" to be a readlock.
** If the file was write locked, then this reduces the lock to a read.
** If the file was read locked, then this acquires a new read lock.
**
** Return SQLITE_OK on success and SQLITE_BUSY on failure.
*/

int sqliteOsReadLock(OsFile *id){
#if OS_UNIX
  int rc;
  sqliteOsEnterMutex();
  if( id->pLock->cnt>0 ){
    if( !id->locked ){
      id->pLock->cnt++;








>
>
>
>



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








>







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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
** A lock is obtained on byte 0 before acquiring either a read lock or
** a write lock.  This prevents two processes from attempting to get a
** lock at a same time.  The semantics of sqliteOsReadLock() require that
** if there is already a write lock, that lock is converted into a read
** lock atomically.  The lock on byte 0 allows us to drop the old write
** lock and get the read lock without another process jumping into the
** middle and messing us up.  The same argument applies to sqliteOsWriteLock().
**
** Locks must be obtained in an area that does not overlap the "real data area"
** otherwise read/write operations will conflict with lock operations. Locking beyond EOF
** is allowed in windows.
**
** There are a finite number of read locks under windows.  That number
** is determined by the following variable:
*/

#define MX_LOCKBYTE 0xFFF0

#if OS_WIN

// get the platform id to decide how to calculate the lock offset

int mkPlatformId(void){
 
 static long init=0;
 static int pid=VER_PLATFORM_WIN32_WINDOWS;
 OSVERSIONINFOA info;	
 
 if (!init) {
  if (InterlockedIncrement(&init)==1)
   {
    info.dwOSVersionInfoSize=sizeof(info);
   if (GetVersionEx(&info)) pid=info.dwPlatformId;
   }
  } 
 return pid;  
}

// locks and unlocks beyond eof. uses platformid to move the lock as far as possible.
int mklock(HANDLE h, WORD base, WORD size)
{
 if (mkPlatformId()==VER_PLATFORM_WIN32_WINDOWS)
  return LockFile(h,0xFFFF0000+base,0,size,0);
 else
  return LockFile(h,base,0xFFFFFFFF,size,0);
}

int mkunlock(HANDLE h, WORD base, WORD size)
{
 if (mkPlatformId()==VER_PLATFORM_WIN32_WINDOWS)
  return UnlockFile(h,0xFFFF0000+base,0,size,0);
 else
  return UnlockFile(h,base,0xFFFFFFFF,size,0);
}

//obtain the sync lock on a handle

void synclock(HANDLE h){ 
 while (!mklock(h,0,1)) Sleep(1);
}

void syncunlock(HANDLE h){ 
 mkunlock(h,0,1);
}

#endif

/*
** Change the status of the lock on the file "id" to be a readlock.
** If the file was write locked, then this reduces the lock to a read.
** If the file was read locked, then this acquires a new read lock.
**
** Return SQLITE_OK on success and SQLITE_BUSY on failure.
*/

int sqliteOsReadLock(OsFile *id){
#if OS_UNIX
  int rc;
  sqliteOsEnterMutex();
  if( id->pLock->cnt>0 ){
    if( !id->locked ){
      id->pLock->cnt++;
670
671
672
673
674
675
676
677
678

679
680
681
682
683
684
685
686
687
688
#if OS_WIN
  int rc;
  if( id->locked>0 ){
    rc = SQLITE_OK;
  }else{
    int lk = (sqliteRandomInteger() & 0x7ffffff)%MX_LOCKBYTE + 1;
    int res;
    if( (res = LockFile(id->h, 0, 0, 1, 0))!=0 ){
      UnlockFile(id->h, 1, 0, MX_LOCKBYTE, 0);

      res = LockFile(id->h, lk, 0, 1, 0);
      UnlockFile(id->h, 0, 0, 1, 0);
    }
    if( res ){
      id->locked = lk;
      rc = SQLITE_OK;
    }else{
      rc = SQLITE_BUSY;
    }
  }







|
|
>
|
|
|







725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#if OS_WIN
  int rc;
  if( id->locked>0 ){
    rc = SQLITE_OK;
  }else{
    int lk = (sqliteRandomInteger() & 0x7ffffff)%MX_LOCKBYTE + 1;
    int res;
    
    synclock(id->h);
    if (id->locked<0) mkunlock(id->h,1,MX_LOCKBYTE); // release write lock if we have it
    res=mklock(id->h,lk,1);
    syncunlock(id->h);
    
    if( res ){
      id->locked = lk;
      rc = SQLITE_OK;
    }else{
      rc = SQLITE_BUSY;
    }
  }
718
719
720
721
722
723
724
725
726
727
728
729
730
731



732
733
734
735
736
737
738
739
740
#endif
#if OS_WIN
  int rc;
  if( id->locked<0 ){
    rc = SQLITE_OK;
  }else{
    int res;
    if( (res = LockFile(id->h, 0, 0, 1, 0))!=0 ){
      if( id->locked==0 || UnlockFile(id->h, id->locked, 0, 1, 0) ){
        res = LockFile(id->h, 1, 0, MX_LOCKBYTE, 0);
      }else{
        res = 0;
      }
      UnlockFile(id->h, 0, 0, 1, 0);



    }
    if( res ){
      id->locked = -1;
      rc = SQLITE_OK;
    }else{
      rc = SQLITE_BUSY;
    }
  }
  return rc;







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







774
775
776
777
778
779
780





781
782
783
784
785
786
787
788
789
790
791
792
793
794
#endif
#if OS_WIN
  int rc;
  if( id->locked<0 ){
    rc = SQLITE_OK;
  }else{
    int res;





    
    synclock(id->h);
    if (id->locked>0) mkunlock(id->h,id->locked,1); // release read lock
    res=mklock(id->h,1,MX_LOCKBYTE);
    syncunlock(id->h);
   
    if(res){
      id->locked = -1;
      rc = SQLITE_OK;
    }else{
      rc = SQLITE_BUSY;
    }
  }
  return rc;
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
  }
  sqliteOsLeaveMutex();
  id->locked = 0;
  return rc;
#endif
#if OS_WIN
  int rc;
  if( id->locked==0 ){
    rc = SQLITE_OK;
  }else if( id->locked<0 ){
    UnlockFile(id->h, 1, 0, MX_LOCKBYTE, 0);
    rc = SQLITE_OK;
    id->locked = 0;
  }else{
    UnlockFile(id->h, id->locked, 0, 1, 0);
    rc = SQLITE_OK;
    id->locked = 0;
  }

  return rc;
#endif
}

/*
** Get information to seed the random number generator.  The seed
** is written into the buffer zBuf[256].  The calling function must
** supply a sufficiently large buffer.







<
<
|
|
<
|
<
<
<
|

>
|







822
823
824
825
826
827
828


829
830

831



832
833
834
835
836
837
838
839
840
841
842
  }
  sqliteOsLeaveMutex();
  id->locked = 0;
  return rc;
#endif
#if OS_WIN
  int rc;


  if(id->locked<0 ) {
    mkunlock(id->h,1,MX_LOCKBYTE);

  }else if (id->locked>0) {



    mkunlock(id->h,id->locked,1);
  }
  id->locked = 0;
  return SQLITE_OK;
#endif
}

/*
** Get information to seed the random number generator.  The seed
** is written into the buffer zBuf[256].  The calling function must
** supply a sufficiently large buffer.