/ Check-in [c0fa3769]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add initial implementations of mutex and memory subsystem modules. (CVS 4226)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c0fa3769790af199a4c8715c80bb8ff900730520
User & Date: drh 2007-08-15 13:04:54
Context
2007-08-15
17:07
Add a debugging memory allocator. (CVS 4227) check-in: 8d2d1c4f user: drh tags: trunk
13:04
Add initial implementations of mutex and memory subsystem modules. (CVS 4226) check-in: c0fa3769 user: drh tags: trunk
11:28
Revisions to the interface design for 3.5. (CVS 4225) check-in: 174116f7 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to main.mk.

54
55
56
57
58
59
60

61
62
63
64
65
66
67
68
..
87
88
89
90
91
92
93


94
95
96
97
98
99
100
...
336
337
338
339
340
341
342






343
344
345
346
347
348
349
TCCX = $(TCC) $(OPTS) $(THREADSAFE) $(USLEEP) -I. -I$(TOP)/src

# Object files for the SQLite library.
#
LIBOBJ+= alter.o analyze.o attach.o auth.o btree.o build.o \
         callback.o complete.o date.o delete.o \
         expr.o func.o hash.o insert.o loadext.o \

         main.o malloc.o opcodes.o os.o os_os2.o os_unix.o os_win.o \
         pager.o parse.o pragma.o prepare.o printf.o random.o \
         select.o table.o tclsqlite.o tokenize.o trigger.o \
         update.o util.o vacuum.o \
         vdbe.o vdbeapi.o vdbeaux.o vdbeblob.o vdbefifo.o vdbemem.o \
         where.o utf.o legacy.o vtab.o

EXTOBJ = icu.o fts2.o fts2_hash.o fts2_icu.o fts2_porter.o       \
................................................................................
  $(TOP)/src/hash.c \
  $(TOP)/src/hash.h \
  $(TOP)/src/insert.c \
  $(TOP)/src/legacy.c \
  $(TOP)/src/loadext.c \
  $(TOP)/src/main.c \
  $(TOP)/src/malloc.c \


  $(TOP)/src/os.c \
  $(TOP)/src/os_os2.c \
  $(TOP)/src/os_unix.c \
  $(TOP)/src/os_win.c \
  $(TOP)/src/pager.c \
  $(TOP)/src/pager.h \
  $(TOP)/src/parse.y \
................................................................................
	$(TCCX) -c $(TOP)/src/loadext.c

main.o:	$(TOP)/src/main.c $(HDR)
	$(TCCX) -c $(TOP)/src/main.c

malloc.o:	$(TOP)/src/malloc.c $(HDR)
	$(TCCX) -c $(TOP)/src/malloc.c







pager.o:	$(TOP)/src/pager.c $(HDR) $(TOP)/src/pager.h
	$(TCCX) -c $(TOP)/src/pager.c

opcodes.o:	opcodes.c
	$(TCCX) -c opcodes.c








>
|







 







>
>







 







>
>
>
>
>
>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
..
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
TCCX = $(TCC) $(OPTS) $(THREADSAFE) $(USLEEP) -I. -I$(TOP)/src

# Object files for the SQLite library.
#
LIBOBJ+= alter.o analyze.o attach.o auth.o btree.o build.o \
         callback.o complete.o date.o delete.o \
         expr.o func.o hash.o insert.o loadext.o \
         main.o malloc.o mem1.o mutex.o \
         opcodes.o os.o os_os2.o os_unix.o os_win.o \
         pager.o parse.o pragma.o prepare.o printf.o random.o \
         select.o table.o tclsqlite.o tokenize.o trigger.o \
         update.o util.o vacuum.o \
         vdbe.o vdbeapi.o vdbeaux.o vdbeblob.o vdbefifo.o vdbemem.o \
         where.o utf.o legacy.o vtab.o

EXTOBJ = icu.o fts2.o fts2_hash.o fts2_icu.o fts2_porter.o       \
................................................................................
  $(TOP)/src/hash.c \
  $(TOP)/src/hash.h \
  $(TOP)/src/insert.c \
  $(TOP)/src/legacy.c \
  $(TOP)/src/loadext.c \
  $(TOP)/src/main.c \
  $(TOP)/src/malloc.c \
  $(TOP)/src/mem.c \
  $(TOP)/src/mutex.c \
  $(TOP)/src/os.c \
  $(TOP)/src/os_os2.c \
  $(TOP)/src/os_unix.c \
  $(TOP)/src/os_win.c \
  $(TOP)/src/pager.c \
  $(TOP)/src/pager.h \
  $(TOP)/src/parse.y \
