xref: /sqlite-3.40.0/src/mutex.c (revision 7aba8fde)
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   if( !sqlite3GlobalConfig.bCoreMutex ) return 0;
81 #ifndef SQLITE_OMIT_AUTOINIT
82   if( sqlite3_initialize() ) return 0;
83 #endif
84   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
85 }
86 
87 sqlite3_mutex *sqlite3MutexAlloc(int id){
88   if( !sqlite3GlobalConfig.bCoreMutex ){
89     return 0;
90   }
91   assert( GLOBAL(int, mutexIsInit) );
92   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
93 }
94 
95 /*
96 ** Free a dynamic mutex.
97 */
98 void sqlite3_mutex_free(sqlite3_mutex *p){
99   if( p ){
100     sqlite3GlobalConfig.mutex.xMutexFree(p);
101   }
102 }
103 
104 /*
105 ** Obtain the mutex p. If some other thread already has the mutex, block
106 ** until it can be obtained.
107 */
108 void sqlite3_mutex_enter(sqlite3_mutex *p){
109   if( p ){
110     sqlite3GlobalConfig.mutex.xMutexEnter(p);
111   }
112 }
113 
114 /*
115 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
116 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
117 */
118 int sqlite3_mutex_try(sqlite3_mutex *p){
119   int rc = SQLITE_OK;
120   if( p ){
121     return sqlite3GlobalConfig.mutex.xMutexTry(p);
122   }
123   return rc;
124 }
125 
126 /*
127 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
128 ** entered by the same thread.  The behavior is undefined if the mutex
129 ** is not currently entered. If a NULL pointer is passed as an argument
130 ** this function is a no-op.
131 */
132 void sqlite3_mutex_leave(sqlite3_mutex *p){
133   if( p ){
134     sqlite3GlobalConfig.mutex.xMutexLeave(p);
135   }
136 }
137 
138 #ifndef NDEBUG
139 /*
140 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
141 ** intended for use inside assert() statements.
142 */
143 int sqlite3_mutex_held(sqlite3_mutex *p){
144   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
145 }
146 int sqlite3_mutex_notheld(sqlite3_mutex *p){
147   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
148 }
149 #endif
150 
151 #endif /* SQLITE_MUTEX_OMIT */
152