PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/IronPython/IronPython.SQLite/c#sqlite/btmutex_c.cs

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