xref: /sqlite-3.40.0/src/mutex.c (revision 6695f47e)
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.bCoreMutex ){
35     if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
36       /* If the xMutexAlloc method has not been set, then the user did not
37       ** install a mutex implementation via sqlite3_config() prior to
38       ** sqlite3_initialize() being called. This block copies pointers to
39       ** the default implementation into the sqlite3GlobalConfig structure.
40       */
41       sqlite3_mutex_methods *pFrom = sqlite3DefaultMutex();
42       sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
43 
44       memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
45       memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
46              sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
47       pTo->xMutexAlloc = pFrom->xMutexAlloc;
48     }
49     rc = sqlite3GlobalConfig.mutex.xMutexInit();
50   }
51 
52 #ifdef SQLITE_DEBUG
53   GLOBAL(int, mutexIsInit) = 1;
54 #endif
55 
56   return rc;
57 }
58 
59 /*
60 ** Shutdown the mutex system. This call frees resources allocated by
61 ** sqlite3MutexInit().
62 */
63 int sqlite3MutexEnd(void){
64   int rc = SQLITE_OK;
65   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
66     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
67   }
68 
69 #ifdef SQLITE_DEBUG
70   GLOBAL(int, mutexIsInit) = 0;
71 #endif
72 
73   return rc;
74 }
75 
76 /*
77 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
78 */
79 sqlite3_mutex *sqlite3_mutex_alloc(int id){
80 #ifndef SQLITE_OMIT_AUTOINIT
81   if( sqlite3_initialize() ) return 0;
82 #endif
83   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
84 }
85 
86 sqlite3_mutex *sqlite3MutexAlloc(int id){
87   if( !sqlite3GlobalConfig.bCoreMutex ){
88     return 0;
89   }
90   assert( GLOBAL(int, mutexIsInit) );
91   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
92 }
93 
94 /*
95 ** Free a dynamic mutex.
96 */
97 void sqlite3_mutex_free(sqlite3_mutex *p){
98   if( p ){
99     sqlite3GlobalConfig.mutex.xMutexFree(p);
100   }
101 }
102 
103 /*
104 ** Obtain the mutex p. If some other thread already has the mutex, block
105 ** until it can be obtained.
106 */
107 void sqlite3_mutex_enter(sqlite3_mutex *p){
108   if( p ){
109     sqlite3GlobalConfig.mutex.xMutexEnter(p);
110   }
111 }
112 
113 /*
114 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
115 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
116 */
117 int sqlite3_mutex_try(sqlite3_mutex *p){
118   int rc = SQLITE_OK;
119   if( p ){
120     return sqlite3GlobalConfig.mutex.xMutexTry(p);
121   }
122   return rc;
123 }
124 
125 /*
126 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
127 ** entered by the same thread.  The behavior is undefined if the mutex
128 ** is not currently entered. If a NULL pointer is passed as an argument
129 ** this function is a no-op.
130 */
131 void sqlite3_mutex_leave(sqlite3_mutex *p){
132   if( p ){
133     sqlite3GlobalConfig.mutex.xMutexLeave(p);
134   }
135 }
136 
137 #ifndef NDEBUG
138 /*
139 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
140 ** intended for use inside assert() statements.
141 */
142 int sqlite3_mutex_held(sqlite3_mutex *p){
143   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
144 }
145 int sqlite3_mutex_notheld(sqlite3_mutex *p){
146   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
147 }
148 #endif
149 
150 #endif /* SQLITE_MUTEX_OMIT */
151