mutex_w32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #include "sqliteInt.h"
  15. /*
  16. ** The code in this file is only used if we are compiling multithreaded
  17. ** on a win32 system.
  18. */
  19. #ifdef SQLITE_MUTEX_W32
  20. /*
  21. ** Each recursive mutex is an instance of the following structure.
  22. */
  23. struct sqlite3_mutex {
  24. CRITICAL_SECTION mutex; /* Mutex controlling the lock */
  25. int id; /* Mutex type */
  26. #ifdef SQLITE_DEBUG
  27. volatile int nRef; /* Number of enterances */
  28. volatile DWORD owner; /* Thread holding this mutex */
  29. int trace; /* True to trace changes */
  30. #endif
  31. };
  32. #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
  33. #ifdef SQLITE_DEBUG
  34. #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
  35. #else
  36. #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
  37. #endif
  38. /*
  39. ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
  40. ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
  41. **
  42. ** Here is an interesting observation: Win95, Win98, and WinME lack
  43. ** the LockFileEx() API. But we can still statically link against that
  44. ** API as long as we don't call it win running Win95/98/ME. A call to
  45. ** this routine is used to determine if the host is Win95/98/ME or
  46. ** WinNT/2K/XP so that we will know whether or not we can safely call
  47. ** the LockFileEx() API.
  48. **
  49. ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
  50. ** which is only available if your application was compiled with
  51. ** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
  52. ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
  53. ** this out as well.
  54. */
  55. #if 0
  56. #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
  57. # define mutexIsNT() (1)
  58. #else
  59. static int mutexIsNT(void){
  60. static int osType = 0;
  61. if( osType==0 ){
  62. OSVERSIONINFO sInfo;
  63. sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  64. GetVersionEx(&sInfo);
  65. osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  66. }
  67. return osType==2;
  68. }
  69. #endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
  70. #endif
  71. #ifdef SQLITE_DEBUG
  72. /*
  73. ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  74. ** intended for use only inside assert() statements.
  75. */
  76. static int winMutexHeld(sqlite3_mutex *p){
  77. return p->nRef!=0 && p->owner==GetCurrentThreadId();
  78. }
  79. static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
  80. return p->nRef==0 || p->owner!=tid;
  81. }
  82. static int winMutexNotheld(sqlite3_mutex *p){
  83. DWORD tid = GetCurrentThreadId();
  84. return winMutexNotheld2(p, tid);
  85. }
  86. #endif
  87. /*
  88. ** Initialize and deinitialize the mutex subsystem.
  89. */
  90. static sqlite3_mutex winMutex_staticMutexes[6] = {
  91. SQLITE3_MUTEX_INITIALIZER,
  92. SQLITE3_MUTEX_INITIALIZER,
  93. SQLITE3_MUTEX_INITIALIZER,
  94. SQLITE3_MUTEX_INITIALIZER,
  95. SQLITE3_MUTEX_INITIALIZER,
  96. SQLITE3_MUTEX_INITIALIZER
  97. };
  98. static int winMutex_isInit = 0;
  99. /* As winMutexInit() and winMutexEnd() are called as part
  100. ** of the sqlite3_initialize and sqlite3_shutdown()
  101. ** processing, the "interlocked" magic is probably not
  102. ** strictly necessary.
  103. */
  104. static LONG winMutex_lock = 0;
  105. void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
  106. static int winMutexInit(void){
  107. /* The first to increment to 1 does actual initialization */
  108. if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
  109. int i;
  110. for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
  111. #if SQLITE_OS_WINRT
  112. InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
  113. #else
  114. InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
  115. #endif
  116. }
  117. winMutex_isInit = 1;
  118. }else{
  119. /* Someone else is in the process of initing the static mutexes */
  120. while( !winMutex_isInit ){
  121. sqlite3_win32_sleep(1);
  122. }
  123. }
  124. return SQLITE_OK;
  125. }
  126. static int winMutexEnd(void){
  127. /* The first to decrement to 0 does actual shutdown
  128. ** (which should be the last to shutdown.) */
  129. if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
  130. if( winMutex_isInit==1 ){
  131. int i;
  132. for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
  133. DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
  134. }
  135. winMutex_isInit = 0;
  136. }
  137. }
  138. return SQLITE_OK;
  139. }
  140. /*
  141. ** The sqlite3_mutex_alloc() routine allocates a new
  142. ** mutex and returns a pointer to it. If it returns NULL
  143. ** that means that a mutex could not be allocated. SQLite
  144. ** will unwind its stack and return an error. The argument
  145. ** to sqlite3_mutex_alloc() is one of these integer constants:
  146. **
  147. ** <ul>
  148. ** <li> SQLITE_MUTEX_FAST
  149. ** <li> SQLITE_MUTEX_RECURSIVE
  150. ** <li> SQLITE_MUTEX_STATIC_MASTER
  151. ** <li> SQLITE_MUTEX_STATIC_MEM
  152. ** <li> SQLITE_MUTEX_STATIC_MEM2
  153. ** <li> SQLITE_MUTEX_STATIC_PRNG
  154. ** <li> SQLITE_MUTEX_STATIC_LRU
  155. ** <li> SQLITE_MUTEX_STATIC_PMEM
  156. ** </ul>
  157. **
  158. ** The first two constants cause sqlite3_mutex_alloc() to create
  159. ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  160. ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  161. ** The mutex implementation does not need to make a distinction
  162. ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  163. ** not want to. But SQLite will only request a recursive mutex in
  164. ** cases where it really needs one. If a faster non-recursive mutex
  165. ** implementation is available on the host platform, the mutex subsystem
  166. ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  167. **
  168. ** The other allowed parameters to sqlite3_mutex_alloc() each return
  169. ** a pointer to a static preexisting mutex. Six static mutexes are
  170. ** used by the current version of SQLite. Future versions of SQLite
  171. ** may add additional static mutexes. Static mutexes are for internal
  172. ** use by SQLite only. Applications that use SQLite mutexes should
  173. ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  174. ** SQLITE_MUTEX_RECURSIVE.
  175. **
  176. ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  177. ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  178. ** returns a different mutex on every call. But for the static
  179. ** mutex types, the same mutex is returned on every call that has
  180. ** the same type number.
  181. */
  182. static sqlite3_mutex *winMutexAlloc(int iType){
  183. sqlite3_mutex *p;
  184. switch( iType ){
  185. case SQLITE_MUTEX_FAST:
  186. case SQLITE_MUTEX_RECURSIVE: {
  187. p = sqlite3MallocZero( sizeof(*p) );
  188. if( p ){
  189. #ifdef SQLITE_DEBUG
  190. p->id = iType;
  191. #endif
  192. #if SQLITE_OS_WINRT
  193. InitializeCriticalSectionEx(&p->mutex, 0, 0);
  194. #else
  195. InitializeCriticalSection(&p->mutex);
  196. #endif
  197. }
  198. break;
  199. }
  200. default: {
  201. assert( winMutex_isInit==1 );
  202. assert( iType-2 >= 0 );
  203. assert( iType-2 < ArraySize(winMutex_staticMutexes) );
  204. p = &winMutex_staticMutexes[iType-2];
  205. #ifdef SQLITE_DEBUG
  206. p->id = iType;
  207. #endif
  208. break;
  209. }
  210. }
  211. return p;
  212. }
  213. /*
  214. ** This routine deallocates a previously
  215. ** allocated mutex. SQLite is careful to deallocate every
  216. ** mutex that it allocates.
  217. */
  218. static void winMutexFree(sqlite3_mutex *p){
  219. assert( p );
  220. assert( p->nRef==0 && p->owner==0 );
  221. assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  222. DeleteCriticalSection(&p->mutex);
  223. sqlite3_free(p);
  224. }
  225. /*
  226. ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  227. ** to enter a mutex. If another thread is already within the mutex,
  228. ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  229. ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
  230. ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
  231. ** be entered multiple times by the same thread. In such cases the,
  232. ** mutex must be exited an equal number of times before another thread
  233. ** can enter. If the same thread tries to enter any other kind of mutex
  234. ** more than once, the behavior is undefined.
  235. */
  236. static void winMutexEnter(sqlite3_mutex *p){
  237. #ifdef SQLITE_DEBUG
  238. DWORD tid = GetCurrentThreadId();
  239. assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
  240. #endif
  241. EnterCriticalSection(&p->mutex);
  242. #ifdef SQLITE_DEBUG
  243. assert( p->nRef>0 || p->owner==0 );
  244. p->owner = tid;
  245. p->nRef++;
  246. if( p->trace ){
  247. printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  248. }
  249. #endif
  250. }
  251. static int winMutexTry(sqlite3_mutex *p){
  252. #ifndef NDEBUG
  253. DWORD tid = GetCurrentThreadId();
  254. #endif
  255. int rc = SQLITE_BUSY;
  256. assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
  257. /*
  258. ** The sqlite3_mutex_try() routine is very rarely used, and when it
  259. ** is used it is merely an optimization. So it is OK for it to always
  260. ** fail.
  261. **
  262. ** The TryEnterCriticalSection() interface is only available on WinNT.
  263. ** And some windows compilers complain if you try to use it without
  264. ** first doing some #defines that prevent SQLite from building on Win98.
  265. ** For that reason, we will omit this optimization for now. See
  266. ** ticket #2685.
  267. */
  268. #if 0
  269. if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
  270. p->owner = tid;
  271. p->nRef++;
  272. rc = SQLITE_OK;
  273. }
  274. #else
  275. UNUSED_PARAMETER(p);
  276. #endif
  277. #ifdef SQLITE_DEBUG
  278. if( rc==SQLITE_OK && p->trace ){
  279. printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  280. }
  281. #endif
  282. return rc;
  283. }
  284. /*
  285. ** The sqlite3_mutex_leave() routine exits a mutex that was
  286. ** previously entered by the same thread. The behavior
  287. ** is undefined if the mutex is not currently entered or
  288. ** is not currently allocated. SQLite will never do either.
  289. */
  290. static void winMutexLeave(sqlite3_mutex *p){
  291. #ifndef NDEBUG
  292. DWORD tid = GetCurrentThreadId();
  293. assert( p->nRef>0 );
  294. assert( p->owner==tid );
  295. p->nRef--;
  296. if( p->nRef==0 ) p->owner = 0;
  297. assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
  298. #endif
  299. LeaveCriticalSection(&p->mutex);
  300. #ifdef SQLITE_DEBUG
  301. if( p->trace ){
  302. printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  303. }
  304. #endif
  305. }
  306. sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
  307. static const sqlite3_mutex_methods sMutex = {
  308. winMutexInit,
  309. winMutexEnd,
  310. winMutexAlloc,
  311. winMutexFree,
  312. winMutexEnter,
  313. winMutexTry,
  314. winMutexLeave,
  315. #ifdef SQLITE_DEBUG
  316. winMutexHeld,
  317. winMutexNotheld
  318. #else
  319. 0,
  320. 0
  321. #endif
  322. };
  323. return &sMutex;
  324. }
  325. #endif /* SQLITE_MUTEX_W32 */