Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/test_mutex.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / test_mutex.c` 497 lines ยท 12082 bytes Raw Blame History 1 /* 2 ** 2008 June 18 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains test logic for the sqlite3_mutex interfaces. 13 */ 14 #include "tclsqlite.h" 15 #include "sqlite3.h" 16 #include "sqliteInt.h" 17 #include 18 #include 19 #include 20 21 #define MAX_MUTEXES (SQLITE_MUTEX_STATIC_VFS3+1) 22 #define STATIC_MUTEXES (MAX_MUTEXES-(SQLITE_MUTEX_RECURSIVE+1)) 23 24 /* defined in main.c */ 25 extern const char *sqlite3ErrName(int); 26 int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); 27 28 static const char *aName[MAX_MUTEXES+1] = { 29 "fast", "recursive", "static_main", "static_mem", 30 "static_open", "static_prng", "static_lru", "static_pmem", 31 "static_app1", "static_app2", "static_app3", "static_vfs1", 32 "static_vfs2", "static_vfs3", 0 33 }; 34 35 /* A countable mutex */ 36 struct sqlite3_mutex { 37 sqlite3_mutex *pReal; 38 int eType; 39 }; 40 41 /* State variables */ 42 static struct test_mutex_globals { 43 int isInstalled; /* True if installed */ 44 int disableInit; /* True to cause sqlite3_initialize() to fail */ 45 int disableTry; /* True to force sqlite3_mutex_try() to fail */ 46 int isInit; /* True if initialized */ 47 sqlite3_mutex_methods m; /* Interface to "real" mutex system */ 48 int aCounter[MAX_MUTEXES]; /* Number of grabs of each type of mutex */ 49 sqlite3_mutex aStatic[STATIC_MUTEXES]; /* The static mutexes */ 50 } g = {0}; 51 52 /* Return true if the countable mutex is currently held */ 53 static int counterMutexHeld(sqlite3_mutex *p){ 54 return g.m.xMutexHeld(p->pReal); 55 } 56 57 /* Return true if the countable mutex is not currently held */ 58 static int counterMutexNotheld(sqlite3_mutex *p){ 59 return g.m.xMutexNotheld(p->pReal); 60 } 61 62 /* Initialize the countable mutex interface 63 ** Or, if g.disableInit is non-zero, then do not initialize but instead 64 ** return the value of g.disableInit as the result code. This can be used 65 ** to simulate an initialization failure. 66 */ 67 static int counterMutexInit(void){ 68 int rc; 69 if( g.disableInit ) return g.disableInit; 70 rc = g.m.xMutexInit(); 71 g.isInit = 1; 72 return rc; 73 } 74 75 /* 76 ** Uninitialize the mutex subsystem 77 */ 78 static int counterMutexEnd(void){ 79 g.isInit = 0; 80 return g.m.xMutexEnd(); 81 } 82 83 /* 84 ** Allocate a countable mutex 85 */ 86 static sqlite3_mutex *counterMutexAlloc(int eType){ 87 sqlite3_mutex *pReal; 88 sqlite3_mutex *pRet = 0; 89 90 assert( g.isInit ); 91 assert( eType>=SQLITE_MUTEX_FAST ); 92 assert( eType<=SQLITE_MUTEX_STATIC_VFS3 ); 93 94 pReal = g.m.xMutexAlloc(eType); 95 if( !pReal ) return 0; 96 97 if( eType==SQLITE_MUTEX_FAST || eType==SQLITE_MUTEX_RECURSIVE ){ 98 pRet = (sqlite3_mutex *)malloc(sizeof(sqlite3_mutex)); 99 }else{ 100 int eStaticType = eType - (MAX_MUTEXES - STATIC_MUTEXES); 101 assert( eStaticType>=0 ); 102 assert( eStaticTypeeType = eType; 107 pRet->pReal = pReal; 108 return pRet; 109 } 110 111 /* 112 ** Free a countable mutex 113 */ 114 static void counterMutexFree(sqlite3_mutex *p){ 115 assert( g.isInit ); 116 g.m.xMutexFree(p->pReal); 117 if( p->eType==SQLITE_MUTEX_FAST || p->eType==SQLITE_MUTEX_RECURSIVE ){ 118 free(p); 119 } 120 } 121 122 /* 123 ** Enter a countable mutex. Block until entry is safe. 124 */ 125 static void counterMutexEnter(sqlite3_mutex *p){ 126 assert( g.isInit ); 127 assert( p->eType>=0 ); 128 assert( p->eTypeeType]++; 130 g.m.xMutexEnter(p->pReal); 131 } 132 133 /* 134 ** Try to enter a mutex. Return true on success. 135 */ 136 static int counterMutexTry(sqlite3_mutex *p){ 137 assert( g.isInit ); 138 assert( p->eType>=0 ); 139 assert( p->eTypeeType]++; 141 if( g.disableTry ) return SQLITE_BUSY; 142 return g.m.xMutexTry(p->pReal); 143 } 144 145 /* Leave a mutex 146 */ 147 static void counterMutexLeave(sqlite3_mutex *p){ 148 assert( g.isInit ); 149 g.m.xMutexLeave(p->pReal); 150 } 151 152 /* 153 ** sqlite3_shutdown 154 */ 155 static int SQLITE_TCLAPI test_shutdown( 156 void * clientData, 157 Tcl_Interp *interp, 158 int objc, 159 Tcl_Obj *CONST objv[] 160 ){ 161 int rc; 162 163 if( objc!=1 ){ 164 Tcl_WrongNumArgs(interp, 1, objv, ""); 165 return TCL_ERROR; 166 } 167 168 rc = sqlite3_shutdown(); 169 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); 170 return TCL_OK; 171 } 172 173 /* 174 ** sqlite3_initialize 175 */ 176 static int SQLITE_TCLAPI test_initialize( 177 void * clientData, 178 Tcl_Interp *interp, 179 int objc, 180 Tcl_Obj *CONST objv[] 181 ){ 182 int rc; 183 184 if( objc!=1 ){ 185 Tcl_WrongNumArgs(interp, 1, objv, ""); 186 return TCL_ERROR; 187 } 188 189 rc = sqlite3_initialize(); 190 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); 191 return TCL_OK; 192 } 193 194 /* 195 ** install_mutex_counters BOOLEAN 196 */ 197 static int SQLITE_TCLAPI test_install_mutex_counters( 198 void * clientData, 199 Tcl_Interp *interp, 200 int objc, 201 Tcl_Obj *CONST objv[] 202 ){ 203 int rc = SQLITE_OK; 204 int isInstall; 205 206 sqlite3_mutex_methods counter_methods = { 207 counterMutexInit, 208 counterMutexEnd, 209 counterMutexAlloc, 210 counterMutexFree, 211 counterMutexEnter, 212 counterMutexTry, 213 counterMutexLeave, 214 counterMutexHeld, 215 counterMutexNotheld 216 }; 217 218 if( objc!=2 ){ 219 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); 220 return TCL_ERROR; 221 } 222 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[1], &isInstall) ){ 223 return TCL_ERROR; 224 } 225 226 assert(isInstall==0 || isInstall==1); 227 assert(g.isInstalled==0 || g.isInstalled==1); 228 if( isInstall==g.isInstalled ){ 229 Tcl_AppendResult(interp, "mutex counters are ", NULL); 230 Tcl_AppendResult(interp, isInstall?"already installed":"not installed", NULL); 231 return TCL_ERROR; 232 } 233 234 if( isInstall ){ 235 assert( g.m.xMutexAlloc==0 ); 236 rc = sqlite3_config(SQLITE_CONFIG_GETMUTEX, &g.m); 237 if( rc==SQLITE_OK ){ 238 sqlite3_config(SQLITE_CONFIG_MUTEX, &counter_methods); 239 } 240 g.disableTry = 0; 241 }else{ 242 assert( g.m.xMutexAlloc ); 243 rc = sqlite3_config(SQLITE_CONFIG_MUTEX, &g.m); 244 memset(&g.m, 0, sizeof(sqlite3_mutex_methods)); 245 } 246 247 if( rc==SQLITE_OK ){ 248 g.isInstalled = isInstall; 249 } 250 251 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); 252 return TCL_OK; 253 } 254 255 /* 256 ** read_mutex_counters 257 */ 258 static int SQLITE_TCLAPI test_read_mutex_counters( 259 void * clientData, 260 Tcl_Interp *interp, 261 int objc, 262 Tcl_Obj *CONST objv[] 263 ){ 264 Tcl_Obj *pRet; 265 int ii; 266 267 if( objc!=1 ){ 268 Tcl_WrongNumArgs(interp, 1, objv, ""); 269 return TCL_ERROR; 270 } 271 272 pRet = Tcl_NewObj(); 273 Tcl_IncrRefCount(pRet); 274 for(ii=0; ii