mutex_w32.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ** 2007 August 14
  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. ** This file contains the C functions that implement mutexes for win32
  13. **
  14. ** $Id: mutex_w32.c,v 1.5 2007/10/05 15:08:01 drh Exp $
  15. */
  16. #include "sqliteInt.h"
  17. /*
  18. ** The code in this file is only used if we are compiling multithreaded
  19. ** on a win32 system.
  20. */
  21. #ifdef SQLITE_MUTEX_W32
  22. /*
  23. ** Each recursive mutex is an instance of the following structure.
  24. */
  25. struct sqlite3_mutex {
  26. CRITICAL_SECTION mutex; /* Mutex controlling the lock */
  27. int id; /* Mutex type */
  28. int nRef; /* Number of enterances */
  29. DWORD owner; /* Thread holding this mutex */
  30. };
  31. /*
  32. ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
  33. ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
  34. **
  35. ** Here is an interesting observation: Win95, Win98, and WinME lack
  36. ** the LockFileEx() API. But we can still statically link against that
  37. ** API as long as we don't call it win running Win95/98/ME. A call to
  38. ** this routine is used to determine if the host is Win95/98/ME or
  39. ** WinNT/2K/XP so that we will know whether or not we can safely call
  40. ** the LockFileEx() API.
  41. */
  42. #if OS_WINCE
  43. # define mutexIsNT() (1)
  44. #else
  45. static int mutexIsNT(void){
  46. static int osType = 0;
  47. if( osType==0 ){
  48. OSVERSIONINFO sInfo;
  49. sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  50. GetVersionEx(&sInfo);
  51. osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  52. }
  53. return osType==2;
  54. }
  55. #endif /* OS_WINCE */
  56. /*
  57. ** The sqlite3_mutex_alloc() routine allocates a new
  58. ** mutex and returns a pointer to it. If it returns NULL
  59. ** that means that a mutex could not be allocated. SQLite
  60. ** will unwind its stack and return an error. The argument
  61. ** to sqlite3_mutex_alloc() is one of these integer constants:
  62. **
  63. ** <ul>
  64. ** <li> SQLITE_MUTEX_FAST 0
  65. ** <li> SQLITE_MUTEX_RECURSIVE 1
  66. ** <li> SQLITE_MUTEX_STATIC_MASTER 2
  67. ** <li> SQLITE_MUTEX_STATIC_MEM 3
  68. ** <li> SQLITE_MUTEX_STATIC_PRNG 4
  69. ** </ul>
  70. **
  71. ** The first two constants cause sqlite3_mutex_alloc() to create
  72. ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  73. ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  74. ** The mutex implementation does not need to make a distinction
  75. ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  76. ** not want to. But SQLite will only request a recursive mutex in
  77. ** cases where it really needs one. If a faster non-recursive mutex
  78. ** implementation is available on the host platform, the mutex subsystem
  79. ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  80. **
  81. ** The other allowed parameters to sqlite3_mutex_alloc() each return
  82. ** a pointer to a static preexisting mutex. Three static mutexes are
  83. ** used by the current version of SQLite. Future versions of SQLite
  84. ** may add additional static mutexes. Static mutexes are for internal
  85. ** use by SQLite only. Applications that use SQLite mutexes should
  86. ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  87. ** SQLITE_MUTEX_RECURSIVE.
  88. **
  89. ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  90. ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  91. ** returns a different mutex on every call. But for the static
  92. ** mutex types, the same mutex is returned on every call that has
  93. ** the same type number.
  94. */
  95. sqlite3_mutex *sqlite3_mutex_alloc(int iType){
  96. sqlite3_mutex *p;
  97. switch( iType ){
  98. case SQLITE_MUTEX_FAST:
  99. case SQLITE_MUTEX_RECURSIVE: {
  100. p = sqlite3MallocZero( sizeof(*p) );
  101. if( p ){
  102. p->id = iType;
  103. InitializeCriticalSection(&p->mutex);
  104. }
  105. break;
  106. }
  107. default: {
  108. static sqlite3_mutex staticMutexes[5];
  109. static int isInit = 0;
  110. while( !isInit ){
  111. static long lock = 0;
  112. if( InterlockedIncrement(&lock)==1 ){
  113. int i;
  114. for(i=0; i<sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++){
  115. InitializeCriticalSection(&staticMutexes[i].mutex);
  116. }
  117. isInit = 1;
  118. }else{
  119. Sleep(1);
  120. }
  121. }
  122. assert( iType-2 >= 0 );
  123. assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
  124. p = &staticMutexes[iType-2];
  125. p->id = iType;
  126. break;
  127. }
  128. }
  129. return p;
  130. }
  131. /*
  132. ** This routine deallocates a previously
  133. ** allocated mutex. SQLite is careful to deallocate every
  134. ** mutex that it allocates.
  135. */
  136. void sqlite3_mutex_free(sqlite3_mutex *p){
  137. assert( p );
  138. assert( p->nRef==0 );
  139. assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  140. DeleteCriticalSection(&p->mutex);
  141. sqlite3_free(p);
  142. }
  143. /*
  144. ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  145. ** to enter a mutex. If another thread is already within the mutex,
  146. ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  147. ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
  148. ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
  149. ** be entered multiple times by the same thread. In such cases the,
  150. ** mutex must be exited an equal number of times before another thread
  151. ** can enter. If the same thread tries to enter any other kind of mutex
  152. ** more than once, the behavior is undefined.
  153. */
  154. void sqlite3_mutex_enter(sqlite3_mutex *p){
  155. assert( p );
  156. assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
  157. EnterCriticalSection(&p->mutex);
  158. p->owner = GetCurrentThreadId();
  159. p->nRef++;
  160. }
  161. int sqlite3_mutex_try(sqlite3_mutex *p){
  162. int rc = SQLITE_BUSY;
  163. assert( p );
  164. assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
  165. /*
  166. ** The sqlite3_mutex_try() routine is very rarely used, and when it
  167. ** is used it is merely an optimization. So it is OK for it to always
  168. ** fail.
  169. **
  170. ** The TryEnterCriticalSection() interface is only available on WinNT.
  171. ** And some windows compilers complain if you try to use it without
  172. ** first doing some #defines that prevent SQLite from building on Win98.
  173. ** For that reason, we will omit this optimization for now. See
  174. ** ticket #2685.
  175. */
  176. #if 0
  177. if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
  178. p->owner = GetCurrentThreadId();
  179. p->nRef++;
  180. rc = SQLITE_OK;
  181. }
  182. #endif
  183. return rc;
  184. }
  185. /*
  186. ** The sqlite3_mutex_leave() routine exits a mutex that was
  187. ** previously entered by the same thread. The behavior
  188. ** is undefined if the mutex is not currently entered or
  189. ** is not currently allocated. SQLite will never do either.
  190. */
  191. void sqlite3_mutex_leave(sqlite3_mutex *p){
  192. assert( p->nRef>0 );
  193. assert( p->owner==GetCurrentThreadId() );
  194. p->nRef--;
  195. assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
  196. LeaveCriticalSection(&p->mutex);
  197. }
  198. /*
  199. ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  200. ** intended for use only inside assert() statements.
  201. */
  202. int sqlite3_mutex_held(sqlite3_mutex *p){
  203. return p==0 || (p->nRef!=0 && p->owner==GetCurrentThreadId());
  204. }
  205. int sqlite3_mutex_notheld(sqlite3_mutex *p){
  206. return p==0 || p->nRef==0 || p->owner!=GetCurrentThreadId();
  207. }
  208. #endif /* SQLITE_MUTEX_W32 */