1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
** 2008 June 18
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
** $Id: test_mutex.c,v 1.4 2008/06/22 12:37:58 drh Exp $
*/
#include "tcl.h"
#include "sqlite3.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
const char *sqlite3TestErrorName(int);
struct sqlite3_mutex {
sqlite3_mutex *pReal;
int eType;
};
static struct test_mutex_globals {
int isInstalled;
sqlite3_mutex_methods m; /* Interface to "real" mutex system */
int aCounter[8]; /* Number of grabs of each type of mutex */
sqlite3_mutex aStatic[6]; /* The six static mutexes */
} g;
static int counterMutexHeld(sqlite3_mutex *p){
return g.m.xMutexHeld(p->pReal);
}
static int counterMutexNotheld(sqlite3_mutex *p){
return g.m.xMutexNotheld(p->pReal);
}
static int counterMutexInit(void){
return g.m.xMutexInit();
}
static int counterMutexEnd(void){
return g.m.xMutexEnd();
}
static sqlite3_mutex *counterMutexAlloc(int eType){
sqlite3_mutex *pReal;
sqlite3_mutex *pRet = 0;
assert(eType<8 && eType>=0);
pReal = g.m.xMutexAlloc(eType);
if( !pReal ) return 0;
if( eType==0 || eType==1 ){
pRet = (sqlite3_mutex *)malloc(sizeof(sqlite3_mutex));
}else{
pRet = &g.aStatic[eType-2];
}
pRet->eType = eType;
pRet->pReal = pReal;
return pRet;
}
static void counterMutexFree(sqlite3_mutex *p){
g.m.xMutexFree(p->pReal);
if( p->eType==0 || p->eType==1 ){
free(p);
}
}
static void counterMutexEnter(sqlite3_mutex *p){
g.aCounter[p->eType]++;
g.m.xMutexEnter(p->pReal);
}
static int counterMutexTry(sqlite3_mutex *p){
g.aCounter[p->eType]++;
return g.m.xMutexTry(p->pReal);
}
static void counterMutexLeave(sqlite3_mutex *p){
g.m.xMutexLeave(p->pReal);
}
/*
** sqlite3_shutdown
*/
static int test_shutdown(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
rc = sqlite3_shutdown();
Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
return TCL_OK;
}
/*
** sqlite3_initialize
*/
static int test_initialize(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
rc = sqlite3_initialize();
Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
return TCL_OK;
}
/*
** install_mutex_counters BOOLEAN
*/
static int test_install_mutex_counters(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc = SQLITE_OK;
int isInstall;
sqlite3_mutex_methods counter_methods = {
counterMutexInit,
counterMutexEnd,
counterMutexAlloc,
counterMutexFree,
counterMutexEnter,
counterMutexTry,
counterMutexLeave,
counterMutexHeld,
counterMutexNotheld
};
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
return TCL_ERROR;
}
if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[1], &isInstall) ){
return TCL_ERROR;
}
assert(isInstall==0 || isInstall==1);
assert(g.isInstalled==0 || g.isInstalled==1);
if( isInstall==g.isInstalled ){
Tcl_AppendResult(interp, "mutex counters are ", 0);
Tcl_AppendResult(interp, isInstall?"already installed":"not installed", 0);
return TCL_ERROR;
}
if( isInstall ){
assert( g.m.xMutexAlloc==0 );
rc = sqlite3_config(SQLITE_CONFIG_GETMUTEX, &g.m);
if( rc==SQLITE_OK ){
sqlite3_config(SQLITE_CONFIG_MUTEX, &counter_methods);
}
}else{
assert( g.m.xMutexAlloc );
rc = sqlite3_config(SQLITE_CONFIG_MUTEX, &g.m);
memset(&g.m, 0, sizeof(sqlite3_mutex_methods));
}
if( rc==SQLITE_OK ){
g.isInstalled = isInstall;
}
Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
return TCL_OK;
}
/*
** read_mutex_counters
*/
static int test_read_mutex_counters(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
Tcl_Obj *pRet;
int ii;
char *aName[8] = {
"fast", "recursive", "static_master", "static_mem",
"static_mem2", "static_prng", "static_lru", "static_lru2"
};
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
pRet = Tcl_NewObj();
Tcl_IncrRefCount(pRet);
for(ii=0; ii<8; ii++){
Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(aName[ii], -1));
Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(g.aCounter[ii]));
}
Tcl_SetObjResult(interp, pRet);
Tcl_DecrRefCount(pRet);
return TCL_OK;
}
/*
** clear_mutex_counters
*/
static int test_clear_mutex_counters(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int ii;
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
for(ii=0; ii<8; ii++){
g.aCounter[ii] = 0;
}
return TCL_OK;
}
/*
** sqlite3_config OPTION
*/
static int test_config(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
struct ConfigOption {
const char *zName;
int iValue;
} aOpt[] = {
{"singlethread", SQLITE_CONFIG_SINGLETHREAD},
{"multithread", SQLITE_CONFIG_MULTITHREAD},
{"serialized", SQLITE_CONFIG_SERIALIZED},
{0, 0}
};
int s = sizeof(struct ConfigOption);
int i;
int rc;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
if( Tcl_GetIndexFromObjStruct(interp, objv[1], aOpt, s, "flag", 0, &i) ){
return TCL_ERROR;
}
rc = sqlite3_config(aOpt[i].iValue);
Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
return TCL_OK;
}
int Sqlitetest_mutex_Init(Tcl_Interp *interp){
static struct {
char *zName;
Tcl_ObjCmdProc *xProc;
} aCmd[] = {
{ "sqlite3_shutdown", (Tcl_ObjCmdProc*)test_shutdown },
{ "sqlite3_initialize", (Tcl_ObjCmdProc*)test_initialize },
{ "sqlite3_config", (Tcl_ObjCmdProc*)test_config },
{ "install_mutex_counters", (Tcl_ObjCmdProc*)test_install_mutex_counters },
{ "read_mutex_counters", (Tcl_ObjCmdProc*)test_read_mutex_counters },
{ "clear_mutex_counters", (Tcl_ObjCmdProc*)test_clear_mutex_counters },
};
int i;
for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
}
memset(&g, 0, sizeof(g));
return SQLITE_OK;
}
|