1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Fix the locking protocol. (CVS 280)

FossilOrigin-Name: 484b82d8a1c84f3d9725a509de93276b9fa9b294
This commit is contained in:
drh
2001-10-09 04:19:46 +00:00
parent f57b339988
commit ad75e9874b
13 changed files with 413 additions and 134 deletions

View File

@ -25,7 +25,7 @@
** ROLLBACK
** PRAGMA
**
** $Id: build.c,v 1.45 2001/10/08 13:22:32 drh Exp $
** $Id: build.c,v 1.46 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@ -573,7 +573,7 @@ void sqliteEndTable(Parse *pParse, Token *pEnd){
/* Add the table to the in-memory representation of the database.
*/
assert( pParse->nameClash==0 || pParse->initFlag==0 );
assert( pParse->nameClash==0 || pParse->initFlag==1 );
if( pParse->explain==0 && pParse->nameClash==0 ){
sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
pParse->pNewTable = 0;

View File

@ -12,7 +12,7 @@
** This is the header file for the generic hash-table implemenation
** used in SQLite.
**
** $Id: hash.h,v 1.1 2001/09/22 18:12:10 drh Exp $
** $Id: hash.h,v 1.2 2001/10/09 04:19:47 drh Exp $
*/
#ifndef _SQLITE_HASH_H_
#define _SQLITE_HASH_H_
@ -30,7 +30,7 @@ typedef struct HashElem HashElem;
** this structure opaque.
*/
struct Hash {
char keyClass; /* SQLITE_HASH_INT, ..._STRING, or _BINARY */
char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
char copyKey; /* True if copy of key made on insert */
int count; /* Number of entries in this table */
HashElem *first; /* The first element of the array */

220
src/os.c
View File

@ -37,6 +37,73 @@
# include <winbase.h>
#endif
#if OS_UNIX
/*
** An instance of the following structure serves as the key used
** to locate a particular lockInfo structure given its inode.
*/
struct inodeKey {
dev_t dev; /* Device number */
ino_t ino; /* Inode number */
};
/*
** An instance of the following structure is allocated for each inode.
** A single inode can have multiple file descriptors, so each OsFile structure
** contains a pointer to an instance of this object.
*/
struct lockInfo {
struct inodeKey key; /* The lookup key */
int cnt; /* 0: unlocked. -1: write lock. >=1: read lock */
int nRef; /* Number of pointers to this structure */
};
/*
** This hash table maps inodes (in the form of inodeKey structures) into
** pointers to lockInfo structures.
*/
static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
/*
** Given a file descriptor, locate a lockInfo structure that describes
** that file descriptor. Create a new one if necessary.
*/
static struct lockInfo *findLockInfo(int fd){
int rc;
struct inodeKey key;
struct stat statbuf;
struct lockInfo *pInfo;
rc = fstat(fd, &statbuf);
if( rc!=0 ) return 0;
key.dev = statbuf.st_dev;
key.ino = statbuf.st_ino;
pInfo = (struct lockInfo*)sqliteHashFind(&lockHash, &key, sizeof(key));
if( pInfo==0 ){
pInfo = sqliteMalloc( sizeof(*pInfo) );
pInfo->key = key;
pInfo->nRef = 1;
pInfo->cnt = 0;
sqliteHashInsert(&lockHash, &pInfo->key, sizeof(key), pInfo);
}else{
pInfo->nRef++;
}
return pInfo;
}
/*
** Release a lockInfo structure previously allocated by findLockInfo().
*/
static void releaseLockInfo(struct lockInfo *pInfo){
pInfo->nRef--;
if( pInfo->nRef==0 ){
sqliteHashInsert(&lockHash, &pInfo->key, sizeof(pInfo->key), 0);
sqliteFree(pInfo);
}
}
#endif
/*
** Delete the named file
*/
@ -95,17 +162,25 @@ int sqliteOsOpenReadWrite(
int *pReadonly
){
#if OS_UNIX
int fd = open(zFilename, O_RDWR|O_CREAT, 0644);
if( fd<0 ){
fd = open(zFilename, O_RDONLY);
if( fd<0 ){
OsFile s;
s.fd = open(zFilename, O_RDWR|O_CREAT, 0644);
if( s.fd<0 ){
s.fd = open(zFilename, O_RDONLY);
if( s.fd<0 ){
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
}else{
*pReadonly = 0;
}
*pResult = fd;
sqliteOsEnterMutex();
s.pLock = findLockInfo(s.fd);
sqliteOsLeaveMutex();
if( s.pLock==0 ){
close(s.fd);
return SQLITE_NOMEM;
}
*pResult = s;
return SQLITE_OK;
#endif
#if OS_WIN
@ -152,18 +227,25 @@ int sqliteOsOpenReadWrite(
*/
int sqliteOsOpenExclusive(const char *zFilename, OsFile *pResult){
#if OS_UNIX
int fd;
OsFile s;
if( access(zFilename, 0)==0 ){
return SQLITE_CANTOPEN;
}
#ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
#endif
fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
if( fd<0 ){
s.fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
if( s.fd<0 ){
return SQLITE_CANTOPEN;
}
*pResult = fd;
sqliteOsEnterMutex();
s.pLock = findLockInfo(s.fd);
sqliteOsLeaveMutex();
if( s.pLock==0 ){
close(s.fd);
return SQLITE_NOMEM;
}
*pResult = s;
return SQLITE_OK;
#endif
#if OS_WIN
@ -192,11 +274,19 @@ int sqliteOsOpenExclusive(const char *zFilename, OsFile *pResult){
*/
int sqliteOsOpenReadOnly(const char *zFilename, OsFile *pResult){
#if OS_UNIX
int fd = open(zFilename, O_RDONLY);
if( fd<0 ){
OsFile s;
s.fd = open(zFilename, O_RDONLY);
if( s.fd<0 ){
return SQLITE_CANTOPEN;
}
*pResult = fd;
sqliteOsEnterMutex();
s.pLock = findLockInfo(s.fd);
sqliteOsLeaveMutex();
if( s.pLock==0 ){
close(s.fd);
return SQLITE_NOMEM;
}
*pResult = s;
return SQLITE_OK;
#endif
#if OS_WIN
@ -246,7 +336,7 @@ int sqliteOsTempFileName(char *zBuf){
sprintf(zBuf, "%s/sqlite_", zDir);
j = strlen(zBuf);
for(i=0; i<15; i++){
int n = rand() % sizeof(zChars);
int n = sqliteRandomByte() % (sizeof(zChars)-1);
zBuf[j++] = zChars[n];
}
zBuf[j] = 0;
@ -264,7 +354,7 @@ int sqliteOsTempFileName(char *zBuf){
sprintf(zBuf, "%s/sqlite_", zTempPath);
j = strlen(zBuf);
for(i=0; i<15; i++){
int n = rand() % sizeof(zChars);
int n = sqliteRandomByte() % sizeof(zChars);
zBuf[j++] = zChars[n];
}
zBuf[j] = 0;
@ -279,7 +369,10 @@ int sqliteOsTempFileName(char *zBuf){
*/
int sqliteOsClose(OsFile id){
#if OS_UNIX
close(id);
close(id.fd);
sqliteOsEnterMutex();
releaseLockInfo(id.pLock);
sqliteOsLeaveMutex();
return SQLITE_OK;
#endif
#if OS_WIN
@ -295,7 +388,7 @@ int sqliteOsClose(OsFile id){
int sqliteOsRead(OsFile id, void *pBuf, int amt){
#if OS_UNIX
int got;
got = read(id, pBuf, amt);
got = read(id.fd, pBuf, amt);
if( got<0 ) got = 0;
return got==amt ? SQLITE_OK : SQLITE_IOERR;
#endif
@ -315,7 +408,7 @@ int sqliteOsRead(OsFile id, void *pBuf, int amt){
int sqliteOsWrite(OsFile id, const void *pBuf, int amt){
#if OS_UNIX
int wrote;
wrote = write(id, pBuf, amt);
wrote = write(id.fd, pBuf, amt);
if( wrote<amt ) return SQLITE_FULL;
return SQLITE_OK;
#endif
@ -333,7 +426,7 @@ int sqliteOsWrite(OsFile id, const void *pBuf, int amt){
*/
int sqliteOsSeek(OsFile id, int offset){
#if OS_UNIX
lseek(id, offset, SEEK_SET);
lseek(id.fd, offset, SEEK_SET);
return SQLITE_OK;
#endif
#if OS_WIN
@ -347,7 +440,7 @@ int sqliteOsSeek(OsFile id, int offset){
*/
int sqliteOsSync(OsFile id){
#if OS_UNIX
return fsync(id)==0 ? SQLITE_OK : SQLITE_IOERR;
return fsync(id.fd)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
return FlushFileBuffers(id) ? SQLITE_OK : SQLITE_IOERR;
@ -359,7 +452,7 @@ int sqliteOsSync(OsFile id){
*/
int sqliteOsTruncate(OsFile id, int nByte){
#if OS_UNIX
return ftruncate(id, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
return ftruncate(id.fd, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
SetFilePointer(id, nByte, 0, FILE_BEGIN);
@ -374,7 +467,7 @@ int sqliteOsTruncate(OsFile id, int nByte){
int sqliteOsFileSize(OsFile id, int *pSize){
#if OS_UNIX
struct stat buf;
if( fstat(id, &buf)!=0 ){
if( fstat(id.fd, &buf)!=0 ){
return SQLITE_IOERR;
}
*pSize = buf.st_size;
@ -386,21 +479,41 @@ int sqliteOsFileSize(OsFile id, int *pSize){
#endif
}
/*
** Get a read or write lock on a file.
*/
int sqliteOsLock(OsFile id, int wrlock){
#if OS_UNIX
int rc;
struct flock lock;
lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id, F_SETLK, &lock);
if( rc ){
fcntl(id, F_GETLK, &lock); /* For debugging */
int needSysLock;
sqliteOsEnterMutex();
if( wrlock ){
if( id.pLock->cnt!=0 ){
rc = SQLITE_BUSY;
}else{
rc = SQLITE_OK;
id.pLock->cnt = -1;
needSysLock = 1;
}
}else{
if( id.pLock<0 ){
rc = SQLITE_BUSY;
}else{
rc = SQLITE_OK;
needSysLock = id.pLock->cnt==0;
id.pLock->cnt++;
}
}
return rc==0 ? SQLITE_OK : SQLITE_BUSY;
sqliteOsLeaveMutex();
if( rc==SQLITE_OK && needSysLock ){
struct flock lock;
lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id.fd, F_SETLK, &lock)==0 ? SQLITE_OK : SQLITE_BUSY;
}
return rc;
#endif
#if OS_WIN
if( !LockFile(id, 0, 0, 1024, 0) ){
@ -416,12 +529,28 @@ int sqliteOsLock(OsFile id, int wrlock){
int sqliteOsUnlock(OsFile id){
#if OS_UNIX
int rc;
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id, F_SETLK, &lock);
return rc==0 ? SQLITE_OK : SQLITE_IOERR;
int needSysUnlock;
sqliteOsEnterMutex();
if( id.pLock->cnt<0 ){
needSysUnlock = 1;
id.pLock->cnt = 0;
}else if( id.pLock->cnt>0 ){
id.pLock->cnt--;
needSysUnlock = id.pLock->cnt==0;
}else{
rc = SQLITE_OK;
needSysUnlock = 0;
}
sqliteOsLeaveMutex();
if( needSysUnlock ){
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id.fd, F_SETLK, &lock)==0 ? SQLITE_OK : SQLITE_IOERR;
}
return rc;
#endif
#if OS_WIN
return UnlockFile(id, 0, 0, 1024, 0) ? SQLITE_OK : SQLITE_IOERR;
@ -469,3 +598,24 @@ int sqliteOsSleep(int ms){
return ms;
#endif
}
/*
** The following pair of routine implement mutual exclusion for
** multi-threaded processes. Only a single thread is allowed to
** executed code that is surrounded by EnterMutex() and LeaveMutex().
**
** SQLite uses only a single Mutex. There is not much critical
** code and what little there is executes quickly and without blocking.
**
****** TBD: The mutex is currently unimplemented. Until it is
****** implemented, SQLite is not threadsafe.
*/
static int inMutex = 0;
void sqliteOsEnterMutex(){
assert( !inMutex );
inMutex = 1;
}
void sqliteOsLeaveMutex(){
assert( inMutex );
inMutex = 0;
}

View File

@ -21,7 +21,11 @@
** A handle for an open file is stored in an OsFile object.
*/
#if OS_UNIX
typedef int OsFile;
typedef struct OsFile OsFile;
struct OsFile {
struct lockInfo *pLock; /* Information about locks on this inode */
int fd; /* The file descriptor */
};
# define SQLITE_TEMPNAME_SIZE 200
# if defined(HAVE_USLEEP) && HAVE_USLEEP
# define SQLITE_MIN_SLEEP_MS 1
@ -55,6 +59,8 @@ int sqliteOsLock(OsFile, int wrlock);
int sqliteOsUnlock(OsFile);
int sqliteOsRandomSeed(char*);
int sqliteOsSleep(int ms);
void sqliteOsEnterMutex();
void sqliteOsLeaveMutex();

View File

@ -15,17 +15,27 @@
** Random numbers are used by some of the database backends in order
** to generate random integer keys for tables or random filenames.
**
** $Id: random.c,v 1.7 2001/09/23 19:46:52 drh Exp $
** $Id: random.c,v 1.8 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
/*
** Get a single 8-bit random value from the RC4 PRNG.
** Get a single 8-bit random value from the RC4 PRNG. The Mutex
** must be held while executing this routine.
*/
int sqliteRandomByte(sqlite *db){
static int randomByte(){
int t;
/* All threads share a single random number generator.
** This structure is the current state of the generator.
*/
static struct {
int isInit; /* True if initialized */
int i, j; /* State variables */
int s[256]; /* State variables */
} prng;
/* Initialize the state of the random number generator once,
** the first time this routine is called. The seed value does
** not need to contain a lot of randomness since we are not
@ -35,46 +45,59 @@ int sqliteRandomByte(sqlite *db){
** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
** number generator) not as an encryption device.
*/
if( !db->prng.isInit ){
if( !prng.isInit ){
int i;
char k[256];
db->prng.j = 0;
db->prng.i = 0;
prng.j = 0;
prng.i = 0;
sqliteOsRandomSeed(k);
for(i=0; i<256; i++){
db->prng.s[i] = i;
prng.s[i] = i;
}
for(i=0; i<256; i++){
int t;
db->prng.j = (db->prng.j + db->prng.s[i] + k[i]) & 0xff;
t = db->prng.s[db->prng.j];
db->prng.s[db->prng.j] = db->prng.s[i];
db->prng.s[i] = t;
prng.j = (prng.j + prng.s[i] + k[i]) & 0xff;
t = prng.s[prng.j];
prng.s[prng.j] = prng.s[i];
prng.s[i] = t;
}
db->prng.isInit = 1;
prng.isInit = 1;
}
/* Generate and return single random byte
*/
db->prng.i = (db->prng.i + 1) & 0xff;
db->prng.j = (db->prng.j + db->prng.s[db->prng.i]) & 0xff;
t = db->prng.s[db->prng.i];
db->prng.s[db->prng.i] = db->prng.s[db->prng.j];
db->prng.s[db->prng.j] = t;
t = db->prng.s[db->prng.i] + db->prng.s[db->prng.j];
return db->prng.s[t & 0xff];
prng.i = (prng.i + 1) & 0xff;
prng.j = (prng.j + prng.s[prng.i]) & 0xff;
t = prng.s[prng.i];
prng.s[prng.i] = prng.s[prng.j];
prng.s[prng.j] = t;
t = prng.s[prng.i] + prng.s[prng.j];
return prng.s[t & 0xff];
}
/*
** Return an random 8-bit integer.
*/
int sqliteRandomByte(){
int r;
sqliteOsEnterMutex();
r = randomByte();
sqliteOsLeaveMutex();
return r;
}
/*
** Return a random 32-bit integer. The integer is generated by making
** 4 calls to sqliteRandomByte().
*/
int sqliteRandomInteger(sqlite *db){
int sqliteRandomInteger(){
int r;
int i;
r = sqliteRandomByte(db);
sqliteOsEnterMutex();
r = randomByte();
for(i=1; i<4; i++){
r = (r<<8) + sqliteRandomByte(db);
r = (r<<8) + randomByte();
}
sqliteOsLeaveMutex();
return r;
}

View File

@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.58 2001/10/08 13:22:33 drh Exp $
** @(#) $Id: sqliteInt.h,v 1.59 2001/10/09 04:19:47 drh Exp $
*/
#include "sqlite.h"
#include "hash.h"
@ -149,11 +149,6 @@ struct sqlite {
int (*xBusyCallback)(void *,const char*,int); /* The busy callback */
Hash tblHash; /* All tables indexed by name */
Hash idxHash; /* All (named) indices indexed by name */
struct { /* State of the RC4 random number generator */
int isInit; /* True if initialized */
int i, j; /* State variables */
int s[256]; /* State variables */
} prng;
int nextRowid; /* Next generated rowID */
};
@ -466,8 +461,8 @@ void sqliteExprResolveInSelect(Parse*, Expr*);
int sqliteExprAnalyzeAggregates(Parse*, Expr*);
void sqliteParseInfoReset(Parse*);
Vdbe *sqliteGetVdbe(Parse*);
int sqliteRandomByte(sqlite*);
int sqliteRandomInteger(sqlite*);
int sqliteRandomByte();
int sqliteRandomInteger();
void sqliteBeginTransaction(Parse*);
void sqliteCommitTransaction(Parse*);
void sqliteRollbackTransaction(Parse*);

View File

@ -15,9 +15,10 @@
** individual tokens and sends those tokens one-by-one over to the
** parser for analysis.
**
** $Id: tokenize.c,v 1.25 2001/10/08 13:22:33 drh Exp $
** $Id: tokenize.c,v 1.26 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include <stdlib.h>
@ -113,16 +114,20 @@ static int sqliteKeywordCode(const char *z, int n){
Keyword *p;
if( aKeywordTable[0].len==0 ){
/* Initialize the keyword hash table */
int i;
int n;
n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
for(i=0; i<n; i++){
aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
h %= KEY_HASH_SIZE;
aKeywordTable[i].pNext = apHashTable[h];
apHashTable[h] = &aKeywordTable[i];
sqliteOsEnterMutex();
if( aKeywordTable[0].len==0 ){
int i;
int n;
n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
for(i=0; i<n; i++){
aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
h %= KEY_HASH_SIZE;
aKeywordTable[i].pNext = apHashTable[h];
apHashTable[h] = &aKeywordTable[i];
}
}
sqliteOsLeaveMutex();
}
h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE;
for(p=apHashTable[h]; p; p=p->pNext){

View File

@ -30,7 +30,7 @@
** But other routines are also provided to help in building up
** a program instruction by instruction.
**
** $Id: vdbe.c,v 1.81 2001/10/08 13:22:33 drh Exp $
** $Id: vdbe.c,v 1.82 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@ -2368,9 +2368,9 @@ case OP_NewRecno: {
v = db->nextRowid;
do{
if( cnt>5 ){
v = sqliteRandomInteger(db);
v = sqliteRandomInteger();
}else{
v += sqliteRandomByte(db) + 1;
v += sqliteRandomByte() + 1;
}
if( v==0 ) continue;
x = bigEndian(v);