PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/sqlite3/sqlite-src-3070900/src/btmutex.c

https://github.com/rogerz/uffs
C | 287 lines | 146 code | 24 blank | 117 comment | 46 complexity | 0866c4b27d5b69472247bd4c9091605b MD5 | raw file
  1. /*
  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. **
  13. ** 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"
  19. #ifndef SQLITE_OMIT_SHARED_CACHE
  20. #if SQLITE_THREADSAFE
  21. /*
  22. ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
  23. ** set BtShared.db to the database handle associated with p and the
  24. ** p->locked boolean to true.
  25. */
  26. static void lockBtreeMutex(Btree *p){
  27. assert( p->locked==0 );
  28. assert( sqlite3_mutex_notheld(p->pBt->mutex) );
  29. assert( sqlite3_mutex_held(p->db->mutex) );
  30. sqlite3_mutex_enter(p->pBt->mutex);
  31. p->pBt->db = p->db;
  32. p->locked = 1;
  33. }
  34. /*
  35. ** Release the BtShared mutex associated with B-Tree handle p and
  36. ** clear the p->locked boolean.
  37. */
  38. static void unlockBtreeMutex(Btree *p){
  39. BtShared *pBt = p->pBt;
  40. assert( p->locked==1 );
  41. assert( sqlite3_mutex_held(pBt->mutex) );
  42. assert( sqlite3_mutex_held(p->db->mutex) );
  43. assert( p->db==pBt->db );
  44. sqlite3_mutex_leave(pBt->mutex);
  45. p->locked = 0;
  46. }
  47. /*
  48. ** Enter a mutex on the given BTree object.
  49. **
  50. ** If the object is not sharable, then no mutex is ever required
  51. ** and this routine is a no-op. The underlying mutex is non-recursive.
  52. ** But we keep a reference count in Btree.wantToLock so the behavior
  53. ** of this interface is recursive.
  54. **
  55. ** To avoid deadlocks, multiple Btrees are locked in the same order
  56. ** by all database connections. The p->pNext is a list of other
  57. ** Btrees belonging to the same database connection as the p Btree
  58. ** which need to be locked after p. If we cannot get a lock on
  59. ** p, then first unlock all of the others on p->pNext, then wait
  60. ** for the lock to become available on p, then relock all of the
  61. ** subsequent Btrees that desire a lock.
  62. */
  63. void sqlite3BtreeEnter(Btree *p){
  64. Btree *pLater;
  65. /* Some basic sanity checking on the Btree. The list of Btrees
  66. ** connected by pNext and pPrev should be in sorted order by
  67. ** Btree.pBt value. All elements of the list should belong to
  68. ** the same connection. Only shared Btrees are on the list. */
  69. assert( p->pNext==0 || p->pNext->pBt>p->pBt );
  70. assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
  71. assert( p->pNext==0 || p->pNext->db==p->db );
  72. assert( p->pPrev==0 || p->pPrev->db==p->db );
  73. assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
  74. /* Check for locking consistency */
  75. assert( !p->locked || p->wantToLock>0 );
  76. assert( p->sharable || p->wantToLock==0 );
  77. /* We should already hold a lock on the database connection */
  78. assert( sqlite3_mutex_held(p->db->mutex) );
  79. /* Unless the database is sharable and unlocked, then BtShared.db
  80. ** should already be set correctly. */
  81. assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
  82. if( !p->sharable ) return;
  83. p->wantToLock++;
  84. if( p->locked ) return;
  85. /* In most cases, we should be able to acquire the lock we
  86. ** want without having to go throught the ascending lock
  87. ** procedure that follows. Just be sure not to block.
  88. */
  89. if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
  90. p->pBt->db = p->db;
  91. p->locked = 1;
  92. return;
  93. }
  94. /* To avoid deadlock, first release all locks with a larger
  95. ** BtShared address. Then acquire our lock. Then reacquire
  96. ** the other BtShared locks that we used to hold in ascending
  97. ** order.
  98. */
  99. for(pLater=p->pNext; pLater; pLater=pLater->pNext){
  100. assert( pLater->sharable );
  101. assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
  102. assert( !pLater->locked || pLater->wantToLock>0 );
  103. if( pLater->locked ){
  104. unlockBtreeMutex(pLater);
  105. }
  106. }
  107. lockBtreeMutex(p);
  108. for(pLater=p->pNext; pLater; pLater=pLater->pNext){
  109. if( pLater->wantToLock ){
  110. lockBtreeMutex(pLater);
  111. }
  112. }
  113. }
  114. /*
  115. ** Exit the recursive mutex on a Btree.
  116. */
  117. void sqlite3BtreeLeave(Btree *p){
  118. if( p->sharable ){
  119. assert( p->wantToLock>0 );
  120. p->wantToLock--;
  121. if( p->wantToLock==0 ){
  122. unlockBtreeMutex(p);
  123. }
  124. }
  125. }
  126. #ifndef NDEBUG
  127. /*
  128. ** Return true if the BtShared mutex is held on the btree, or if the
  129. ** B-Tree is not marked as sharable.
  130. **
  131. ** This routine is used only from within assert() statements.
  132. */
  133. int sqlite3BtreeHoldsMutex(Btree *p){
  134. assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
  135. assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
  136. assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
  137. assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
  138. return (p->sharable==0 || p->locked);
  139. }
  140. #endif
  141. #ifndef SQLITE_OMIT_INCRBLOB
  142. /*
  143. ** Enter and leave a mutex on a Btree given a cursor owned by that
  144. ** Btree. These entry points are used by incremental I/O and can be
  145. ** omitted if that module is not used.
  146. */
  147. void sqlite3BtreeEnterCursor(BtCursor *pCur){
  148. sqlite3BtreeEnter(pCur->pBtree);
  149. }
  150. void sqlite3BtreeLeaveCursor(BtCursor *pCur){
  151. sqlite3BtreeLeave(pCur->pBtree);
  152. }
  153. #endif /* SQLITE_OMIT_INCRBLOB */
  154. /*
  155. ** Enter the mutex on every Btree associated with a database
  156. ** connection. This is needed (for example) prior to parsing
  157. ** a statement since we will be comparing table and column names
  158. ** against all schemas and we do not want those schemas being
  159. ** reset out from under us.
  160. **
  161. ** There is a corresponding leave-all procedures.
  162. **
  163. ** Enter the mutexes in accending order by BtShared pointer address
  164. ** to avoid the possibility of deadlock when two threads with
  165. ** two or more btrees in common both try to lock all their btrees
  166. ** at the same instant.
  167. */
  168. void sqlite3BtreeEnterAll(sqlite3 *db){
  169. int i;
  170. Btree *p;
  171. assert( sqlite3_mutex_held(db->mutex) );
  172. for(i=0; i<db->nDb; i++){
  173. p = db->aDb[i].pBt;
  174. if( p ) sqlite3BtreeEnter(p);
  175. }
  176. }
  177. void sqlite3BtreeLeaveAll(sqlite3 *db){
  178. int i;
  179. Btree *p;
  180. assert( sqlite3_mutex_held(db->mutex) );
  181. for(i=0; i<db->nDb; i++){
  182. p = db->aDb[i].pBt;
  183. if( p ) sqlite3BtreeLeave(p);
  184. }
  185. }
  186. /*
  187. ** Return true if a particular Btree requires a lock. Return FALSE if
  188. ** no lock is ever required since it is not sharable.
  189. */
  190. int sqlite3BtreeSharable(Btree *p){
  191. return p->sharable;
  192. }
  193. #ifndef NDEBUG
  194. /*
  195. ** Return true if the current thread holds the database connection
  196. ** mutex and all required BtShared mutexes.
  197. **
  198. ** This routine is used inside assert() statements only.
  199. */
  200. int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
  201. int i;
  202. if( !sqlite3_mutex_held(db->mutex) ){
  203. return 0;
  204. }
  205. for(i=0; i<db->nDb; i++){
  206. Btree *p;
  207. p = db->aDb[i].pBt;
  208. if( p && p->sharable &&
  209. (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
  210. return 0;
  211. }
  212. }
  213. return 1;
  214. }
  215. #endif /* NDEBUG */
  216. #ifndef NDEBUG
  217. /*
  218. ** Return true if the correct mutexes are held for accessing the
  219. ** db->aDb[iDb].pSchema structure. The mutexes required for schema
  220. ** access are:
  221. **
  222. ** (1) The mutex on db
  223. ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
  224. **
  225. ** If pSchema is not NULL, then iDb is computed from pSchema and
  226. ** db using sqlite3SchemaToIndex().
  227. */
  228. int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
  229. Btree *p;
  230. assert( db!=0 );
  231. if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
  232. assert( iDb>=0 && iDb<db->nDb );
  233. if( !sqlite3_mutex_held(db->mutex) ) return 0;
  234. if( iDb==1 ) return 1;
  235. p = db->aDb[iDb].pBt;
  236. assert( p!=0 );
  237. return p->sharable==0 || p->locked==1;
  238. }
  239. #endif /* NDEBUG */
  240. #else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
  241. /*
  242. ** The following are special cases for mutex enter routines for use
  243. ** in single threaded applications that use shared cache. Except for
  244. ** these two routines, all mutex operations are no-ops in that case and
  245. ** are null #defines in btree.h.
  246. **
  247. ** If shared cache is disabled, then all btree mutex routines, including
  248. ** the ones below, are no-ops and are null #defines in btree.h.
  249. */
  250. void sqlite3BtreeEnter(Btree *p){
  251. p->pBt->db = p->db;
  252. }
  253. void sqlite3BtreeEnterAll(sqlite3 *db){
  254. int i;
  255. for(i=0; i<db->nDb; i++){
  256. Btree *p = db->aDb[i].pBt;
  257. if( p ){
  258. p->pBt->db = p->db;
  259. }
  260. }
  261. }
  262. #endif /* if SQLITE_THREADSAFE */
  263. #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */