Artifact
5e2ea0e0490a3567dc08a014bcee748c0cea727f:
- File
src/mutex.c
— part of check-in
[79431c58]
at
2009-02-17 16:29:11
on branch trunk
— Allow sqlite3_shutdown() to be called by a process before sqlite3_initialize() is. Prior to this commit such a call could segfault. (CVS 6296)
(user:
danielk1977
size: 4171)
0000: 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
0010: 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 14.**.** The a
0020: 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
0030: 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
0040: 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
0050: 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
0060: 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
0070: 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
0080: 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
0090: 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
00a0: 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
00b0: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
00c0: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
00d0: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
00e0: 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
00f0: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
0100: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
0110: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
0120: 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
0180: 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
0190: 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
01a0: 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
01b0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ..**.** This fil
01c0: 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
01d0: 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 that is common a
01e0: 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 cross all mutex
01f0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e implementations.
0200: 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 ..**.** $Id: mut
0210: 65 78 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 ex.c,v 1.30 2009
0220: 2f 30 32 2f 31 37 20 31 36 3a 32 39 3a 31 31 20 /02/17 16:29:11
0230: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
0240: 24 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 73 $.*/.#include "s
0250: 71 6c 69 74 65 49 6e 74 2e 68 22 0a 0a 23 69 66 qliteInt.h"..#if
0260: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 ndef SQLITE_MUTE
0270: 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 X_OMIT./*.** Ini
0280: 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 tialize the mute
0290: 78 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 69 6e 74 x system..*/.int
02a0: 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 sqlite3MutexIni
02b0: 74 28 76 6f 69 64 29 7b 20 0a 20 20 69 6e 74 20 t(void){ . int
02c0: 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
02d0: 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
02e0: 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d balConfig.bCoreM
02f0: 75 74 65 78 20 29 7b 0a 20 20 20 20 69 66 28 20 utex ){. if(
0300: 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f !sqlite3GlobalCo
0310: 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
0320: 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 xAlloc ){.
0330: 2f 2a 20 49 66 20 74 68 65 20 78 4d 75 74 65 78 /* If the xMutex
0340: 41 6c 6c 6f 63 20 6d 65 74 68 6f 64 20 68 61 73 Alloc method has
0350: 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 74 not been set, t
0360: 68 65 6e 20 74 68 65 20 75 73 65 72 20 64 69 64 hen the user did
0370: 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e not. ** in
0380: 73 74 61 6c 6c 20 61 20 6d 75 74 65 78 20 69 6d stall a mutex im
0390: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 76 69 61 plementation via
03a0: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
03b0: 29 20 70 72 69 6f 72 20 74 6f 20 0a 20 20 20 20 ) prior to .
03c0: 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 ** sqlite3_ini
03d0: 74 69 61 6c 69 7a 65 28 29 20 62 65 69 6e 67 20 tialize() being
03e0: 63 61 6c 6c 65 64 2e 20 54 68 69 73 20 62 6c 6f called. This blo
03f0: 63 6b 20 63 6f 70 69 65 73 20 70 6f 69 6e 74 65 ck copies pointe
0400: 72 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 rs to. ** t
0410: 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 he default imple
0420: 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 mentation into t
0430: 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c he sqlite3Global
0440: 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75 72 65 Config structure
0450: 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
0460: 20 2a 2a 20 54 68 65 20 64 61 6e 67 65 72 20 69 ** The danger i
0470: 73 20 74 68 61 74 20 61 6c 74 68 6f 75 67 68 20 s that although
0480: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
0490: 20 69 73 20 6e 6f 74 20 61 20 74 68 72 65 61 64 is not a thread
04a0: 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 41 50 safe. ** AP
04b0: 49 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 I, sqlite3_initi
04c0: 61 6c 69 7a 65 28 29 20 69 73 2c 20 61 6e 64 20 alize() is, and
04d0: 73 6f 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 so multiple thre
04e0: 61 64 73 20 6d 61 79 20 62 65 0a 20 20 20 20 20 ads may be.
04f0: 20 2a 2a 20 61 74 74 65 6d 70 74 69 6e 67 20 74 ** attempting t
0500: 6f 20 72 75 6e 20 74 68 69 73 20 66 75 6e 63 74 o run this funct
0510: 69 6f 6e 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 ion simultaneous
0520: 6c 79 2e 20 54 6f 20 67 75 61 72 64 20 77 72 69 ly. To guard wri
0530: 74 65 0a 20 20 20 20 20 20 2a 2a 20 61 63 63 65 te. ** acce
0540: 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 ss to the sqlite
0550: 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 3GlobalConfig st
0560: 72 75 63 74 75 72 65 2c 20 74 68 65 20 27 4d 41 ructure, the 'MA
0570: 53 54 45 52 27 20 73 74 61 74 69 63 20 6d 75 74 STER' static mut
0580: 65 78 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6f ex. ** is o
0590: 62 74 61 69 6e 65 64 20 62 65 66 6f 72 65 20 6d btained before m
05a0: 6f 64 69 66 79 69 6e 67 20 69 74 2e 0a 20 20 20 odifying it..
05b0: 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 */. sqli
05c0: 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
05d0: 73 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 65 s *p = sqlite3De
05e0: 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20 faultMutex();.
05f0: 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
0600: 78 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a x *pMaster = 0;.
0610: 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20 70 2d . rc = p-
0620: 3e 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 >xMutexInit();.
0630: 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
0640: 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
0650: 20 20 70 4d 61 73 74 65 72 20 3d 20 70 2d 3e 78 pMaster = p->x
0660: 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
0670: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
0680: 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 20 20 ASTER);.
0690: 61 73 73 65 72 74 28 70 4d 61 73 74 65 72 29 3b assert(pMaster);
06a0: 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 4d 75 74 . p->xMut
06b0: 65 78 45 6e 74 65 72 28 70 4d 61 73 74 65 72 29 exEnter(pMaster)
06c0: 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
06d0: 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
06e0: 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
06f0: 65 78 41 6c 6c 6f 63 3d 3d 30 20 0a 20 20 20 20 exAlloc==0 .
0700: 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 || sqli
0710: 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
0720: 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f mutex.xMutexAllo
0730: 63 3d 3d 70 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f c==p->xMutexAllo
0740: 63 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 c. );.
0750: 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 if( !sqlite
0760: 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
0770: 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 tex.xMutexAlloc
0780: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
0790: 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
07a0: 2e 6d 75 74 65 78 20 3d 20 2a 70 3b 0a 20 20 20 .mutex = *p;.
07b0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
07c0: 2d 3e 78 4d 75 74 65 78 4c 65 61 76 65 28 70 4d ->xMutexLeave(pM
07d0: 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a aster);. }.
07e0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
07f0: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f rc = sqlite3Glo
0800: 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
0810: 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 xMutexInit();.
0820: 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 }. }.. retur
0830: 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
0840: 68 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 hutdown the mute
0850: 78 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 x system. This c
0860: 61 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 all frees resour
0870: 63 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ces allocated by
0880: 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 .** sqlite3Mutex
0890: 49 6e 69 74 28 29 2e 0a 2a 2f 0a 69 6e 74 20 73 Init()..*/.int s
08a0: 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 qlite3MutexEnd(v
08b0: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d oid){. int rc =
08c0: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
08d0: 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
08e0: 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
08f0: 65 78 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 exEnd ){. rc
0900: 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
0910: 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
0920: 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a 20 20 72 exEnd();. }. r
0930: 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
0940: 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 6f ** Retrieve a po
0950: 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 inter to a stati
0960: 63 20 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 c mutex or alloc
0970: 61 74 65 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 ate a new dynami
0980: 63 20 6f 6e 65 2e 0a 2a 2f 0a 73 71 6c 69 74 65 c one..*/.sqlite
0990: 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 3_mutex *sqlite3
09a0: 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 _mutex_alloc(int
09b0: 20 69 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 id){.#ifndef SQ
09c0: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
09d0: 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 IT. if( sqlite3
09e0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 _initialize() )
09f0: 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 return 0;.#endif
0a00: 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
0a10: 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
0a20: 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 tex.xMutexAlloc(
0a30: 69 64 29 3b 0a 7d 0a 0a 73 71 6c 69 74 65 33 5f id);.}..sqlite3_
0a40: 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d 75 mutex *sqlite3Mu
0a50: 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 texAlloc(int id)
0a60: 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 {. if( !sqlite3
0a70: 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f GlobalConfig.bCo
0a80: 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 72 reMutex ){. r
0a90: 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 eturn 0;. }. r
0aa0: 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
0ab0: 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
0ac0: 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b xMutexAlloc(id);
0ad0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
0ae0: 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a dynamic mutex..
0af0: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f */.void sqlite3_
0b00: 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 mutex_free(sqlit
0b10: 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
0b20: 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c if( p ){. sql
0b30: 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
0b40: 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 46 72 65 .mutex.xMutexFre
0b50: 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a e(p);. }.}../*.
0b60: 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 ** Obtain the mu
0b70: 74 65 78 20 70 2e 20 49 66 20 73 6f 6d 65 20 6f tex p. If some o
0b80: 74 68 65 72 20 74 68 72 65 61 64 20 61 6c 72 65 ther thread alre
0b90: 61 64 79 20 68 61 73 20 74 68 65 20 6d 75 74 65 ady has the mute
0ba0: 78 2c 20 62 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 x, block.** unti
0bb0: 6c 20 69 74 20 63 61 6e 20 62 65 20 6f 62 74 61 l it can be obta
0bc0: 69 6e 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ined..*/.void sq
0bd0: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
0be0: 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 r(sqlite3_mutex
0bf0: 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a *p){. if( p ){.
0c00: 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
0c10: 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
0c20: 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0a 20 20 utexEnter(p);.
0c30: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 }.}../*.** Obtai
0c40: 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 n the mutex p. I
0c50: 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 f successful, re
0c60: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 turn SQLITE_OK.
0c70: 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e Otherwise, if an
0c80: 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 20 other.** thread
0c90: 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 20 holds the mutex
0ca0: 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 and it cannot be
0cb0: 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 74 75 72 obtained, retur
0cc0: 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a n SQLITE_BUSY..*
0cd0: 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 /.int sqlite3_mu
0ce0: 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f tex_try(sqlite3_
0cf0: 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 mutex *p){. int
0d00: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
0d10: 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
0d20: 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
0d30: 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
0d40: 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b 0a 20 .xMutexTry(p);.
0d50: 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
0d60: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
0d70: 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
0d80: 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
0d90: 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
0da0: 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 s previously.**
0db0: 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
0dc0: 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
0dd0: 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
0de0: 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 efined if the mu
0df0: 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 tex .** is not c
0e00: 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
0e10: 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e . If a NULL poin
0e20: 74 65 72 20 69 73 20 70 61 73 73 65 64 20 61 73 ter is passed as
0e30: 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 an argument.**
0e40: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
0e50: 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 76 6f 69 a no-op..*/.voi
0e60: 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
0e70: 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 leave(sqlite3_mu
0e80: 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 tex *p){. if( p
0e90: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
0ea0: 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
0eb0: 78 2e 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 x.xMutexLeave(p)
0ec0: 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 ;. }.}..#ifndef
0ed0: 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 NDEBUG./*.** Th
0ee0: 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
0ef0: 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
0f00: 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
0f10: 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
0f20: 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
0f30: 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
0f40: 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
0f50: 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 /.int sqlite3_mu
0f60: 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 tex_held(sqlite3
0f70: 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
0f80: 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c turn p==0 || sql
0f90: 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
0fa0: 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 48 65 6c .mutex.xMutexHel
0fb0: 64 28 70 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 d(p);.}.int sqli
0fc0: 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
0fd0: 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
0fe0: 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d *p){. return p=
0ff0: 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f =0 || sqlite3Glo
1000: 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
1010: 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 xMutexNotheld(p)
1020: 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 ;.}.#endif..#end
1030: 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1040: 54 5f 4d 55 54 45 58 20 2a 2f 0a T_MUTEX */.