................................................................................
	$(TCCX) -c $(TOP)/src/loadext.c

main.o:	$(TOP)/src/main.c $(HDR)
	$(TCCX) -c $(TOP)/src/main.c

malloc.o:	$(TOP)/src/malloc.c $(HDR)
	$(TCCX) -c $(TOP)/src/malloc.c

mem1.o:	$(TOP)/src/mem1.c $(HDR)
	$(TCCX) -c $(TOP)/src/mem1.c

mutex.o:	$(TOP)/src/mutex.c $(HDR)
	$(TCCX) -c $(TOP)/src/mutex.c

pager.o:	$(TOP)/src/pager.c $(HDR) $(TOP)/src/pager.h
	$(TCCX) -c $(TOP)/src/pager.c

opcodes.o:	opcodes.c
	$(TCCX) -c opcodes.c

Changes to src/main.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.378 2007/08/13 15:28:34 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** The version of the library
................................................................................
*/
void sqlite3_interrupt(sqlite3 *db){
  if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){
    db->u1.isInterrupted = 1;
  }
}

/*
** Memory allocation routines that use SQLites internal memory
** memory allocator.  Depending on how SQLite is compiled, the
** internal memory allocator might be just an alias for the
** system default malloc/realloc/free.  Or the built-in allocator
** might do extra stuff like put sentinals around buffers to 
** check for overruns or look for memory leaks.
**
** Use sqlite3_free() to free memory returned by sqlite3_mprintf().
*/
void sqlite3_free(void *p){ if( p ) sqlite3OsFree(p); }
void *sqlite3_malloc(int nByte){ return nByte>0 ? sqlite3OsMalloc(nByte) : 0; }
void *sqlite3_realloc(void *pOld, int nByte){ 
  if( pOld ){
    if( nByte>0 ){
      return sqlite3OsRealloc(pOld, nByte);
    }else{
      sqlite3OsFree(pOld);
      return 0;
    }
  }else{
    return sqlite3_malloc(nByte);
  }
}

/*
** This function is exactly the same as sqlite3_create_function(), except
** that it is designed to be called by internal code. The difference is
** that if a malloc() fails in sqlite3_create_function(), an error code
** is returned and the mallocFailed flag cleared. 
*/







|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
417
418
419
420
421
422
423
























424
425
426
427
428
429
430
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.379 2007/08/15 13:04:54 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** The version of the library
................................................................................
*/
void sqlite3_interrupt(sqlite3 *db){
  if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){
    db->u1.isInterrupted = 1;
  }
}


























/*
** This function is exactly the same as sqlite3_create_function(), except
** that it is designed to be called by internal code. The difference is
** that if a malloc() fails in sqlite3_create_function(), an error code
** is returned and the mallocFailed flag cleared. 
*/

Added src/mem1.c.

























































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
** 2007 August 14
**
** 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.
**
*************************************************************************
** This file contains the C functions that implement a memory
** allocation subsystem for use by SQLite.  
**
** $Id: mem1.c,v 1.1 2007/08/15 13:04:54 drh Exp $
*/

/*
** We will eventually construct multiple memory allocation subsystems
** suitable for use in various contexts:
**
**    *  Normal multi-threaded builds
**    *  Normal single-threaded builds
**    *  Debugging builds
**
** This initial version is suitable for use in normal multi-threaded
** builds.  We envision that alternative versions will be stored in
** separate source files.  #ifdefs will be used to select the code from
** one of the various memN.c source files for use in any given build.
*/
#include "sqliteInt.h"


/*
** Mutex to control access to the memory allocation subsystem.
*/
static sqlite3_mutex *memMutex = 0;

/*
** Current allocation and high-water mark.
*/
static sqlite3_uint64 nowUsed = 0;
static sqlite3_uint64 mxUsed = 0;

/*
** The alarm callback and its arguments.  The memMutex lock will
** be held while the callback is running.  Recursive calls into
** the memory subsystem are allowed, but no new callbacks will be
** issued.  The alarmBusy variable is set to prevent recursive
** callbacks.
*/
static void (*alarmCallback)(void*, sqlite3_uint64, unsigned) = 0;
static void *alarmArg = 0;
static sqlite3_uint64 alarmThreshold = (((sqlite3_uint64)1)<<63);
static int alarmBusy = 0;


/*
** Return the amount of memory currently checked out.
*/
sqlite3_uint64 sqlite3_memory_used(void){
  sqlite3_uint64 n;
  if( memMutex==0 ){
    memMutex = sqlite3_mutex_alloc(1);
  }
  sqlite3_mutex_enter(memMutex, 1);
  n = nowUsed;
  sqlite3_mutex_leave(memMutex);  
  return n;
}

/*
** Return the maximum amount of memory that has ever been
** checked out since either the beginning of this process
** or since the most recent reset.
*/
sqlite3_uint64 sqlite3_memory_highwater(int resetFlag){
  sqlite3_uint64 n;
  if( memMutex==0 ){
    memMutex = sqlite3_mutex_alloc(1);
  }
  sqlite3_mutex_enter(memMutex, 1);
  n = mxUsed;
  if( resetFlag ){
    mxUsed = nowUsed;
  }
  sqlite3_mutex_leave(memMutex);  
  return n;
}

/*
** Change the alarm callback
*/
int sqlite3_memory_alarm(
  void(*xCallback)(void *pArg, sqlite3_uint64 used, unsigned int N),
  void *pArg,
  sqlite3_uint64 iThreshold
){
  if( memMutex==0 ){
    memMutex = sqlite3_mutex_alloc(1);
  }
  sqlite3_mutex_enter(memMutex, 1);
  alarmCallback = xCallback;
  alarmArg = pArg;
  alarmThreshold = iThreshold;
  sqlite3_mutex_leave(memMutex);
  return SQLITE_OK;
}

/*
** Trigger the alarm 
*/
static void sqlite3MemsysAlarm(unsigned nByte){
  if( alarmCallback==0 || alarmBusy  ) return;
  alarmBusy = 1;
  alarmCallback(alarmArg, nowUsed, nByte);
  alarmBusy = 0;
}

/*
** Allocate nBytes of memory
*/
void *sqlite3_malloc(unsigned int nBytes){
  sqlite3_uint64 *p;
  if( memMutex==0 ){
    memMutex = sqlite3_mutex_alloc(1);
  }
  sqlite3_mutex_enter(memMutex, 1);
  if( nowUsed+nBytes>=alarmThreshold ){
    sqlite3MemsysAlarm(nBytes);
  }
  p = malloc(nBytes+8);
  if( p==0 ){
    sqlite3MemsysAlarm(nBytes);
    p = malloc(nBytes+8);
  }
  if( p ){
    p[0] = nBytes;
    p++;
    nowUsed += nBytes;
    if( nowUsed>mxUsed ){
      mxUsed = nowUsed;
    }
  }
  sqlite3_mutex_leave(memMutex);
  return (void*)p; 
}

/*
** Free memory.
*/
void sqlite3_free(void *pPrior){
  sqlite3_uint64 *p;
  unsigned nByte;
  if( pPrior==0 ){
    return;
  }
  assert( memMutex!=0 );
  p = pPrior;
  p--;
  nByte = (unsigned int)*p;
  sqlite3_mutex_enter(memMutex, 1);
  nowUsed -= nByte;
  free(p);
  sqlite3_mutex_leave(memMutex);  
}

/*
** Change the size of an existing memory allocation
*/
void *sqlite3_realloc(void *pPrior, unsigned int nBytes){
  unsigned nOld;
  sqlite3_uint64 *p;
  if( pPrior==0 ){
    return sqlite3_malloc(nBytes);
  }
  if( nBytes==0 ){
    sqlite3_free(pPrior);
    return;
  }
  p = pPrior;
  p--;
  nOld = (unsigned int)p[0];
  assert( memMutex!=0 );
  sqlite3_mutex_enter(memMutex, 1);
  if( nowUsed+nBytes-nOld>=alarmThreshold ){
    sqlite3MemsysAlarm(nBytes-nOld);
  }
  p = realloc(p, nBytes+8);
  if( p==0 ){
    sqlite3MemsysAlarm(nBytes);
    p = realloc(p, nBytes+8);
  }
  if( p ){
    p[0] = nBytes;
    p++;
    nowUsed += nBytes-nOld;
    if( nowUsed>mxUsed ){
      mxUsed = nowUsed;
    }
  }
  sqlite3_mutex_leave(memMutex);
  return (void*)p;
}

