Omit unused batch-atomic-write code if SQLITE_ENABLE_BATCH_ATOMIC_WRITE is
[sqlite.git] / src / mutex.c
blob6f1bc9767db8e1a3ce002bcc10b77c72c9e6f51c
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.
11 *************************************************************************
12 ** This file contains the C functions that implement mutexes.
14 ** This file contains code that is common across all mutex implementations.
16 #include "sqliteInt.h"
18 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
20 ** For debugging purposes, record when the mutex subsystem is initialized
21 ** and uninitialized so that we can assert() if there is an attempt to
22 ** allocate a mutex while the system is uninitialized.
24 static SQLITE_WSD int mutexIsInit = 0;
25 #endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
28 #ifndef SQLITE_MUTEX_OMIT
30 ** Initialize the mutex system.
32 int sqlite3MutexInit(void){
33 int rc = SQLITE_OK;
34 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
35 /* If the xMutexAlloc method has not been set, then the user did not
36 ** install a mutex implementation via sqlite3_config() prior to
37 ** sqlite3_initialize() being called. This block copies pointers to
38 ** the default implementation into the sqlite3GlobalConfig structure.
40 sqlite3_mutex_methods const *pFrom;
41 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
43 if( sqlite3GlobalConfig.bCoreMutex ){
44 pFrom = sqlite3DefaultMutex();
45 }else{
46 pFrom = sqlite3NoopMutex();
48 pTo->xMutexInit = pFrom->xMutexInit;
49 pTo->xMutexEnd = pFrom->xMutexEnd;
50 pTo->xMutexFree = pFrom->xMutexFree;
51 pTo->xMutexEnter = pFrom->xMutexEnter;
52 pTo->xMutexTry = pFrom->xMutexTry;
53 pTo->xMutexLeave = pFrom->xMutexLeave;
54 pTo->xMutexHeld = pFrom->xMutexHeld;
55 pTo->xMutexNotheld = pFrom->xMutexNotheld;
56 sqlite3MemoryBarrier();
57 pTo->xMutexAlloc = pFrom->xMutexAlloc;
59 assert( sqlite3GlobalConfig.mutex.xMutexInit );
60 rc = sqlite3GlobalConfig.mutex.xMutexInit();
62 #ifdef SQLITE_DEBUG
63 GLOBAL(int, mutexIsInit) = 1;
64 #endif
66 return rc;
70 ** Shutdown the mutex system. This call frees resources allocated by
71 ** sqlite3MutexInit().
73 int sqlite3MutexEnd(void){
74 int rc = SQLITE_OK;
75 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
76 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
79 #ifdef SQLITE_DEBUG
80 GLOBAL(int, mutexIsInit) = 0;
81 #endif
83 return rc;
87 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
89 sqlite3_mutex *sqlite3_mutex_alloc(int id){
90 #ifndef SQLITE_OMIT_AUTOINIT
91 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
92 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
93 #endif
94 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
95 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
98 sqlite3_mutex *sqlite3MutexAlloc(int id){
99 if( !sqlite3GlobalConfig.bCoreMutex ){
100 return 0;
102 assert( GLOBAL(int, mutexIsInit) );
103 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
104 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
108 ** Free a dynamic mutex.
110 void sqlite3_mutex_free(sqlite3_mutex *p){
111 if( p ){
112 assert( sqlite3GlobalConfig.mutex.xMutexFree );
113 sqlite3GlobalConfig.mutex.xMutexFree(p);
118 ** Obtain the mutex p. If some other thread already has the mutex, block
119 ** until it can be obtained.
121 void sqlite3_mutex_enter(sqlite3_mutex *p){
122 if( p ){
123 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
124 sqlite3GlobalConfig.mutex.xMutexEnter(p);
129 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
130 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
132 int sqlite3_mutex_try(sqlite3_mutex *p){
133 int rc = SQLITE_OK;
134 if( p ){
135 assert( sqlite3GlobalConfig.mutex.xMutexTry );
136 return sqlite3GlobalConfig.mutex.xMutexTry(p);
138 return rc;
142 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
143 ** entered by the same thread. The behavior is undefined if the mutex
144 ** is not currently entered. If a NULL pointer is passed as an argument
145 ** this function is a no-op.
147 void sqlite3_mutex_leave(sqlite3_mutex *p){
148 if( p ){
149 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
150 sqlite3GlobalConfig.mutex.xMutexLeave(p);
154 #ifndef NDEBUG
156 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
157 ** intended for use inside assert() statements.
159 int sqlite3_mutex_held(sqlite3_mutex *p){
160 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
161 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
163 int sqlite3_mutex_notheld(sqlite3_mutex *p){
164 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
165 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
167 #endif
169 #endif /* !defined(SQLITE_MUTEX_OMIT) */