1900b31efSdrh /*
2900b31efSdrh ** 2007 August 27
3900b31efSdrh **
4900b31efSdrh ** The author disclaims copyright to this source code. In place of
5900b31efSdrh ** a legal notice, here is a blessing:
6900b31efSdrh **
7900b31efSdrh ** May you do good and not evil.
8900b31efSdrh ** May you find forgiveness for yourself and forgive others.
9900b31efSdrh ** May you share freely, never taking more than you give.
10900b31efSdrh **
11900b31efSdrh *************************************************************************
12900b31efSdrh **
13900b31efSdrh ** This file contains code used to implement mutexes on Btree objects.
14900b31efSdrh ** This code really belongs in btree.c. But btree.c is getting too
15900b31efSdrh ** big and we want to break it down some. This packaged seemed like
16900b31efSdrh ** a good breakout.
17900b31efSdrh */
18900b31efSdrh #include "btreeInt.h"
19f7590db0Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE
20f7590db0Sdanielk1977 #if SQLITE_THREADSAFE
21900b31efSdrh
222a50ff03Sdanielk1977 /*
232a50ff03Sdanielk1977 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
242a50ff03Sdanielk1977 ** set BtShared.db to the database handle associated with p and the
252a50ff03Sdanielk1977 ** p->locked boolean to true.
262a50ff03Sdanielk1977 */
lockBtreeMutex(Btree * p)272a50ff03Sdanielk1977 static void lockBtreeMutex(Btree *p){
282a50ff03Sdanielk1977 assert( p->locked==0 );
292a50ff03Sdanielk1977 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
302a50ff03Sdanielk1977 assert( sqlite3_mutex_held(p->db->mutex) );
312a50ff03Sdanielk1977
322a50ff03Sdanielk1977 sqlite3_mutex_enter(p->pBt->mutex);
332a50ff03Sdanielk1977 p->pBt->db = p->db;
342a50ff03Sdanielk1977 p->locked = 1;
352a50ff03Sdanielk1977 }
362a50ff03Sdanielk1977
372a50ff03Sdanielk1977 /*
382a50ff03Sdanielk1977 ** Release the BtShared mutex associated with B-Tree handle p and
392a50ff03Sdanielk1977 ** clear the p->locked boolean.
402a50ff03Sdanielk1977 */
unlockBtreeMutex(Btree * p)4175e2a2d3Sdrh static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
42bdaec52cSdrh BtShared *pBt = p->pBt;
432a50ff03Sdanielk1977 assert( p->locked==1 );
44bdaec52cSdrh assert( sqlite3_mutex_held(pBt->mutex) );
452a50ff03Sdanielk1977 assert( sqlite3_mutex_held(p->db->mutex) );
46bdaec52cSdrh assert( p->db==pBt->db );
472a50ff03Sdanielk1977
48bdaec52cSdrh sqlite3_mutex_leave(pBt->mutex);
492a50ff03Sdanielk1977 p->locked = 0;
502a50ff03Sdanielk1977 }
51900b31efSdrh
5275e2a2d3Sdrh /* Forward reference */
5375e2a2d3Sdrh static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
5475e2a2d3Sdrh
55900b31efSdrh /*
56900b31efSdrh ** Enter a mutex on the given BTree object.
57900b31efSdrh **
58900b31efSdrh ** If the object is not sharable, then no mutex is ever required
59900b31efSdrh ** and this routine is a no-op. The underlying mutex is non-recursive.
60900b31efSdrh ** But we keep a reference count in Btree.wantToLock so the behavior
61900b31efSdrh ** of this interface is recursive.
62900b31efSdrh **
63900b31efSdrh ** To avoid deadlocks, multiple Btrees are locked in the same order
64900b31efSdrh ** by all database connections. The p->pNext is a list of other
65900b31efSdrh ** Btrees belonging to the same database connection as the p Btree
66900b31efSdrh ** which need to be locked after p. If we cannot get a lock on
67900b31efSdrh ** p, then first unlock all of the others on p->pNext, then wait
68900b31efSdrh ** for the lock to become available on p, then relock all of the
69900b31efSdrh ** subsequent Btrees that desire a lock.
70900b31efSdrh */
sqlite3BtreeEnter(Btree * p)71900b31efSdrh void sqlite3BtreeEnter(Btree *p){
72900b31efSdrh /* Some basic sanity checking on the Btree. The list of Btrees
73900b31efSdrh ** connected by pNext and pPrev should be in sorted order by
74900b31efSdrh ** Btree.pBt value. All elements of the list should belong to
75900b31efSdrh ** the same connection. Only shared Btrees are on the list. */
76900b31efSdrh assert( p->pNext==0 || p->pNext->pBt>p->pBt );
77900b31efSdrh assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
78e5fe690dSdrh assert( p->pNext==0 || p->pNext->db==p->db );
79e5fe690dSdrh assert( p->pPrev==0 || p->pPrev->db==p->db );
80900b31efSdrh assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
81900b31efSdrh
82900b31efSdrh /* Check for locking consistency */
83900b31efSdrh assert( !p->locked || p->wantToLock>0 );
84900b31efSdrh assert( p->sharable || p->wantToLock==0 );
85900b31efSdrh
86900b31efSdrh /* We should already hold a lock on the database connection */
87e5fe690dSdrh assert( sqlite3_mutex_held(p->db->mutex) );
88900b31efSdrh
892a50ff03Sdanielk1977 /* Unless the database is sharable and unlocked, then BtShared.db
902a50ff03Sdanielk1977 ** should already be set correctly. */
912a50ff03Sdanielk1977 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
922a50ff03Sdanielk1977
93900b31efSdrh if( !p->sharable ) return;
94900b31efSdrh p->wantToLock++;
95900b31efSdrh if( p->locked ) return;
9675e2a2d3Sdrh btreeLockCarefully(p);
9775e2a2d3Sdrh }
9875e2a2d3Sdrh
9975e2a2d3Sdrh /* This is a helper function for sqlite3BtreeLock(). By moving
10075e2a2d3Sdrh ** complex, but seldom used logic, out of sqlite3BtreeLock() and
10175e2a2d3Sdrh ** into this routine, we avoid unnecessary stack pointer changes
10275e2a2d3Sdrh ** and thus help the sqlite3BtreeLock() routine to run much faster
10375e2a2d3Sdrh ** in the common case.
10475e2a2d3Sdrh */
btreeLockCarefully(Btree * p)10575e2a2d3Sdrh static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
10675e2a2d3Sdrh Btree *pLater;
107900b31efSdrh
108900b31efSdrh /* In most cases, we should be able to acquire the lock we
10960ec914cSpeter.d.reid ** want without having to go through the ascending lock
110900b31efSdrh ** procedure that follows. Just be sure not to block.
111900b31efSdrh */
112900b31efSdrh if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
1132a50ff03Sdanielk1977 p->pBt->db = p->db;
114900b31efSdrh p->locked = 1;
115900b31efSdrh return;
116900b31efSdrh }
117900b31efSdrh
118900b31efSdrh /* To avoid deadlock, first release all locks with a larger
119900b31efSdrh ** BtShared address. Then acquire our lock. Then reacquire
120900b31efSdrh ** the other BtShared locks that we used to hold in ascending
121900b31efSdrh ** order.
122900b31efSdrh */
123900b31efSdrh for(pLater=p->pNext; pLater; pLater=pLater->pNext){
124900b31efSdrh assert( pLater->sharable );
125900b31efSdrh assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
126900b31efSdrh assert( !pLater->locked || pLater->wantToLock>0 );
127900b31efSdrh if( pLater->locked ){
1282a50ff03Sdanielk1977 unlockBtreeMutex(pLater);
129900b31efSdrh }
130900b31efSdrh }
1312a50ff03Sdanielk1977 lockBtreeMutex(p);
132900b31efSdrh for(pLater=p->pNext; pLater; pLater=pLater->pNext){
133900b31efSdrh if( pLater->wantToLock ){
1342a50ff03Sdanielk1977 lockBtreeMutex(pLater);
135900b31efSdrh }
136900b31efSdrh }
137900b31efSdrh }
138900b31efSdrh
13975e2a2d3Sdrh
140900b31efSdrh /*
141900b31efSdrh ** Exit the recursive mutex on a Btree.
142900b31efSdrh */
sqlite3BtreeLeave(Btree * p)143900b31efSdrh void sqlite3BtreeLeave(Btree *p){
144b77009fdSdan assert( sqlite3_mutex_held(p->db->mutex) );
145900b31efSdrh if( p->sharable ){
146900b31efSdrh assert( p->wantToLock>0 );
147900b31efSdrh p->wantToLock--;
148900b31efSdrh if( p->wantToLock==0 ){
1492a50ff03Sdanielk1977 unlockBtreeMutex(p);
150900b31efSdrh }
151900b31efSdrh }
152900b31efSdrh }
153900b31efSdrh
1541fee73e7Sdrh #ifndef NDEBUG
1551fee73e7Sdrh /*
1562a50ff03Sdanielk1977 ** Return true if the BtShared mutex is held on the btree, or if the
1572a50ff03Sdanielk1977 ** B-Tree is not marked as sharable.
1581fee73e7Sdrh **
1591fee73e7Sdrh ** This routine is used only from within assert() statements.
1601fee73e7Sdrh */
sqlite3BtreeHoldsMutex(Btree * p)1611fee73e7Sdrh int sqlite3BtreeHoldsMutex(Btree *p){
1622a50ff03Sdanielk1977 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
1632a50ff03Sdanielk1977 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
1642a50ff03Sdanielk1977 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
1652a50ff03Sdanielk1977 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
1662a50ff03Sdanielk1977
1672a50ff03Sdanielk1977 return (p->sharable==0 || p->locked);
1681fee73e7Sdrh }
1691fee73e7Sdrh #endif
1701fee73e7Sdrh
1711fee73e7Sdrh
172900b31efSdrh /*
173b1ab8ea7Sdrh ** Enter the mutex on every Btree associated with a database
174b1ab8ea7Sdrh ** connection. This is needed (for example) prior to parsing
175b1ab8ea7Sdrh ** a statement since we will be comparing table and column names
176b1ab8ea7Sdrh ** against all schemas and we do not want those schemas being
177b1ab8ea7Sdrh ** reset out from under us.
178b1ab8ea7Sdrh **
179b1ab8ea7Sdrh ** There is a corresponding leave-all procedures.
180b1ab8ea7Sdrh **
181b1ab8ea7Sdrh ** Enter the mutexes in accending order by BtShared pointer address
182b1ab8ea7Sdrh ** to avoid the possibility of deadlock when two threads with
183b1ab8ea7Sdrh ** two or more btrees in common both try to lock all their btrees
184b1ab8ea7Sdrh ** at the same instant.
185b1ab8ea7Sdrh */
btreeEnterAll(sqlite3 * db)18638eef321Sdrh static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
187b1ab8ea7Sdrh int i;
18838eef321Sdrh int skipOk = 1;
1891a86f50dSdrh Btree *p;
190b1ab8ea7Sdrh assert( sqlite3_mutex_held(db->mutex) );
1911fee73e7Sdrh for(i=0; i<db->nDb; i++){
1921fee73e7Sdrh p = db->aDb[i].pBt;
19338eef321Sdrh if( p && p->sharable ){
19438eef321Sdrh sqlite3BtreeEnter(p);
19538eef321Sdrh skipOk = 0;
1961fee73e7Sdrh }
1971fee73e7Sdrh }
198b2c8559fSdrh db->noSharedCache = skipOk;
19938eef321Sdrh }
sqlite3BtreeEnterAll(sqlite3 * db)20038eef321Sdrh void sqlite3BtreeEnterAll(sqlite3 *db){
201b2c8559fSdrh if( db->noSharedCache==0 ) btreeEnterAll(db);
20238eef321Sdrh }
btreeLeaveAll(sqlite3 * db)20338eef321Sdrh static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
204b1ab8ea7Sdrh int i;
205b1ab8ea7Sdrh Btree *p;
206b1ab8ea7Sdrh assert( sqlite3_mutex_held(db->mutex) );
2071fee73e7Sdrh for(i=0; i<db->nDb; i++){
2081fee73e7Sdrh p = db->aDb[i].pBt;
2091a86f50dSdrh if( p ) sqlite3BtreeLeave(p);
210b1ab8ea7Sdrh }
211b1ab8ea7Sdrh }
sqlite3BtreeLeaveAll(sqlite3 * db)21238eef321Sdrh void sqlite3BtreeLeaveAll(sqlite3 *db){
213b2c8559fSdrh if( db->noSharedCache==0 ) btreeLeaveAll(db);
21438eef321Sdrh }
215b1ab8ea7Sdrh
2161fee73e7Sdrh #ifndef NDEBUG
2171fee73e7Sdrh /*
2181fee73e7Sdrh ** Return true if the current thread holds the database connection
2191fee73e7Sdrh ** mutex and all required BtShared mutexes.
2201fee73e7Sdrh **
2211fee73e7Sdrh ** This routine is used inside assert() statements only.
2221fee73e7Sdrh */
sqlite3BtreeHoldsAllMutexes(sqlite3 * db)2231fee73e7Sdrh int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
2241fee73e7Sdrh int i;
2251fee73e7Sdrh if( !sqlite3_mutex_held(db->mutex) ){
2261fee73e7Sdrh return 0;
2271fee73e7Sdrh }
2281fee73e7Sdrh for(i=0; i<db->nDb; i++){
2291fee73e7Sdrh Btree *p;
2301fee73e7Sdrh p = db->aDb[i].pBt;
2311fee73e7Sdrh if( p && p->sharable &&
2321fee73e7Sdrh (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
2331fee73e7Sdrh return 0;
2341fee73e7Sdrh }
2351fee73e7Sdrh }
2361fee73e7Sdrh return 1;
2371fee73e7Sdrh }
2381fee73e7Sdrh #endif /* NDEBUG */
2391fee73e7Sdrh
2402120608eSdrh #ifndef NDEBUG
2412120608eSdrh /*
2422120608eSdrh ** Return true if the correct mutexes are held for accessing the
2432120608eSdrh ** db->aDb[iDb].pSchema structure. The mutexes required for schema
2442120608eSdrh ** access are:
2452120608eSdrh **
2462120608eSdrh ** (1) The mutex on db
2472120608eSdrh ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
2482120608eSdrh **
2492120608eSdrh ** If pSchema is not NULL, then iDb is computed from pSchema and
2502120608eSdrh ** db using sqlite3SchemaToIndex().
2512120608eSdrh */
sqlite3SchemaMutexHeld(sqlite3 * db,int iDb,Schema * pSchema)2522120608eSdrh int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
2532120608eSdrh Btree *p;
2542120608eSdrh assert( db!=0 );
255*41ce47c4Sdrh if( db->pVfs==0 && db->nDb==0 ) return 1;
2562120608eSdrh if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
2572120608eSdrh assert( iDb>=0 && iDb<db->nDb );
2582120608eSdrh if( !sqlite3_mutex_held(db->mutex) ) return 0;
2592120608eSdrh if( iDb==1 ) return 1;
2602120608eSdrh p = db->aDb[iDb].pBt;
2612120608eSdrh assert( p!=0 );
2622120608eSdrh return p->sharable==0 || p->locked==1;
2632120608eSdrh }
2642120608eSdrh #endif /* NDEBUG */
2652120608eSdrh
266bdaec52cSdrh #else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
267b1ab8ea7Sdrh /*
268bdaec52cSdrh ** The following are special cases for mutex enter routines for use
269bdaec52cSdrh ** in single threaded applications that use shared cache. Except for
270bdaec52cSdrh ** these two routines, all mutex operations are no-ops in that case and
271bdaec52cSdrh ** are null #defines in btree.h.
272900b31efSdrh **
273bdaec52cSdrh ** If shared cache is disabled, then all btree mutex routines, including
274bdaec52cSdrh ** the ones below, are no-ops and are null #defines in btree.h.
275900b31efSdrh */
276900b31efSdrh
sqlite3BtreeEnter(Btree * p)277f7590db0Sdanielk1977 void sqlite3BtreeEnter(Btree *p){
278f7590db0Sdanielk1977 p->pBt->db = p->db;
279f7590db0Sdanielk1977 }
sqlite3BtreeEnterAll(sqlite3 * db)280f7590db0Sdanielk1977 void sqlite3BtreeEnterAll(sqlite3 *db){
281f7590db0Sdanielk1977 int i;
282f7590db0Sdanielk1977 for(i=0; i<db->nDb; i++){
283f7590db0Sdanielk1977 Btree *p = db->aDb[i].pBt;
284f7590db0Sdanielk1977 if( p ){
285f7590db0Sdanielk1977 p->pBt->db = p->db;
286f7590db0Sdanielk1977 }
287f7590db0Sdanielk1977 }
288f7590db0Sdanielk1977 }
289f7590db0Sdanielk1977 #endif /* if SQLITE_THREADSAFE */
29020d876faSdan
29120d876faSdan #ifndef SQLITE_OMIT_INCRBLOB
29220d876faSdan /*
29320d876faSdan ** Enter a mutex on a Btree given a cursor owned by that Btree.
29420d876faSdan **
29520d876faSdan ** These entry points are used by incremental I/O only. Enter() is required
29620d876faSdan ** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
29720d876faSdan ** the build is threadsafe. Leave() is only required by threadsafe builds.
29820d876faSdan */
sqlite3BtreeEnterCursor(BtCursor * pCur)29920d876faSdan void sqlite3BtreeEnterCursor(BtCursor *pCur){
30020d876faSdan sqlite3BtreeEnter(pCur->pBtree);
30120d876faSdan }
30220d876faSdan # if SQLITE_THREADSAFE
sqlite3BtreeLeaveCursor(BtCursor * pCur)30320d876faSdan void sqlite3BtreeLeaveCursor(BtCursor *pCur){
30420d876faSdan sqlite3BtreeLeave(pCur->pBtree);
30520d876faSdan }
30620d876faSdan # endif
30720d876faSdan #endif /* ifndef SQLITE_OMIT_INCRBLOB */
30820d876faSdan
309f7590db0Sdanielk1977 #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
310