Added src/mutex.c.





























































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
** 2007 August 14
**
** 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.
**
*************************************************************************
** This file contains the C functions that implement mutexes for
** use by the SQLite core.
**
** $Id: mutex.c,v 1.1 2007/08/15 13:04:54 drh Exp $
*/

/*
** If SQLITE_MUTEX_APPDEF is defined, then this whole module is
** omitted and equivalent functionality just be provided by the
** application that links against the SQLite library.
*/
#ifndef SQLITE_MUTEX_APPDEF

/*
** The start of real code
*/
#include "sqliteInt.h"

/************************ No-op Mutex Implementation **********************
**
** This first implementation of mutexes is really a no-op.  In other words,
** no real locking occurs.  This implementation is appropriate for use
** in single threaded applications which do not want the extra overhead
** of thread locking primitives.
*/

/*
** 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.  SQLite
** will unwind its stack and return an error.  The argument
** to sqlite3_mutex_alloc() is usually zero, which causes
** any space required for the mutex to be obtained from
** sqlite3_malloc().  However if the argument is a positive
** integer less than SQLITE_NUM_STATIC_MUTEX, then a pointer
** to a static mutex is returned.  There are a finite number
** of static mutexes.  Static mutexes should not be passed
** to sqlite3_mutex_free().  The allocation of a static
** mutex cannot fail.
*/
sqlite3_mutex *sqlite3_mutex_alloc(int idNotUsed){
  return (sqlite3_mutex*)sqlite3_mutex_alloc;
}

/*
** This routine deallocates a previously
** allocated mutex.  SQLite is careful to deallocate every
** mutex that it allocates.
*/
void sqlite3_mutex_free(sqlite3_mutex *pNotUsed){}

/*
** The sqlite3_mutex_enter() routine attempts to enter a
** mutex.  If another thread is already within the mutex,
** sqlite3_mutex_enter() will return SQLITE_BUSY if blockFlag
** is zero, or it will block and wait for the other thread to
** exit if blockFlag is non-zero.  Mutexes are recursive.  The
** same thread can enter a single mutex multiple times.  Each
** entrance must be matched with a corresponding exit before
** another thread is able to enter the mutex.
*/
int sqlite3_mutex_enter(sqlite3_mutex *pNotUsed, int blockFlag){
  return SQLITE_OK;
}

/*
** The sqlite3_mutex_exit() 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.
*/
void sqlite3_mutex_leave(sqlite3_mutex *pNotUsed){
  return;
}

/*
** The sqlite3_mutex_serialize() routine is used to serialize 
** execution of a subroutine.  The subroutine given in the argument
** is invoked.  But only one thread at a time is allowed to be
** running a subroutine using sqlite3_mutex_serialize().
*/
int sqlite3_mutex_serialize(void (*xCallback)(void*), void *pArg){
  xCallback(pArg);
}

#if 0
/**************** Non-recursive Pthread Mutex Implementation *****************
**
** This implementation of mutexes is built using a version of pthreads that
** does not have native support for recursive mutexes.
*/

/*
** Each recursive mutex is an instance of the following structure.
*/
struct RMutex {
  int nRef;                   /* Number of entrances */
  pthread_mutex_t auxMutex;   /* Mutex controlling access to nRef and owner */
  pthread_mutex_t mainMutex;  /* Mutex controlling the lock */
  pthread_t owner;            /* Thread that is within this mutex */
};

/*
** Static mutexes
*/
static struct RMutex rmutexes[] = {
  { 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, },
  { 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, },
  { 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, },
};

/*
** A mutex used for serialization.
*/
static RMutex serialMutex =
   {0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, };

