xref: /sqlite-3.40.0/src/mutex.c (revision 3f09beda)
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.
13 **
14 ** This file contains code that is common across all mutex implementations.
15 */
16 #include "sqliteInt.h"
17 
18 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
19 /*
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.
23 */
24 static SQLITE_WSD int mutexIsInit = 0;
25 #endif /* SQLITE_DEBUG */
26 
27 
28 #ifndef SQLITE_MUTEX_OMIT
29 /*
30 ** Initialize the mutex system.
31 */
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.
39     */
40     sqlite3_mutex_methods const *pFrom;
41     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
42 
43     if( sqlite3GlobalConfig.bCoreMutex ){
44       pFrom = sqlite3DefaultMutex();
45     }else{
46       pFrom = sqlite3NoopMutex();
47     }
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     pTo->xMutexAlloc = pFrom->xMutexAlloc;
57   }
58   rc = sqlite3GlobalConfig.mutex.xMutexInit();
59 
60 #ifdef SQLITE_DEBUG
61   GLOBAL(int, mutexIsInit) = 1;
62 #endif
63 
64   return rc;
65 }
66 
67 /*
68 ** Shutdown the mutex system. This call frees resources allocated by
69 ** sqlite3MutexInit().
70 */
71 int sqlite3MutexEnd(void){
72   int rc = SQLITE_OK;
73   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
74     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
75   }
76 
77 #ifdef SQLITE_DEBUG
78   GLOBAL(int, mutexIsInit) = 0;
79 #endif
80 
81   return rc;
82 }
83 
84 /*
85 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
86 */
87 sqlite3_mutex *sqlite3_mutex_alloc(int id){
88 #ifndef SQLITE_OMIT_AUTOINIT
89   if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
90   if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
91 #endif
92   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
93 }
94 
95 sqlite3_mutex *sqlite3MutexAlloc(int id){
96   if( !sqlite3GlobalConfig.bCoreMutex ){
97     return 0;
98   }
99   assert( GLOBAL(int, mutexIsInit) );
100   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
101 }
102 
103 /*
104 ** Free a dynamic mutex.
105 */
106 void sqlite3_mutex_free(sqlite3_mutex *p){
107   if( p ){
108     sqlite3GlobalConfig.mutex.xMutexFree(p);
109   }
110 }
111 
112 /*
113 ** Obtain the mutex p. If some other thread already has the mutex, block
114 ** until it can be obtained.
115 */
116 void sqlite3_mutex_enter(sqlite3_mutex *p){
117   if( p ){
118     sqlite3GlobalConfig.mutex.xMutexEnter(p);
119   }
120 }
121 
122 /*
123 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
124 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
125 */
126 int sqlite3_mutex_try(sqlite3_mutex *p){
127   int rc = SQLITE_OK;
128   if( p ){
129     return sqlite3GlobalConfig.mutex.xMutexTry(p);
130   }
131   return rc;
132 }
133 
134 /*
135 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
136 ** entered by the same thread.  The behavior is undefined if the mutex
137 ** is not currently entered. If a NULL pointer is passed as an argument
138 ** this function is a no-op.
139 */
140 void sqlite3_mutex_leave(sqlite3_mutex *p){
141   if( p ){
142     sqlite3GlobalConfig.mutex.xMutexLeave(p);
143   }
144 }
145 
146 #ifndef NDEBUG
147 /*
148 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
149 ** intended for use inside assert() statements.
150 */
151 int sqlite3_mutex_held(sqlite3_mutex *p){
152   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
153 }
154 int sqlite3_mutex_notheld(sqlite3_mutex *p){
155   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
156 }
157 #endif
158 
159 #endif /* !defined(SQLITE_MUTEX_OMIT) */
160