interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

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 <stdlib.h>
18#include <assert.h>
19#include <string.h>
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 */
25extern const char *sqlite3ErrName(int);
26int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
27
28static 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 */
36struct sqlite3_mutex {
37 sqlite3_mutex *pReal;
38 int eType;
39};
40
41/* State variables */
42static 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 */
53static 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 */
58static 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*/
67static 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*/
78static int counterMutexEnd(void){
79 g.isInit = 0;
80 return g.m.xMutexEnd();
81}
82
83/*
84** Allocate a countable mutex
85*/
86static 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( eStaticType<STATIC_MUTEXES );
103 pRet = &g.aStatic[eStaticType];
104 }
105
106 pRet->eType = eType;
107 pRet->pReal = pReal;
108 return pRet;
109}
110
111/*
112** Free a countable mutex
113*/
114static 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*/
125static void counterMutexEnter(sqlite3_mutex *p){
126 assert( g.isInit );
127 assert( p->eType>=0 );
128 assert( p->eType<MAX_MUTEXES );
129 g.aCounter[p->eType]++;
130 g.m.xMutexEnter(p->pReal);
131}
132
133/*
134** Try to enter a mutex. Return true on success.
135*/
136static int counterMutexTry(sqlite3_mutex *p){
137 assert( g.isInit );
138 assert( p->eType>=0 );
139 assert( p->eType<MAX_MUTEXES );
140 g.aCounter[p->eType]++;
141 if( g.disableTry ) return SQLITE_BUSY;
142 return g.m.xMutexTry(p->pReal);
143}
144
145/* Leave a mutex
146*/
147static void counterMutexLeave(sqlite3_mutex *p){
148 assert( g.isInit );
149 g.m.xMutexLeave(p->pReal);
150}
151
152/*
153** sqlite3_shutdown
154*/
155static 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*/
176static 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*/
197static 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*/
258static 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<MAX_MUTEXES; ii++){
275 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(aName[ii], -1));
276 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(g.aCounter[ii]));
277 }
278 Tcl_SetObjResult(interp, pRet);
279 Tcl_DecrRefCount(pRet);
280
281 return TCL_OK;
282}
283
284/*
285** clear_mutex_counters
286*/
287static int SQLITE_TCLAPI test_clear_mutex_counters(
288 void * clientData,
289 Tcl_Interp *interp,
290 int objc,
291 Tcl_Obj *CONST objv[]
292){
293 int ii;
294
295 if( objc!=1 ){
296 Tcl_WrongNumArgs(interp, 1, objv, "");
297 return TCL_ERROR;
298 }
299
300 for(ii=0; ii<MAX_MUTEXES; ii++){
301 g.aCounter[ii] = 0;
302 }
303 return TCL_OK;
304}
305
306/*
307** Create and free a mutex. Return the mutex pointer. The pointer
308** will be invalid since the mutex has already been freed. The
309** return pointer just checks to see if the mutex really was allocated.
310*/
311static int SQLITE_TCLAPI test_alloc_mutex(
312 void * clientData,
313 Tcl_Interp *interp,
314 int objc,
315 Tcl_Obj *CONST objv[]
316){
317#if SQLITE_THREADSAFE
318 sqlite3_mutex *p = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
319 char zBuf[100];
320 sqlite3_mutex_free(p);
321 sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", p);
322 Tcl_AppendResult(interp, zBuf, (char*)0);
323#endif
324 return TCL_OK;
325}
326
327/*
328** sqlite3_config OPTION
329**
330** OPTION can be either one of the keywords:
331**
332** SQLITE_CONFIG_SINGLETHREAD
333** SQLITE_CONFIG_MULTITHREAD
334** SQLITE_CONFIG_SERIALIZED
335**
336** Or OPTION can be an raw integer.
337*/
338static int SQLITE_TCLAPI test_config(
339 void * clientData,
340 Tcl_Interp *interp,
341 int objc,
342 Tcl_Obj *CONST objv[]
343){
344 struct ConfigOption {
345 const char *zName;
346 int iValue;
347 } aOpt[] = {
348 {"singlethread", SQLITE_CONFIG_SINGLETHREAD},
349 {"multithread", SQLITE_CONFIG_MULTITHREAD},
350 {"serialized", SQLITE_CONFIG_SERIALIZED},
351 {0, 0}
352 };
353 int s = sizeof(struct ConfigOption);
354 int i;
355 int rc;
356
357 if( objc!=2 ){
358 Tcl_WrongNumArgs(interp, 1, objv, "");
359 return TCL_ERROR;
360 }
361
362 if( Tcl_GetIndexFromObjStruct(interp, objv[1], aOpt, s, "flag", 0, &i) ){
363 if( Tcl_GetIntFromObj(interp, objv[1], &i) ){
364 return TCL_ERROR;
365 }
366 }else{
367 i = aOpt[i].iValue;
368 }
369
370 rc = sqlite3_config(i);
371 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
372 return TCL_OK;
373}
374
375static sqlite3_mutex *getStaticMutexPointer(
376 Tcl_Interp *pInterp,
377 Tcl_Obj *pObj
378){
379 int iMutex;
380 if( Tcl_GetIndexFromObj(pInterp, pObj, aName, "mutex name", 0, &iMutex) ){
381 return 0;
382 }
383 assert( iMutex!=SQLITE_MUTEX_FAST && iMutex!=SQLITE_MUTEX_RECURSIVE );
384 return counterMutexAlloc(iMutex);
385}
386
387static int SQLITE_TCLAPI test_enter_static_mutex(
388 void * clientData,
389 Tcl_Interp *interp,
390 int objc,
391 Tcl_Obj *CONST objv[]
392){
393 sqlite3_mutex *pMutex;
394 if( objc!=2 ){
395 Tcl_WrongNumArgs(interp, 1, objv, "NAME");
396 return TCL_ERROR;
397 }
398 pMutex = getStaticMutexPointer(interp, objv[1]);
399 if( !pMutex ){
400 return TCL_ERROR;
401 }
402 sqlite3_mutex_enter(pMutex);
403 return TCL_OK;
404}
405
406static int SQLITE_TCLAPI test_leave_static_mutex(
407 void * clientData,
408 Tcl_Interp *interp,
409 int objc,
410 Tcl_Obj *CONST objv[]
411){
412 sqlite3_mutex *pMutex;
413 if( objc!=2 ){
414 Tcl_WrongNumArgs(interp, 1, objv, "NAME");
415 return TCL_ERROR;
416 }
417 pMutex = getStaticMutexPointer(interp, objv[1]);
418 if( !pMutex ){
419 return TCL_ERROR;
420 }
421 sqlite3_mutex_leave(pMutex);
422 return TCL_OK;
423}
424
425static int SQLITE_TCLAPI test_enter_db_mutex(
426 void * clientData,
427 Tcl_Interp *interp,
428 int objc,
429 Tcl_Obj *CONST objv[]
430){
431 sqlite3 *db;
432 if( objc!=2 ){
433 Tcl_WrongNumArgs(interp, 1, objv, "DB");
434 return TCL_ERROR;
435 }
436 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
437 return TCL_ERROR;
438 }
439 if( !db ){
440 return TCL_ERROR;
441 }
442 sqlite3_mutex_enter(sqlite3_db_mutex(db));
443 return TCL_OK;
444}
445
446static int SQLITE_TCLAPI test_leave_db_mutex(
447 void * clientData,
448 Tcl_Interp *interp,
449 int objc,
450 Tcl_Obj *CONST objv[]
451){
452 sqlite3 *db;
453 if( objc!=2 ){
454 Tcl_WrongNumArgs(interp, 1, objv, "DB");
455 return TCL_ERROR;
456 }
457 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
458 return TCL_ERROR;
459 }
460 if( !db ){
461 return TCL_ERROR;
462 }
463 sqlite3_mutex_leave(sqlite3_db_mutex(db));
464 return TCL_OK;
465}
466
467int Sqlitetest_mutex_Init(Tcl_Interp *interp){
468 static struct {
469 char *zName;
470 Tcl_ObjCmdProc *xProc;
471 } aCmd[] = {
472 { "sqlite3_shutdown", (Tcl_ObjCmdProc*)test_shutdown },
473 { "sqlite3_initialize", (Tcl_ObjCmdProc*)test_initialize },
474 { "sqlite3_config", (Tcl_ObjCmdProc*)test_config },
475
476 { "enter_static_mutex", (Tcl_ObjCmdProc*)test_enter_static_mutex },
477 { "leave_static_mutex", (Tcl_ObjCmdProc*)test_leave_static_mutex },
478
479 { "enter_db_mutex", (Tcl_ObjCmdProc*)test_enter_db_mutex },
480 { "leave_db_mutex", (Tcl_ObjCmdProc*)test_leave_db_mutex },
481
482 { "alloc_dealloc_mutex", (Tcl_ObjCmdProc*)test_alloc_mutex },
483 { "install_mutex_counters", (Tcl_ObjCmdProc*)test_install_mutex_counters },
484 { "read_mutex_counters", (Tcl_ObjCmdProc*)test_read_mutex_counters },
485 { "clear_mutex_counters", (Tcl_ObjCmdProc*)test_clear_mutex_counters },
486 };
487 int i;
488 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
489 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
490 }
491
492 Tcl_LinkVar(interp, "disable_mutex_init",
493 (char*)&g.disableInit, TCL_LINK_INT);
494 Tcl_LinkVar(interp, "disable_mutex_try",
495 (char*)&g.disableTry, TCL_LINK_INT);
496 return SQLITE_OK;
497}