/*
** 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.  SQLite
** will unwind its stack and return an error.  The argument
** to sqlite3_mutex_alloc() is usually zero, which causes
** any space required for the mutex to be obtained from
** sqlite3_malloc().  However if the argument is a positive
** integer less than SQLITE_NUM_STATIC_MUTEX, then a pointer
** to a static mutex is returned.  There are a finite number
** of static mutexes.  Static mutexes should not be passed
** to sqlite3_mutex_free().  The allocation of a static
** mutex cannot fail.
*/
sqlite3_mutex *sqlite3_mutex_alloc(int id){
  struct RMutex *p;
  if( id>0 ){
    if( id>sizeof(rmutexes)/sizeof(rmutexes[0]) ){
      p = 0;
    }else{
      p = &rmutexes[id-1];
    }
  }else{
    p = sqlite3_malloc( sizeof(*p) );
    if( p ){
      p->nRef = 0;
      pthread_mutex_init(&p->mutex, 0);
    }
  }
  return (sqlite3_mutex*)p;
}

/*
** This routine deallocates a previously
** allocated mutex.  SQLite is careful to deallocate every
** mutex that it allocates.
*/
void sqlite3_mutex_free(sqlite3_mutex *pMutex){
  struct RMutex *p = (struct RMutex*)pMutex;
  assert( p->nRef==0 );
  pthread_mutex_destroy(&p->mutex);
  sqlite3_free(p);
}

/*
** The sqlite3_mutex_enter() routine attempts to enter a
** mutex.  If another thread is already within the mutex,
** sqlite3_mutex_enter() will return SQLITE_BUSY if blockFlag
** is zero, or it will block and wait for the other thread to
** exit if blockFlag is non-zero.  Mutexes are recursive.  The
** same thread can enter a single mutex multiple times.  Each
** entrance must be matched with a corresponding exit before
** another thread is able to enter the mutex.
*/
int sqlite3_mutex_enter(sqlite3_mutex *pMutex, int blockFlag){
  struct RMutex *p = (struct RMutex*)pMutex;
  while(1){
    pthread_mutex_lock(&p->auxMutex);
    if( p->nRef==0 ){
      p->nRef++;
      p->owner = pthread_self();
      pthread_mutex_lock(&p->mainMutex);
      pthread_mutex_unlock(&p->auxMutex);
      return SQLITE_OK;
    }else if( pthread_equal(p->owner, pthread_self()) ){
      p->nRef++;
      pthread_mutex_unlock(&p->auxMutex);
      return SQLITE_OK;
    }else if( !blockFlag ){
      pthread_mutex_unlock(&p->auxMutex);
      return SQLITE_BUSY;
    }else{
      pthread_mutex_unlock(&p->auxMutex);
      pthread_mutex_lock(&p->mainMutex);
      pthread_mutex_unlock(&p->mainMutex);
    }
  }
  /* NOTREACHED */
}

/*
** The sqlite3_mutex_exit() 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.
*/
void sqlite3_mutex_leave(sqlite3_mutex *pMutex){
  struct RMutex *p = (struct RMutex*)pMutex;
  pthread_mutex_lock(&p->auxMutex);
  p->nRef--;
  if( p->nRef<=0 ){
    pthread_mutex_unlock(&p->mainMutex);
  }
  pthread_mutex_unlock(&p->auxMutex);
}

/*
** The sqlite3_mutex_serialize() routine is used to serialize 
** execution of a subroutine.  The subroutine given in the argument
** is invoked.  But only one thread at a time is allowed to be
** running a subroutine using sqlite3_mutex_serialize().
*/
int sqlite3_mutex_serialize(void (*xCallback)(void*), void *pArg){
  sqlite3_mutex_enter(&serialMutex, 1);
  xCallback(pArg);
  sqlite3_mutex_leave(&serialMutex);
}
#endif /* non-recursive pthreads */

#endif /* !defined(SQLITE_MUTEX_APPDEF) */

