mutex_noop.c

Go to the documentation of this file.
00001 /*
00002 ** 2008 October 07
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains the C functions that implement mutexes.
00013 **
00014 ** This implementation in this file does not provide any mutual
00015 ** exclusion and is thus suitable for use only in applications
00016 ** that use SQLite in a single thread.  The routines defined
00017 ** here are place-holders.  Applications can substitute working
00018 ** mutex routines at start-time using the
00019 **
00020 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
00021 **
00022 ** interface.
00023 **
00024 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
00025 ** that does error checking on mutexes to make sure they are being
00026 ** called correctly.
00027 **
00028 ** $Id: mutex_noop.c,v 1.2 2008/10/15 19:03:03 drh Exp $
00029 */
00030 #include "sqliteInt.h"
00031 
00032 
00033 #if defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG)
00034 /*
00035 ** Stub routines for all mutex methods.
00036 **
00037 ** This routines provide no mutual exclusion or error checking.
00038 */
00039 static int noopMutexHeld(sqlite3_mutex *p){ return 1; }
00040 static int noopMutexNotheld(sqlite3_mutex *p){ return 1; }
00041 static int noopMutexInit(void){ return SQLITE_OK; }
00042 static int noopMutexEnd(void){ return SQLITE_OK; }
00043 static sqlite3_mutex *noopMutexAlloc(int id){ return (sqlite3_mutex*)8; }
00044 static void noopMutexFree(sqlite3_mutex *p){ return; }
00045 static void noopMutexEnter(sqlite3_mutex *p){ return; }
00046 static int noopMutexTry(sqlite3_mutex *p){ return SQLITE_OK; }
00047 static void noopMutexLeave(sqlite3_mutex *p){ return; }
00048 
00049 sqlite3_mutex_methods *sqlite3DefaultMutex(void){
00050   static sqlite3_mutex_methods sMutex = {
00051     noopMutexInit,
00052     noopMutexEnd,
00053     noopMutexAlloc,
00054     noopMutexFree,
00055     noopMutexEnter,
00056     noopMutexTry,
00057     noopMutexLeave,
00058 
00059     noopMutexHeld,
00060     noopMutexNotheld
00061   };
00062 
00063   return &sMutex;
00064 }
00065 #endif /* defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG) */
00066 
00067 #if defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG)
00068 /*
00069 ** In this implementation, error checking is provided for testing
00070 ** and debugging purposes.  The mutexes still do not provide any
00071 ** mutual exclusion.
00072 */
00073 
00074 /*
00075 ** The mutex object
00076 */
00077 struct sqlite3_mutex {
00078   int id;     /* The mutex type */
00079   int cnt;    /* Number of entries without a matching leave */
00080 };
00081 
00082 /*
00083 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
00084 ** intended for use inside assert() statements.
00085 */
00086 static int debugMutexHeld(sqlite3_mutex *p){
00087   return p==0 || p->cnt>0;
00088 }
00089 static int debugMutexNotheld(sqlite3_mutex *p){
00090   return p==0 || p->cnt==0;
00091 }
00092 
00093 /*
00094 ** Initialize and deinitialize the mutex subsystem.
00095 */
00096 static int debugMutexInit(void){ return SQLITE_OK; }
00097 static int debugMutexEnd(void){ return SQLITE_OK; }
00098 
00099 /*
00100 ** The sqlite3_mutex_alloc() routine allocates a new
00101 ** mutex and returns a pointer to it.  If it returns NULL
00102 ** that means that a mutex could not be allocated. 
00103 */
00104 static sqlite3_mutex *debugMutexAlloc(int id){
00105   static sqlite3_mutex aStatic[6];
00106   sqlite3_mutex *pNew = 0;
00107   switch( id ){
00108     case SQLITE_MUTEX_FAST:
00109     case SQLITE_MUTEX_RECURSIVE: {
00110       pNew = sqlite3Malloc(sizeof(*pNew));
00111       if( pNew ){
00112         pNew->id = id;
00113         pNew->cnt = 0;
00114       }
00115       break;
00116     }
00117     default: {
00118       assert( id-2 >= 0 );
00119       assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) );
00120       pNew = &aStatic[id-2];
00121       pNew->id = id;
00122       break;
00123     }
00124   }
00125   return pNew;
00126 }
00127 
00128 /*
00129 ** This routine deallocates a previously allocated mutex.
00130 */
00131 static void debugMutexFree(sqlite3_mutex *p){
00132   assert( p->cnt==0 );
00133   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
00134   sqlite3_free(p);
00135 }
00136 
00137 /*
00138 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
00139 ** to enter a mutex.  If another thread is already within the mutex,
00140 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
00141 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
00142 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
00143 ** be entered multiple times by the same thread.  In such cases the,
00144 ** mutex must be exited an equal number of times before another thread
00145 ** can enter.  If the same thread tries to enter any other kind of mutex
00146 ** more than once, the behavior is undefined.
00147 */
00148 static void debugMutexEnter(sqlite3_mutex *p){
00149   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
00150   p->cnt++;
00151 }
00152 static int debugMutexTry(sqlite3_mutex *p){
00153   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
00154   p->cnt++;
00155   return SQLITE_OK;
00156 }
00157 
00158 /*
00159 ** The sqlite3_mutex_leave() routine exits a mutex that was
00160 ** previously entered by the same thread.  The behavior
00161 ** is undefined if the mutex is not currently entered or
00162 ** is not currently allocated.  SQLite will never do either.
00163 */
00164 static void debugMutexLeave(sqlite3_mutex *p){
00165   assert( debugMutexHeld(p) );
00166   p->cnt--;
00167   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
00168 }
00169 
00170 sqlite3_mutex_methods *sqlite3DefaultMutex(void){
00171   static sqlite3_mutex_methods sMutex = {
00172     debugMutexInit,
00173     debugMutexEnd,
00174     debugMutexAlloc,
00175     debugMutexFree,
00176     debugMutexEnter,
00177     debugMutexTry,
00178     debugMutexLeave,
00179 
00180     debugMutexHeld,
00181     debugMutexNotheld
00182   };
00183 
00184   return &sMutex;
00185 }
00186 #endif /* defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG) */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1