blob: 201291a3e9d5020da64584ceede7f247359bd7bf [file] [log] [blame]
drh900b31e2007-08-28 02:27:51 +00001/*
2** 2007 August 27
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**
drh900b31e2007-08-28 02:27:51 +000013** This file contains code used to implement mutexes on Btree objects.
14** This code really belongs in btree.c. But btree.c is getting too
15** big and we want to break it down some. This packaged seemed like
16** a good breakout.
17*/
18#include "btreeInt.h"
danielk1977f7590db2009-04-10 12:55:16 +000019#ifndef SQLITE_OMIT_SHARED_CACHE
20#if SQLITE_THREADSAFE
drh900b31e2007-08-28 02:27:51 +000021
danielk19772a50ff02009-04-10 09:47:06 +000022/*
23** Obtain the BtShared mutex associated with B-Tree handle p. Also,
24** set BtShared.db to the database handle associated with p and the
25** p->locked boolean to true.
26*/
27static void lockBtreeMutex(Btree *p){
28 assert( p->locked==0 );
29 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
30 assert( sqlite3_mutex_held(p->db->mutex) );
31
32 sqlite3_mutex_enter(p->pBt->mutex);
33 p->pBt->db = p->db;
34 p->locked = 1;
35}
36
37/*
38** Release the BtShared mutex associated with B-Tree handle p and
39** clear the p->locked boolean.
40*/
41static void unlockBtreeMutex(Btree *p){
42 assert( p->locked==1 );
43 assert( sqlite3_mutex_held(p->pBt->mutex) );
44 assert( sqlite3_mutex_held(p->db->mutex) );
45 assert( p->db==p->pBt->db );
46
47 sqlite3_mutex_leave(p->pBt->mutex);
48 p->locked = 0;
49}
drh900b31e2007-08-28 02:27:51 +000050
51/*
52** Enter a mutex on the given BTree object.
53**
54** If the object is not sharable, then no mutex is ever required
55** and this routine is a no-op. The underlying mutex is non-recursive.
56** But we keep a reference count in Btree.wantToLock so the behavior
57** of this interface is recursive.
58**
59** To avoid deadlocks, multiple Btrees are locked in the same order
60** by all database connections. The p->pNext is a list of other
61** Btrees belonging to the same database connection as the p Btree
62** which need to be locked after p. If we cannot get a lock on
63** p, then first unlock all of the others on p->pNext, then wait
64** for the lock to become available on p, then relock all of the
65** subsequent Btrees that desire a lock.
66*/
67void sqlite3BtreeEnter(Btree *p){
68 Btree *pLater;
69
70 /* Some basic sanity checking on the Btree. The list of Btrees
71 ** connected by pNext and pPrev should be in sorted order by
72 ** Btree.pBt value. All elements of the list should belong to
73 ** the same connection. Only shared Btrees are on the list. */
74 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
75 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
drhe5fe6902007-12-07 18:55:28 +000076 assert( p->pNext==0 || p->pNext->db==p->db );
77 assert( p->pPrev==0 || p->pPrev->db==p->db );
drh900b31e2007-08-28 02:27:51 +000078 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
79
80 /* Check for locking consistency */
81 assert( !p->locked || p->wantToLock>0 );
82 assert( p->sharable || p->wantToLock==0 );
83
84 /* We should already hold a lock on the database connection */
drhe5fe6902007-12-07 18:55:28 +000085 assert( sqlite3_mutex_held(p->db->mutex) );
drh900b31e2007-08-28 02:27:51 +000086
danielk19772a50ff02009-04-10 09:47:06 +000087 /* Unless the database is sharable and unlocked, then BtShared.db
88 ** should already be set correctly. */
89 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
90
drh900b31e2007-08-28 02:27:51 +000091 if( !p->sharable ) return;
92 p->wantToLock++;
93 if( p->locked ) return;
94
95 /* In most cases, we should be able to acquire the lock we
96 ** want without having to go throught the ascending lock
97 ** procedure that follows. Just be sure not to block.
98 */
99 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
danielk19772a50ff02009-04-10 09:47:06 +0000100 p->pBt->db = p->db;
drh900b31e2007-08-28 02:27:51 +0000101 p->locked = 1;
102 return;
103 }
104
105 /* To avoid deadlock, first release all locks with a larger
106 ** BtShared address. Then acquire our lock. Then reacquire
107 ** the other BtShared locks that we used to hold in ascending
108 ** order.
109 */
110 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
111 assert( pLater->sharable );
112 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
113 assert( !pLater->locked || pLater->wantToLock>0 );
114 if( pLater->locked ){
danielk19772a50ff02009-04-10 09:47:06 +0000115 unlockBtreeMutex(pLater);
drh900b31e2007-08-28 02:27:51 +0000116 }
117 }
danielk19772a50ff02009-04-10 09:47:06 +0000118 lockBtreeMutex(p);
drh900b31e2007-08-28 02:27:51 +0000119 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
120 if( pLater->wantToLock ){
danielk19772a50ff02009-04-10 09:47:06 +0000121 lockBtreeMutex(pLater);
drh900b31e2007-08-28 02:27:51 +0000122 }
123 }
124}
125
126/*
127** Exit the recursive mutex on a Btree.
128*/
129void sqlite3BtreeLeave(Btree *p){
130 if( p->sharable ){
131 assert( p->wantToLock>0 );
132 p->wantToLock--;
133 if( p->wantToLock==0 ){
danielk19772a50ff02009-04-10 09:47:06 +0000134 unlockBtreeMutex(p);
drh900b31e2007-08-28 02:27:51 +0000135 }
136 }
137}
138
drh1fee73e2007-08-29 04:00:57 +0000139#ifndef NDEBUG
140/*
danielk19772a50ff02009-04-10 09:47:06 +0000141** Return true if the BtShared mutex is held on the btree, or if the
142** B-Tree is not marked as sharable.
drh1fee73e2007-08-29 04:00:57 +0000143**
144** This routine is used only from within assert() statements.
145*/
146int sqlite3BtreeHoldsMutex(Btree *p){
danielk19772a50ff02009-04-10 09:47:06 +0000147 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
148 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
149 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
150 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
151
152 return (p->sharable==0 || p->locked);
drh1fee73e2007-08-29 04:00:57 +0000153}
154#endif
155
156
drhff0587c2007-08-29 17:43:19 +0000157#ifndef SQLITE_OMIT_INCRBLOB
158/*
159** Enter and leave a mutex on a Btree given a cursor owned by that
160** Btree. These entry points are used by incremental I/O and can be
161** omitted if that module is not used.
162*/
163void sqlite3BtreeEnterCursor(BtCursor *pCur){
164 sqlite3BtreeEnter(pCur->pBtree);
165}
166void sqlite3BtreeLeaveCursor(BtCursor *pCur){
167 sqlite3BtreeLeave(pCur->pBtree);
168}
169#endif /* SQLITE_OMIT_INCRBLOB */
170
171
drh900b31e2007-08-28 02:27:51 +0000172/*
drhb1ab8ea2007-08-29 00:33:07 +0000173** Enter the mutex on every Btree associated with a database
174** connection. This is needed (for example) prior to parsing
175** a statement since we will be comparing table and column names
176** against all schemas and we do not want those schemas being
177** reset out from under us.
178**
179** There is a corresponding leave-all procedures.
180**
181** Enter the mutexes in accending order by BtShared pointer address
182** to avoid the possibility of deadlock when two threads with
183** two or more btrees in common both try to lock all their btrees
184** at the same instant.
185*/
186void sqlite3BtreeEnterAll(sqlite3 *db){
187 int i;
drh1fee73e2007-08-29 04:00:57 +0000188 Btree *p, *pLater;
drhb1ab8ea2007-08-29 00:33:07 +0000189 assert( sqlite3_mutex_held(db->mutex) );
drh1fee73e2007-08-29 04:00:57 +0000190 for(i=0; i<db->nDb; i++){
191 p = db->aDb[i].pBt;
danielk19772a50ff02009-04-10 09:47:06 +0000192 assert( !p || (p->locked==0 && p->sharable) || p->pBt->db==p->db );
drh1fee73e2007-08-29 04:00:57 +0000193 if( p && p->sharable ){
drhb1ab8ea2007-08-29 00:33:07 +0000194 p->wantToLock++;
drh1fee73e2007-08-29 04:00:57 +0000195 if( !p->locked ){
196 assert( p->wantToLock==1 );
197 while( p->pPrev ) p = p->pPrev;
drh5dea3152009-07-20 12:33:32 +0000198 /* Reason for ALWAYS: There must be at least on unlocked Btree in
199 ** the chain. Otherwise the !p->locked test above would have failed */
200 while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
drh1fee73e2007-08-29 04:00:57 +0000201 for(pLater = p->pNext; pLater; pLater=pLater->pNext){
202 if( pLater->locked ){
danielk19772a50ff02009-04-10 09:47:06 +0000203 unlockBtreeMutex(pLater);
drh1fee73e2007-08-29 04:00:57 +0000204 }
205 }
206 while( p ){
danielk19772a50ff02009-04-10 09:47:06 +0000207 lockBtreeMutex(p);
drh1fee73e2007-08-29 04:00:57 +0000208 p = p->pNext;
209 }
210 }
drhb1ab8ea2007-08-29 00:33:07 +0000211 }
212 }
213}
214void sqlite3BtreeLeaveAll(sqlite3 *db){
215 int i;
216 Btree *p;
217 assert( sqlite3_mutex_held(db->mutex) );
drh1fee73e2007-08-29 04:00:57 +0000218 for(i=0; i<db->nDb; i++){
219 p = db->aDb[i].pBt;
220 if( p && p->sharable ){
221 assert( p->wantToLock>0 );
drhb1ab8ea2007-08-29 00:33:07 +0000222 p->wantToLock--;
223 if( p->wantToLock==0 ){
danielk19772a50ff02009-04-10 09:47:06 +0000224 unlockBtreeMutex(p);
drhb1ab8ea2007-08-29 00:33:07 +0000225 }
drhb1ab8ea2007-08-29 00:33:07 +0000226 }
227 }
228}
229
drh1fee73e2007-08-29 04:00:57 +0000230#ifndef NDEBUG
231/*
232** Return true if the current thread holds the database connection
233** mutex and all required BtShared mutexes.
234**
235** This routine is used inside assert() statements only.
236*/
237int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
238 int i;
239 if( !sqlite3_mutex_held(db->mutex) ){
240 return 0;
241 }
242 for(i=0; i<db->nDb; i++){
243 Btree *p;
244 p = db->aDb[i].pBt;
245 if( p && p->sharable &&
246 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
247 return 0;
248 }
249 }
250 return 1;
251}
252#endif /* NDEBUG */
253
drhb1ab8ea2007-08-29 00:33:07 +0000254/*
drh65cef1a2008-07-14 19:39:16 +0000255** Add a new Btree pointer to a BtreeMutexArray.
256** if the pointer can possibly be shared with
drh900b31e2007-08-28 02:27:51 +0000257** another database connection.
258**
drh65cef1a2008-07-14 19:39:16 +0000259** The pointers are kept in sorted order by pBtree->pBt. That
drh900b31e2007-08-28 02:27:51 +0000260** way when we go to enter all the mutexes, we can enter them
261** in order without every having to backup and retry and without
262** worrying about deadlock.
263**
264** The number of shared btrees will always be small (usually 0 or 1)
265** so an insertion sort is an adequate algorithm here.
266*/
drhb1ab8ea2007-08-29 00:33:07 +0000267void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
drh900b31e2007-08-28 02:27:51 +0000268 int i, j;
269 BtShared *pBt;
drhfb982642007-08-30 01:19:59 +0000270 if( pBtree==0 || pBtree->sharable==0 ) return;
drh900b31e2007-08-28 02:27:51 +0000271#ifndef NDEBUG
272 {
drhb1ab8ea2007-08-29 00:33:07 +0000273 for(i=0; i<pArray->nMutex; i++){
274 assert( pArray->aBtree[i]!=pBtree );
drh900b31e2007-08-28 02:27:51 +0000275 }
276 }
277#endif
drhb1ab8ea2007-08-29 00:33:07 +0000278 assert( pArray->nMutex>=0 );
danielk197700e13612008-11-17 19:18:54 +0000279 assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
drh900b31e2007-08-28 02:27:51 +0000280 pBt = pBtree->pBt;
drhb1ab8ea2007-08-29 00:33:07 +0000281 for(i=0; i<pArray->nMutex; i++){
282 assert( pArray->aBtree[i]!=pBtree );
283 if( pArray->aBtree[i]->pBt>pBt ){
284 for(j=pArray->nMutex; j>i; j--){
285 pArray->aBtree[j] = pArray->aBtree[j-1];
drh900b31e2007-08-28 02:27:51 +0000286 }
drhb1ab8ea2007-08-29 00:33:07 +0000287 pArray->aBtree[i] = pBtree;
288 pArray->nMutex++;
drh900b31e2007-08-28 02:27:51 +0000289 return;
290 }
291 }
drhb1ab8ea2007-08-29 00:33:07 +0000292 pArray->aBtree[pArray->nMutex++] = pBtree;
drh900b31e2007-08-28 02:27:51 +0000293}
294
295/*
drh4cf7c7f2007-08-28 23:28:07 +0000296** Enter the mutex of every btree in the array. This routine is
drh900b31e2007-08-28 02:27:51 +0000297** called at the beginning of sqlite3VdbeExec(). The mutexes are
298** exited at the end of the same function.
299*/
drhb1ab8ea2007-08-29 00:33:07 +0000300void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
drh900b31e2007-08-28 02:27:51 +0000301 int i;
drhb1ab8ea2007-08-29 00:33:07 +0000302 for(i=0; i<pArray->nMutex; i++){
303 Btree *p = pArray->aBtree[i];
drh900b31e2007-08-28 02:27:51 +0000304 /* Some basic sanity checking */
drhb1ab8ea2007-08-29 00:33:07 +0000305 assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
drh900b31e2007-08-28 02:27:51 +0000306 assert( !p->locked || p->wantToLock>0 );
drh900b31e2007-08-28 02:27:51 +0000307
308 /* We should already hold a lock on the database connection */
drhe5fe6902007-12-07 18:55:28 +0000309 assert( sqlite3_mutex_held(p->db->mutex) );
drh900b31e2007-08-28 02:27:51 +0000310
drhf18a61d2009-07-17 11:44:07 +0000311 /* The Btree is sharable because only sharable Btrees are entered
312 ** into the array in the first place. */
313 assert( p->sharable );
314
drh900b31e2007-08-28 02:27:51 +0000315 p->wantToLock++;
drhf18a61d2009-07-17 11:44:07 +0000316 if( !p->locked ){
danielk19772a50ff02009-04-10 09:47:06 +0000317 lockBtreeMutex(p);
drh900b31e2007-08-28 02:27:51 +0000318 }
319 }
320}
321
322/*
drh4cf7c7f2007-08-28 23:28:07 +0000323** Leave the mutex of every btree in the group.
drh900b31e2007-08-28 02:27:51 +0000324*/
drhb1ab8ea2007-08-29 00:33:07 +0000325void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
drh900b31e2007-08-28 02:27:51 +0000326 int i;
drhb1ab8ea2007-08-29 00:33:07 +0000327 for(i=0; i<pArray->nMutex; i++){
328 Btree *p = pArray->aBtree[i];
drh900b31e2007-08-28 02:27:51 +0000329 /* Some basic sanity checking */
drhb1ab8ea2007-08-29 00:33:07 +0000330 assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
drhf18a61d2009-07-17 11:44:07 +0000331 assert( p->locked );
drh900b31e2007-08-28 02:27:51 +0000332 assert( p->wantToLock>0 );
333
334 /* We should already hold a lock on the database connection */
drhe5fe6902007-12-07 18:55:28 +0000335 assert( sqlite3_mutex_held(p->db->mutex) );
drh900b31e2007-08-28 02:27:51 +0000336
337 p->wantToLock--;
drhf18a61d2009-07-17 11:44:07 +0000338 if( p->wantToLock==0 ){
danielk19772a50ff02009-04-10 09:47:06 +0000339 unlockBtreeMutex(p);
drh900b31e2007-08-28 02:27:51 +0000340 }
341 }
342}
343
danielk1977f7590db2009-04-10 12:55:16 +0000344#else
345void sqlite3BtreeEnter(Btree *p){
346 p->pBt->db = p->db;
347}
348void sqlite3BtreeEnterAll(sqlite3 *db){
349 int i;
350 for(i=0; i<db->nDb; i++){
351 Btree *p = db->aDb[i].pBt;
352 if( p ){
353 p->pBt->db = p->db;
354 }
355 }
356}
357#endif /* if SQLITE_THREADSAFE */
358#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */