wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System.Diagnostics; |
2 | |||
3 | namespace Community.CsharpSqlite |
||
4 | { |
||
5 | public partial class Sqlite3 |
||
6 | { |
||
7 | /* |
||
8 | ** 2007 August 27 |
||
9 | ** |
||
10 | ** The author disclaims copyright to this source code. In place of |
||
11 | ** a legal notice, here is a blessing: |
||
12 | ** |
||
13 | ** May you do good and not evil. |
||
14 | ** May you find forgiveness for yourself and forgive others. |
||
15 | ** May you share freely, never taking more than you give. |
||
16 | ** |
||
17 | ************************************************************************* |
||
18 | ** |
||
19 | ** This file contains code used to implement mutexes on Btree objects. |
||
20 | ** This code really belongs in btree.c. But btree.c is getting too |
||
21 | ** big and we want to break it down some. This packaged seemed like |
||
22 | ** a good breakout. |
||
23 | ************************************************************************* |
||
24 | ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart |
||
25 | ** C#-SQLite is an independent reimplementation of the SQLite software library |
||
26 | ** |
||
27 | ** SQLITE_SOURCE_ID: 2011-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908e |
||
28 | ** |
||
29 | ************************************************************************* |
||
30 | */ |
||
31 | //#include "btreeInt.h" |
||
32 | #if !SQLITE_OMIT_SHARED_CACHE |
||
33 | #if SQLITE_THREADSAFE |
||
34 | |||
35 | /* |
||
36 | ** Obtain the BtShared mutex associated with B-Tree handle p. Also, |
||
37 | ** set BtShared.db to the database handle associated with p and the |
||
38 | ** p->locked boolean to true. |
||
39 | */ |
||
40 | static void lockBtreeMutex(Btree *p){ |
||
41 | assert( p->locked==0 ); |
||
42 | assert( sqlite3_mutex_notheld(p->pBt->mutex) ); |
||
43 | assert( sqlite3_mutex_held(p->db->mutex) ); |
||
44 | |||
45 | sqlite3_mutex_enter(p->pBt->mutex); |
||
46 | p->pBt->db = p->db; |
||
47 | p->locked = 1; |
||
48 | } |
||
49 | |||
50 | /* |
||
51 | ** Release the BtShared mutex associated with B-Tree handle p and |
||
52 | ** clear the p->locked boolean. |
||
53 | */ |
||
54 | static void unlockBtreeMutex(Btree *p){ |
||
55 | BtShared *pBt = p->pBt; |
||
56 | assert( p->locked==1 ); |
||
57 | assert( sqlite3_mutex_held(pBt->mutex) ); |
||
58 | assert( sqlite3_mutex_held(p->db->mutex) ); |
||
59 | assert( p->db==pBt->db ); |
||
60 | |||
61 | sqlite3_mutex_leave(pBt->mutex); |
||
62 | p->locked = 0; |
||
63 | } |
||
64 | |||
65 | /* |
||
66 | ** Enter a mutex on the given BTree object. |
||
67 | ** |
||
68 | ** If the object is not sharable, then no mutex is ever required |
||
69 | ** and this routine is a no-op. The underlying mutex is non-recursive. |
||
70 | ** But we keep a reference count in Btree.wantToLock so the behavior |
||
71 | ** of this interface is recursive. |
||
72 | ** |
||
73 | ** To avoid deadlocks, multiple Btrees are locked in the same order |
||
74 | ** by all database connections. The p->pNext is a list of other |
||
75 | ** Btrees belonging to the same database connection as the p Btree |
||
76 | ** which need to be locked after p. If we cannot get a lock on |
||
77 | ** p, then first unlock all of the others on p->pNext, then wait |
||
78 | ** for the lock to become available on p, then relock all of the |
||
79 | ** subsequent Btrees that desire a lock. |
||
80 | */ |
||
81 | void sqlite3BtreeEnter(Btree *p){ |
||
82 | Btree *pLater; |
||
83 | |||
84 | /* Some basic sanity checking on the Btree. The list of Btrees |
||
85 | ** connected by pNext and pPrev should be in sorted order by |
||
86 | ** Btree.pBt value. All elements of the list should belong to |
||
87 | ** the same connection. Only shared Btrees are on the list. */ |
||
88 | assert( p->pNext==0 || p->pNext->pBt>p->pBt ); |
||
89 | assert( p->pPrev==0 || p->pPrev->pBt<p->pBt ); |
||
90 | assert( p->pNext==0 || p->pNext->db==p->db ); |
||
91 | assert( p->pPrev==0 || p->pPrev->db==p->db ); |
||
92 | assert( p->sharable || (p->pNext==0 && p->pPrev==0) ); |
||
93 | |||
94 | /* Check for locking consistency */ |
||
95 | assert( !p->locked || p->wantToLock>0 ); |
||
96 | assert( p->sharable || p->wantToLock==0 ); |
||
97 | |||
98 | /* We should already hold a lock on the database connection */ |
||
99 | assert( sqlite3_mutex_held(p->db->mutex) ); |
||
100 | |||
101 | /* Unless the database is sharable and unlocked, then BtShared.db |
||
102 | ** should already be set correctly. */ |
||
103 | assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db ); |
||
104 | |||
105 | if( !p->sharable ) return; |
||
106 | p->wantToLock++; |
||
107 | if( p->locked ) return; |
||
108 | |||
109 | /* In most cases, we should be able to acquire the lock we |
||
110 | ** want without having to go throught the ascending lock |
||
111 | ** procedure that follows. Just be sure not to block. |
||
112 | */ |
||
113 | if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){ |
||
114 | p->pBt->db = p->db; |
||
115 | p->locked = 1; |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | /* To avoid deadlock, first release all locks with a larger |
||
120 | ** BtShared address. Then acquire our lock. Then reacquire |
||
121 | ** the other BtShared locks that we used to hold in ascending |
||
122 | ** order. |
||
123 | */ |
||
124 | for(pLater=p->pNext; pLater; pLater=pLater->pNext){ |
||
125 | assert( pLater->sharable ); |
||
126 | assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt ); |
||
127 | assert( !pLater->locked || pLater->wantToLock>0 ); |
||
128 | if( pLater->locked ){ |
||
129 | unlockBtreeMutex(pLater); |
||
130 | } |
||
131 | } |
||
132 | lockBtreeMutex(p); |
||
133 | for(pLater=p->pNext; pLater; pLater=pLater->pNext){ |
||
134 | if( pLater->wantToLock ){ |
||
135 | lockBtreeMutex(pLater); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /* |
||
141 | ** Exit the recursive mutex on a Btree. |
||
142 | */ |
||
143 | void sqlite3BtreeLeave(Btree *p){ |
||
144 | if( p->sharable ){ |
||
145 | assert( p->wantToLock>0 ); |
||
146 | p->wantToLock--; |
||
147 | if( p->wantToLock==0 ){ |
||
148 | unlockBtreeMutex(p); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | #if NDEBUG |
||
154 | /* |
||
155 | ** Return true if the BtShared mutex is held on the btree, or if the |
||
156 | ** B-Tree is not marked as sharable. |
||
157 | ** |
||
158 | ** This routine is used only from within assert() statements. |
||
159 | */ |
||
160 | int sqlite3BtreeHoldsMutex(Btree *p){ |
||
161 | assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 ); |
||
162 | assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db ); |
||
163 | assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) ); |
||
164 | assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) ); |
||
165 | |||
166 | return (p->sharable==0 || p->locked); |
||
167 | } |
||
168 | #endif |
||
169 | |||
170 | |||
171 | #if SQLITE_OMIT_INCRBLOB |
||
172 | /* |
||
173 | ** Enter and leave a mutex on a Btree given a cursor owned by that |
||
174 | ** Btree. These entry points are used by incremental I/O and can be |
||
175 | ** omitted if that module is not used. |
||
176 | */ |
||
177 | void sqlite3BtreeEnterCursor(BtCursor *pCur){ |
||
178 | sqlite3BtreeEnter(pCur->pBtree); |
||
179 | } |
||
180 | void sqlite3BtreeLeaveCursor(BtCursor *pCur){ |
||
181 | sqlite3BtreeLeave(pCur->pBtree); |
||
182 | } |
||
183 | #endif //* SQLITE_OMIT_INCRBLOB */ |
||
184 | |||
185 | |||
186 | /* |
||
187 | ** Enter the mutex on every Btree associated with a database |
||
188 | ** connection. This is needed (for example) prior to parsing |
||
189 | ** a statement since we will be comparing table and column names |
||
190 | ** against all schemas and we do not want those schemas being |
||
191 | ** reset out from under us. |
||
192 | ** |
||
193 | ** There is a corresponding leave-all procedures. |
||
194 | ** |
||
195 | ** Enter the mutexes in accending order by BtShared pointer address |
||
196 | ** to avoid the possibility of deadlock when two threads with |
||
197 | ** two or more btrees in common both try to lock all their btrees |
||
198 | ** at the same instant. |
||
199 | */ |
||
200 | void sqlite3BtreeEnterAll(sqlite3 db){ |
||
201 | int i; |
||
202 | Btree *p; |
||
203 | assert( sqlite3_mutex_held(db->mutex) ); |
||
204 | for(i=0; i<db->nDb; i++){ |
||
205 | p = db->aDb[i].pBt; |
||
206 | if( p ) sqlite3BtreeEnter(p); |
||
207 | } |
||
208 | } |
||
209 | void sqlite3BtreeLeaveAll(sqlite3 db){ |
||
210 | int i; |
||
211 | Btree *p; |
||
212 | assert( sqlite3_mutex_held(db->mutex) ); |
||
213 | for(i=0; i<db->nDb; i++){ |
||
214 | p = db->aDb[i].pBt; |
||
215 | if( p ) sqlite3BtreeLeave(p); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | /* |
||
220 | ** Return true if a particular Btree requires a lock. Return FALSE if |
||
221 | ** no lock is ever required since it is not sharable. |
||
222 | */ |
||
223 | int sqlite3BtreeSharable(Btree *p){ |
||
224 | return p->sharable; |
||
225 | } |
||
226 | |||
227 | #if NDEBUG |
||
228 | /* |
||
229 | ** Return true if the current thread holds the database connection |
||
230 | ** mutex and all required BtShared mutexes. |
||
231 | ** |
||
232 | ** This routine is used inside assert() statements only. |
||
233 | */ |
||
234 | int sqlite3BtreeHoldsAllMutexes(sqlite3 db){ |
||
235 | int i; |
||
236 | if( !sqlite3_mutex_held(db->mutex) ){ |
||
237 | return 0; |
||
238 | } |
||
239 | for(i=0; i<db->nDb; i++){ |
||
240 | Btree *p; |
||
241 | p = db->aDb[i].pBt; |
||
242 | if( p && p->sharable && |
||
243 | (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){ |
||
244 | return 0; |
||
245 | } |
||
246 | } |
||
247 | return 1; |
||
248 | } |
||
249 | #endif //* NDEBUG */ |
||
250 | |||
251 | #if NDEBUG |
||
252 | /* |
||
253 | ** Return true if the correct mutexes are held for accessing the |
||
254 | ** db->aDb[iDb].pSchema structure. The mutexes required for schema |
||
255 | ** access are: |
||
256 | ** |
||
257 | ** (1) The mutex on db |
||
258 | ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt. |
||
259 | ** |
||
260 | ** If pSchema is not NULL, then iDb is computed from pSchema and |
||
261 | ** db using sqlite3SchemaToIndex(). |
||
262 | */ |
||
263 | int sqlite3SchemaMutexHeld(sqlite3 db, int iDb, Schema *pSchema){ |
||
264 | Btree *p; |
||
265 | assert( db!=0 ); |
||
266 | if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema); |
||
267 | assert( iDb>=0 && iDb<db->nDb ); |
||
268 | if( !sqlite3_mutex_held(db->mutex) ) return 0; |
||
269 | if( iDb==1 ) return 1; |
||
270 | p = db->aDb[iDb].pBt; |
||
271 | assert( p!=0 ); |
||
272 | return p->sharable==0 || p->locked==1; |
||
273 | } |
||
274 | #endif //* NDEBUG */ |
||
275 | |||
276 | #else //* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */ |
||
277 | /* |
||
278 | ** The following are special cases for mutex enter routines for use |
||
279 | ** in single threaded applications that use shared cache. Except for |
||
280 | ** these two routines, all mutex operations are no-ops in that case and |
||
281 | ** are null #defines in btree.h. |
||
282 | ** |
||
283 | ** If shared cache is disabled, then all btree mutex routines, including |
||
284 | ** the ones below, are no-ops and are null #defines in btree.h. |
||
285 | */ |
||
286 | |||
287 | void sqlite3BtreeEnter(Btree *p){ |
||
288 | p->pBt->db = p->db; |
||
289 | } |
||
290 | void sqlite3BtreeEnterAll(sqlite3 db){ |
||
291 | int i; |
||
292 | for(i=0; i<db->nDb; i++){ |
||
293 | Btree *p = db->aDb[i].pBt; |
||
294 | if( p ){ |
||
295 | p->pBt->db = p->db; |
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | #endif //* if SQLITE_THREADSAFE */ |
||
300 | #endif //* ifndef SQLITE_OMIT_SHARED_CACHE */ |
||
301 | |||
302 | } |
||
303 | } |