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

https://github.com/kumaryu/IronLanguages-main · C# · 179 lines · 92 code · 20 blank · 67 comment · 13 complexity · 2350a6df45096b4cb1454e19697d01e2 MD5 · raw file

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. namespace Community.CsharpSqlite
  5. {
  6. public partial class Sqlite3
  7. {
  8. /*
  9. ** 2007 August 14
  10. **
  11. ** The author disclaims copyright to this source code. In place of
  12. ** a legal notice, here is a blessing:
  13. **
  14. ** May you do good and not evil.
  15. ** May you find forgiveness for yourself and forgive others.
  16. ** May you share freely, never taking more than you give.
  17. **
  18. *************************************************************************
  19. ** This file contains the C functions that implement mutexes.
  20. **
  21. ** This file contains code that is common across all mutex implementations.
  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 "sqliteInt.h"
  31. #if (SQLITE_DEBUG) && !(SQLITE_MUTEX_OMIT)
  32. /*
  33. ** For debugging purposes, record when the mutex subsystem is initialized
  34. ** and uninitialized so that we can assert() if there is an attempt to
  35. ** allocate a mutex while the system is uninitialized.
  36. */
  37. static int mutexIsInit = 0;
  38. #endif //* SQLITE_DEBUG */
  39. #if !SQLITE_MUTEX_OMIT
  40. /*
  41. ** Initialize the mutex system.
  42. */
  43. static int sqlite3MutexInit(){
  44. int rc = SQLITE_OK;
  45. if( null==sqlite3GlobalConfig.mutex.xMutexAlloc ){
  46. /* If the xMutexAlloc method has not been set, then the user did not
  47. ** install a mutex implementation via sqlite3_config() prior to
  48. ** sqlite3_initialize() being called. This block copies pointers to
  49. ** the default implementation into the sqlite3GlobalConfig structure.
  50. */
  51. sqlite3_mutex_methods pFrom;
  52. sqlite3_mutex_methods pTo = sqlite3GlobalConfig.mutex;
  53. if( sqlite3GlobalConfig.bCoreMutex ){
  54. pFrom = sqlite3DefaultMutex();
  55. }else{
  56. pFrom = sqlite3NoopMutex();
  57. }
  58. //memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
  59. //memcpy(pTo.xMutexFree, pFrom.xMutexFree,
  60. // sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
  61. pTo.Copy(pFrom);
  62. }
  63. rc = sqlite3GlobalConfig.mutex.xMutexInit();
  64. #if SQLITE_DEBUG
  65. mutexIsInit = 1; //GLOBAL(int, mutexIsInit) = 1;
  66. #endif
  67. return rc;
  68. }
  69. /*
  70. ** Shutdown the mutex system. This call frees resources allocated by
  71. ** sqlite3MutexInit().
  72. */
  73. static int sqlite3MutexEnd(){
  74. int rc = SQLITE_OK;
  75. if( sqlite3GlobalConfig.mutex.xMutexEnd !=null){
  76. rc = sqlite3GlobalConfig.mutex.xMutexEnd();
  77. }
  78. #if SQLITE_DEBUG
  79. mutexIsInit = 0;//GLOBAL(int, mutexIsInit) = 0;
  80. #endif
  81. return rc;
  82. }
  83. /*
  84. ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
  85. */
  86. static sqlite3_mutex sqlite3_mutex_alloc(int id){
  87. #if !SQLITE_OMIT_AUTOINIT
  88. if( sqlite3_initialize()!=0 ) return null;
  89. #endif
  90. return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
  91. }
  92. static sqlite3_mutex sqlite3MutexAlloc(int id){
  93. if( !sqlite3GlobalConfig.bCoreMutex ){
  94. return null;
  95. }
  96. Debug.Assert( mutexIsInit !=0 );//assert( GLOBAL(int, mutexIsInit) );
  97. return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
  98. }
  99. /*
  100. ** Free a dynamic mutex.
  101. */
  102. static void sqlite3_mutex_free( sqlite3_mutex p){
  103. if( p!=null ){
  104. sqlite3GlobalConfig.mutex.xMutexFree( p);
  105. }
  106. }
  107. /*
  108. ** Obtain the mutex p. If some other thread already has the mutex, block
  109. ** until it can be obtained.
  110. */
  111. static void sqlite3_mutex_enter(sqlite3_mutex p){
  112. if( p !=null){
  113. sqlite3GlobalConfig.mutex.xMutexEnter(p);
  114. }
  115. }
  116. /*
  117. ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
  118. ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
  119. */
  120. static int sqlite3_mutex_try(sqlite3_mutex p){
  121. int rc = SQLITE_OK;
  122. if( p!=null ){
  123. return sqlite3GlobalConfig.mutex.xMutexTry(p);
  124. }
  125. return rc;
  126. }
  127. /*
  128. ** The sqlite3_mutex_leave() routine exits a mutex that was previously
  129. ** entered by the same thread. The behavior is undefined if the mutex
  130. ** is not currently entered. If a NULL pointer is passed as an argument
  131. ** this function is a no-op.
  132. */
  133. static void sqlite3_mutex_leave(sqlite3_mutex p){
  134. if( p !=null){
  135. sqlite3GlobalConfig.mutex.xMutexLeave(p);
  136. }
  137. }
  138. #if !NDEBUG
  139. /*
  140. ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  141. ** intended for use inside assert() statements.
  142. */
  143. static bool sqlite3_mutex_held(sqlite3_mutex p){
  144. return p==null || sqlite3GlobalConfig.mutex.xMutexHeld(p);
  145. }
  146. static bool sqlite3_mutex_notheld(sqlite3_mutex p){
  147. return p == null || sqlite3GlobalConfig.mutex.xMutexNotheld( p );
  148. }
  149. #else
  150. static bool sqlite3_mutex_held(sqlite3_mutex p) {
  151. return true;
  152. }
  153. static bool sqlite3_mutex_notheld(sqlite3_mutex p) {
  154. return true;
  155. }
  156. #endif
  157. #endif //* SQLITE_MUTEX_OMIT */
  158. }
  159. }