mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-09 14:21:03 +03:00
Remove the SQLITE_MUTEX_APPDEF compile-time option. The SQLITE_THREADSAFE=0
option always removes all mutex code. For application-defined mutexes only, use SQLITE_THREADSAFE=1 with SQLITE_MUTEX_NOOP=1. Ticket #3421. (CVS 5779) FossilOrigin-Name: 02a12eb1cfe9307c66556105a1a99d657cc01ab5
This commit is contained in:
136
src/mutex.c
136
src/mutex.c
@@ -11,19 +11,14 @@
|
||||
*************************************************************************
|
||||
** This file contains the C functions that implement mutexes.
|
||||
**
|
||||
** The implementation in this file does not provide any mutual
|
||||
** exclusion and is thus suitable for use only in applications
|
||||
** that use SQLite in a single thread. But this implementation
|
||||
** does do a lot of error checking on mutexes to make sure they
|
||||
** are called correctly and at appropriate times. Hence, this
|
||||
** implementation is suitable for testing.
|
||||
** debugging purposes
|
||||
** This file contains code that is common across all mutex implementations.
|
||||
|
||||
**
|
||||
** $Id: mutex.c,v 1.28 2008/09/01 18:34:20 danielk1977 Exp $
|
||||
** $Id: mutex.c,v 1.29 2008/10/07 15:25:48 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
|
||||
#ifndef SQLITE_MUTEX_NOOP
|
||||
#ifndef SQLITE_MUTEX_OMIT
|
||||
/*
|
||||
** Initialize the mutex system.
|
||||
*/
|
||||
@@ -149,125 +144,4 @@ int sqlite3_mutex_notheld(sqlite3_mutex *p){
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef SQLITE_MUTEX_NOOP_DEBUG
|
||||
/*
|
||||
** In this implementation, mutexes do not provide any mutual exclusion.
|
||||
** But the error checking is provided. This implementation is useful
|
||||
** for test purposes.
|
||||
*/
|
||||
|
||||
/*
|
||||
** The mutex object
|
||||
*/
|
||||
struct sqlite3_mutex {
|
||||
int id; /* The mutex type */
|
||||
int cnt; /* Number of entries without a matching leave */
|
||||
};
|
||||
|
||||
/*
|
||||
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
|
||||
** intended for use inside assert() statements.
|
||||
*/
|
||||
static int noopMutexHeld(sqlite3_mutex *p){
|
||||
return p==0 || p->cnt>0;
|
||||
}
|
||||
static int noopMutexNotheld(sqlite3_mutex *p){
|
||||
return p==0 || p->cnt==0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Initialize and deinitialize the mutex subsystem.
|
||||
*/
|
||||
static int noopMutexInit(void){ return SQLITE_OK; }
|
||||
static int noopMutexEnd(void){ return SQLITE_OK; }
|
||||
|
||||
/*
|
||||
** The sqlite3_mutex_alloc() routine allocates a new
|
||||
** mutex and returns a pointer to it. If it returns NULL
|
||||
** that means that a mutex could not be allocated.
|
||||
*/
|
||||
static sqlite3_mutex *noopMutexAlloc(int id){
|
||||
static sqlite3_mutex aStatic[6];
|
||||
sqlite3_mutex *pNew = 0;
|
||||
switch( id ){
|
||||
case SQLITE_MUTEX_FAST:
|
||||
case SQLITE_MUTEX_RECURSIVE: {
|
||||
pNew = sqlite3Malloc(sizeof(*pNew));
|
||||
if( pNew ){
|
||||
pNew->id = id;
|
||||
pNew->cnt = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
assert( id-2 >= 0 );
|
||||
assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) );
|
||||
pNew = &aStatic[id-2];
|
||||
pNew->id = id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pNew;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine deallocates a previously allocated mutex.
|
||||
*/
|
||||
static void noopMutexFree(sqlite3_mutex *p){
|
||||
assert( p->cnt==0 );
|
||||
assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
|
||||
sqlite3_free(p);
|
||||
}
|
||||
|
||||
/*
|
||||
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
|
||||
** to enter a mutex. If another thread is already within the mutex,
|
||||
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
|
||||
** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
|
||||
** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
|
||||
** be entered multiple times by the same thread. In such cases the,
|
||||
** mutex must be exited an equal number of times before another thread
|
||||
** can enter. If the same thread tries to enter any other kind of mutex
|
||||
** more than once, the behavior is undefined.
|
||||
*/
|
||||
static void noopMutexEnter(sqlite3_mutex *p){
|
||||
assert( p->id==SQLITE_MUTEX_RECURSIVE || noopMutexNotheld(p) );
|
||||
p->cnt++;
|
||||
}
|
||||
static int noopMutexTry(sqlite3_mutex *p){
|
||||
assert( p->id==SQLITE_MUTEX_RECURSIVE || noopMutexNotheld(p) );
|
||||
p->cnt++;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** The sqlite3_mutex_leave() routine exits a mutex that was
|
||||
** previously entered by the same thread. The behavior
|
||||
** is undefined if the mutex is not currently entered or
|
||||
** is not currently allocated. SQLite will never do either.
|
||||
*/
|
||||
static void noopMutexLeave(sqlite3_mutex *p){
|
||||
assert( noopMutexHeld(p) );
|
||||
p->cnt--;
|
||||
assert( p->id==SQLITE_MUTEX_RECURSIVE || noopMutexNotheld(p) );
|
||||
}
|
||||
|
||||
sqlite3_mutex_methods *sqlite3DefaultMutex(void){
|
||||
static sqlite3_mutex_methods sMutex = {
|
||||
noopMutexInit,
|
||||
noopMutexEnd,
|
||||
noopMutexAlloc,
|
||||
noopMutexFree,
|
||||
noopMutexEnter,
|
||||
noopMutexTry,
|
||||
noopMutexLeave,
|
||||
|
||||
noopMutexHeld,
|
||||
noopMutexNotheld
|
||||
};
|
||||
|
||||
return &sMutex;
|
||||
}
|
||||
#endif /* SQLITE_MUTEX_NOOP_DEBUG */
|
||||
#endif /* SQLITE_OMIT_MUTEX */
|
||||
|
||||
Reference in New Issue
Block a user