Changes to src/sqlite.h.in.

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
....
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
** on how SQLite interfaces are suppose to operate.
**
** The name of this file under configuration management is "sqlite.h.in".
** The makefile makes some minor changes to this file (such as inserting
** the version number) and changes its name to "sqlite3.h" as
** part of the build process.
**
** @(#) $Id: sqlite.h.in,v 1.222 2007/08/15 11:28:56 drh Exp $
*/
#ifndef _SQLITE3_H_
#define _SQLITE3_H_
#include <stdarg.h>     /* Needed for the definition of va_list */

/*
** Make sure we can call this stuff from C++.
................................................................................
** variable even if the zText variable is a NULL pointer.
*/
char *sqlite3_mprintf(const char*,...);
char *sqlite3_vmprintf(const char*, va_list);
char *sqlite3_snprintf(int,char*,const char*, ...);

/*
** CAPI3REF: Memory Allocation Functions
**
** The SQLite sources include a memory allocation subsystem
** that implements the interfaces shown here.
**
** The SQLite core uses these three routines for all of its own
** internal memory allocation needs.  The default implementation
** of the memory allocation subsystem uses the malloc(), realloc()
** and free() provided by the standard C library.  However, if 
** SQLite is compiled with the following C preprocessor macro
**
** <blockquote>SQLITE_OMIT_MEMORY_ALLOCATION</blockquote>
**
** then no implementation is provided for these routines by
** SQLite.  The application that links against SQLite is
** expected to provide its own implementation.
*/
void *sqlite3_malloc(int);
void *sqlite3_realloc(void*, int);
void sqlite3_free(void*);

/*
** CAPI3REF: Memory Allocator Statistics
**
** In addition to the basic three allocation routines 
** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()],







|







 







|
<
<
<







|





|
|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
....
1016
1017
1018
1019
1020
1021
1022
1023



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
** on how SQLite interfaces are suppose to operate.
**
** The name of this file under configuration management is "sqlite.h.in".
** The makefile makes some minor changes to this file (such as inserting
** the version number) and changes its name to "sqlite3.h" as
** part of the build process.
**
** @(#) $Id: sqlite.h.in,v 1.223 2007/08/15 13:04:54 drh Exp $
*/
#ifndef _SQLITE3_H_
#define _SQLITE3_H_
#include <stdarg.h>     /* Needed for the definition of va_list */

/*
** Make sure we can call this stuff from C++.
................................................................................
** variable even if the zText variable is a NULL pointer.
*/
char *sqlite3_mprintf(const char*,...);
char *sqlite3_vmprintf(const char*, va_list);
char *sqlite3_snprintf(int,char*,const char*, ...);

/*
** CAPI3REF: Memory Allocation Subsystem



**
** The SQLite core uses these three routines for all of its own
** internal memory allocation needs.  The default implementation
** of the memory allocation subsystem uses the malloc(), realloc()
** and free() provided by the standard C library.  However, if 
** SQLite is compiled with the following C preprocessor macro
**
** <blockquote> SQLITE_OMIT_MEMORY_ALLOCATION </blockquote>
**
** then no implementation is provided for these routines by
** SQLite.  The application that links against SQLite is
** expected to provide its own implementation.
*/
void *sqlite3_malloc(unsigned int);
void *sqlite3_realloc(void*, unsigned int);
void sqlite3_free(void*);

/*
** CAPI3REF: Memory Allocator Statistics
**
** In addition to the basic three allocation routines 
** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()],

Changes to src/sqliteInt.h.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
437
438
439
440
441
442
443

444
445
446
447
448
449
450
**    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.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.585 2007/08/08 12:11:21 drh Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_
#include "sqliteLimit.h"


#if defined(SQLITE_TCL) || defined(TCLSH)
................................................................................
**
** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
** store the most recent error code and, if applicable, string. The
** internal function sqlite3Error() is used to set these variables
** consistently.
*/
struct sqlite3 {

  int nDb;                      /* Number of backends currently in use */
  Db *aDb;                      /* All backends */
  int flags;                    /* Miscellanous flags. See below */
  int errCode;                  /* Most recent error code (SQLITE_*) */
  int errMask;                  /* & result codes with this before returning */
  u8 autoCommit;                /* The auto-commit flag. */
  u8 temp_store;                /* 1: file 2: memory 0: default */







|







 







>







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
**    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.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.586 2007/08/15 13:04:54 drh Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_
#include "sqliteLimit.h"


#if defined(SQLITE_TCL) || defined(TCLSH)
................................................................................
**
** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
** store the most recent error code and, if applicable, string. The
** internal function sqlite3Error() is used to set these variables
** consistently.
*/
struct sqlite3 {
  sqlite3_vfs *pVfs;            /* OS Interface */
  int nDb;                      /* Number of backends currently in use */
  Db *aDb;                      /* All backends */
  int flags;                    /* Miscellanous flags. See below */
  int errCode;                  /* Most recent error code (SQLITE_*) */
  int errMask;                  /* & result codes with this before returning */
  u8 autoCommit;                /* The auto-commit flag. */
  u8 temp_store;                /* 1: file 2: memory 0: default */