OLD | NEW |
1 /* | 1 /* |
2 ** 2007 August 14 | 2 ** 2007 August 14 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains the C functions that implement mutexes. | 12 ** This file contains the C functions that implement mutexes. |
13 ** | 13 ** |
14 ** This file contains code that is common across all mutex implementations. | 14 ** This file contains code that is common across all mutex implementations. |
15 */ | 15 */ |
16 #include "sqliteInt.h" | 16 #include "sqliteInt.h" |
17 | 17 |
18 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT) | 18 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT) |
19 /* | 19 /* |
20 ** For debugging purposes, record when the mutex subsystem is initialized | 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 | 21 ** and uninitialized so that we can assert() if there is an attempt to |
22 ** allocate a mutex while the system is uninitialized. | 22 ** allocate a mutex while the system is uninitialized. |
23 */ | 23 */ |
24 static SQLITE_WSD int mutexIsInit = 0; | 24 static SQLITE_WSD int mutexIsInit = 0; |
25 #endif /* SQLITE_DEBUG */ | 25 #endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */ |
26 | 26 |
27 | 27 |
28 #ifndef SQLITE_MUTEX_OMIT | 28 #ifndef SQLITE_MUTEX_OMIT |
29 /* | 29 /* |
30 ** Initialize the mutex system. | 30 ** Initialize the mutex system. |
31 */ | 31 */ |
32 int sqlite3MutexInit(void){ | 32 int sqlite3MutexInit(void){ |
33 int rc = SQLITE_OK; | 33 int rc = SQLITE_OK; |
34 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){ | 34 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){ |
35 /* If the xMutexAlloc method has not been set, then the user did not | 35 /* If the xMutexAlloc method has not been set, then the user did not |
36 ** install a mutex implementation via sqlite3_config() prior to | 36 ** install a mutex implementation via sqlite3_config() prior to |
37 ** sqlite3_initialize() being called. This block copies pointers to | 37 ** sqlite3_initialize() being called. This block copies pointers to |
38 ** the default implementation into the sqlite3GlobalConfig structure. | 38 ** the default implementation into the sqlite3GlobalConfig structure. |
39 */ | 39 */ |
40 sqlite3_mutex_methods const *pFrom; | 40 sqlite3_mutex_methods const *pFrom; |
41 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; | 41 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; |
42 | 42 |
43 if( sqlite3GlobalConfig.bCoreMutex ){ | 43 if( sqlite3GlobalConfig.bCoreMutex ){ |
44 pFrom = sqlite3DefaultMutex(); | 44 pFrom = sqlite3DefaultMutex(); |
45 }else{ | 45 }else{ |
46 pFrom = sqlite3NoopMutex(); | 46 pFrom = sqlite3NoopMutex(); |
47 } | 47 } |
48 memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc)); | 48 pTo->xMutexInit = pFrom->xMutexInit; |
49 memcpy(&pTo->xMutexFree, &pFrom->xMutexFree, | 49 pTo->xMutexEnd = pFrom->xMutexEnd; |
50 sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree)); | 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(); |
51 pTo->xMutexAlloc = pFrom->xMutexAlloc; | 57 pTo->xMutexAlloc = pFrom->xMutexAlloc; |
52 } | 58 } |
| 59 assert( sqlite3GlobalConfig.mutex.xMutexInit ); |
53 rc = sqlite3GlobalConfig.mutex.xMutexInit(); | 60 rc = sqlite3GlobalConfig.mutex.xMutexInit(); |
54 | 61 |
55 #ifdef SQLITE_DEBUG | 62 #ifdef SQLITE_DEBUG |
56 GLOBAL(int, mutexIsInit) = 1; | 63 GLOBAL(int, mutexIsInit) = 1; |
57 #endif | 64 #endif |
58 | 65 |
59 return rc; | 66 return rc; |
60 } | 67 } |
61 | 68 |
62 /* | 69 /* |
(...skipping 12 matching lines...) Expand all Loading... |
75 | 82 |
76 return rc; | 83 return rc; |
77 } | 84 } |
78 | 85 |
79 /* | 86 /* |
80 ** Retrieve a pointer to a static mutex or allocate a new dynamic one. | 87 ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
81 */ | 88 */ |
82 sqlite3_mutex *sqlite3_mutex_alloc(int id){ | 89 sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
83 #ifndef SQLITE_OMIT_AUTOINIT | 90 #ifndef SQLITE_OMIT_AUTOINIT |
84 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; | 91 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; |
| 92 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; |
85 #endif | 93 #endif |
| 94 assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); |
86 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); | 95 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
87 } | 96 } |
88 | 97 |
89 sqlite3_mutex *sqlite3MutexAlloc(int id){ | 98 sqlite3_mutex *sqlite3MutexAlloc(int id){ |
90 if( !sqlite3GlobalConfig.bCoreMutex ){ | 99 if( !sqlite3GlobalConfig.bCoreMutex ){ |
91 return 0; | 100 return 0; |
92 } | 101 } |
93 assert( GLOBAL(int, mutexIsInit) ); | 102 assert( GLOBAL(int, mutexIsInit) ); |
| 103 assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); |
94 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); | 104 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
95 } | 105 } |
96 | 106 |
97 /* | 107 /* |
98 ** Free a dynamic mutex. | 108 ** Free a dynamic mutex. |
99 */ | 109 */ |
100 void sqlite3_mutex_free(sqlite3_mutex *p){ | 110 void sqlite3_mutex_free(sqlite3_mutex *p){ |
101 if( p ){ | 111 if( p ){ |
| 112 assert( sqlite3GlobalConfig.mutex.xMutexFree ); |
102 sqlite3GlobalConfig.mutex.xMutexFree(p); | 113 sqlite3GlobalConfig.mutex.xMutexFree(p); |
103 } | 114 } |
104 } | 115 } |
105 | 116 |
106 /* | 117 /* |
107 ** Obtain the mutex p. If some other thread already has the mutex, block | 118 ** Obtain the mutex p. If some other thread already has the mutex, block |
108 ** until it can be obtained. | 119 ** until it can be obtained. |
109 */ | 120 */ |
110 void sqlite3_mutex_enter(sqlite3_mutex *p){ | 121 void sqlite3_mutex_enter(sqlite3_mutex *p){ |
111 if( p ){ | 122 if( p ){ |
| 123 assert( sqlite3GlobalConfig.mutex.xMutexEnter ); |
112 sqlite3GlobalConfig.mutex.xMutexEnter(p); | 124 sqlite3GlobalConfig.mutex.xMutexEnter(p); |
113 } | 125 } |
114 } | 126 } |
115 | 127 |
116 /* | 128 /* |
117 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another | 129 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another |
118 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. | 130 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. |
119 */ | 131 */ |
120 int sqlite3_mutex_try(sqlite3_mutex *p){ | 132 int sqlite3_mutex_try(sqlite3_mutex *p){ |
121 int rc = SQLITE_OK; | 133 int rc = SQLITE_OK; |
122 if( p ){ | 134 if( p ){ |
| 135 assert( sqlite3GlobalConfig.mutex.xMutexTry ); |
123 return sqlite3GlobalConfig.mutex.xMutexTry(p); | 136 return sqlite3GlobalConfig.mutex.xMutexTry(p); |
124 } | 137 } |
125 return rc; | 138 return rc; |
126 } | 139 } |
127 | 140 |
128 /* | 141 /* |
129 ** The sqlite3_mutex_leave() routine exits a mutex that was previously | 142 ** The sqlite3_mutex_leave() routine exits a mutex that was previously |
130 ** entered by the same thread. The behavior is undefined if the mutex | 143 ** entered by the same thread. The behavior is undefined if the mutex |
131 ** is not currently entered. If a NULL pointer is passed as an argument | 144 ** is not currently entered. If a NULL pointer is passed as an argument |
132 ** this function is a no-op. | 145 ** this function is a no-op. |
133 */ | 146 */ |
134 void sqlite3_mutex_leave(sqlite3_mutex *p){ | 147 void sqlite3_mutex_leave(sqlite3_mutex *p){ |
135 if( p ){ | 148 if( p ){ |
| 149 assert( sqlite3GlobalConfig.mutex.xMutexLeave ); |
136 sqlite3GlobalConfig.mutex.xMutexLeave(p); | 150 sqlite3GlobalConfig.mutex.xMutexLeave(p); |
137 } | 151 } |
138 } | 152 } |
139 | 153 |
140 #ifndef NDEBUG | 154 #ifndef NDEBUG |
141 /* | 155 /* |
142 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are | 156 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
143 ** intended for use inside assert() statements. | 157 ** intended for use inside assert() statements. |
144 */ | 158 */ |
145 int sqlite3_mutex_held(sqlite3_mutex *p){ | 159 int sqlite3_mutex_held(sqlite3_mutex *p){ |
| 160 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld ); |
146 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); | 161 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); |
147 } | 162 } |
148 int sqlite3_mutex_notheld(sqlite3_mutex *p){ | 163 int sqlite3_mutex_notheld(sqlite3_mutex *p){ |
| 164 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld ); |
149 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); | 165 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); |
150 } | 166 } |
151 #endif | 167 #endif |
152 | 168 |
153 #endif /* !defined(SQLITE_MUTEX_OMIT) */ | 169 #endif /* !defined(SQLITE_MUTEX_OMIT) */ |
OLD | NEW |