blob: 402757fdf7e863a3ac74088c31b7fc8d3adc2e3e [file] [log] [blame]
drh437b9012007-08-28 16:34:42 +00001/*
2** 2007 August 28
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 for pthreads
drh437b9012007-08-28 16:34:42 +000013*/
14#include "sqliteInt.h"
15
16/*
17** The code in this file is only used if we are compiling threadsafe
18** under unix with pthreads.
19**
20** Note that this implementation requires a version of pthreads that
21** supports recursive mutexes.
22*/
23#ifdef SQLITE_MUTEX_PTHREADS
24
25#include <pthread.h>
26
drhed05efb2007-11-28 00:51:34 +000027
drh437b9012007-08-28 16:34:42 +000028/*
29** Each recursive mutex is an instance of the following structure.
30*/
31struct sqlite3_mutex {
32 pthread_mutex_t mutex; /* Mutex controlling the lock */
33 int id; /* Mutex type */
34 int nRef; /* Number of entrances */
35 pthread_t owner; /* Thread that is within this mutex */
drhd0679ed2007-08-28 22:24:34 +000036#ifdef SQLITE_DEBUG
37 int trace; /* True to trace changes */
38#endif
drh437b9012007-08-28 16:34:42 +000039};
rse28f667f2008-03-29 12:47:27 +000040#ifdef SQLITE_DEBUG
41#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
42#else
43#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 }
44#endif
drh437b9012007-08-28 16:34:42 +000045
46/*
danielk19776d2ab0e2008-06-17 17:21:18 +000047** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
48** intended for use only inside assert() statements. On some platforms,
49** there might be race conditions that can cause these routines to
50** deliver incorrect results. In particular, if pthread_equal() is
51** not an atomic operation, then these routines might delivery
52** incorrect results. On most platforms, pthread_equal() is a
53** comparison of two integers and is therefore atomic. But we are
54** told that HPUX is not such a platform. If so, then these routines
55** will not always work correctly on HPUX.
56**
57** On those platforms where pthread_equal() is not atomic, SQLite
58** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
59** make sure no assert() statements are evaluated and hence these
60** routines are never called.
61*/
chw97185482008-11-17 08:05:31 +000062#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
danielk19776d2ab0e2008-06-17 17:21:18 +000063static int pthreadMutexHeld(sqlite3_mutex *p){
64 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
65}
66static int pthreadMutexNotheld(sqlite3_mutex *p){
67 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
68}
69#endif
70
71/*
drh40257ff2008-06-13 18:24:27 +000072** Initialize and deinitialize the mutex subsystem.
73*/
danielk19776d2ab0e2008-06-17 17:21:18 +000074static int pthreadMutexInit(void){ return SQLITE_OK; }
75static int pthreadMutexEnd(void){ return SQLITE_OK; }
drh40257ff2008-06-13 18:24:27 +000076
77/*
drh437b9012007-08-28 16:34:42 +000078** The sqlite3_mutex_alloc() routine allocates a new
79** mutex and returns a pointer to it. If it returns NULL
80** that means that a mutex could not be allocated. SQLite
81** will unwind its stack and return an error. The argument
82** to sqlite3_mutex_alloc() is one of these integer constants:
83**
84** <ul>
85** <li> SQLITE_MUTEX_FAST
86** <li> SQLITE_MUTEX_RECURSIVE
87** <li> SQLITE_MUTEX_STATIC_MASTER
88** <li> SQLITE_MUTEX_STATIC_MEM
89** <li> SQLITE_MUTEX_STATIC_MEM2
90** <li> SQLITE_MUTEX_STATIC_PRNG
91** <li> SQLITE_MUTEX_STATIC_LRU
shane7c7c3112009-08-17 15:31:23 +000092** <li> SQLITE_MUTEX_STATIC_LRU2
drh437b9012007-08-28 16:34:42 +000093** </ul>
94**
95** The first two constants cause sqlite3_mutex_alloc() to create
96** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
97** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
98** The mutex implementation does not need to make a distinction
99** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
100** not want to. But SQLite will only request a recursive mutex in
101** cases where it really needs one. If a faster non-recursive mutex
102** implementation is available on the host platform, the mutex subsystem
103** might return such a mutex in response to SQLITE_MUTEX_FAST.
104**
105** The other allowed parameters to sqlite3_mutex_alloc() each return
shane7c7c3112009-08-17 15:31:23 +0000106** a pointer to a static preexisting mutex. Six static mutexes are
drh437b9012007-08-28 16:34:42 +0000107** used by the current version of SQLite. Future versions of SQLite
108** may add additional static mutexes. Static mutexes are for internal
109** use by SQLite only. Applications that use SQLite mutexes should
110** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
111** SQLITE_MUTEX_RECURSIVE.
112**
113** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
114** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
115** returns a different mutex on every call. But for the static
116** mutex types, the same mutex is returned on every call that has
117** the same type number.
118*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000119static sqlite3_mutex *pthreadMutexAlloc(int iType){
drh437b9012007-08-28 16:34:42 +0000120 static sqlite3_mutex staticMutexes[] = {
rse28f667f2008-03-29 12:47:27 +0000121 SQLITE3_MUTEX_INITIALIZER,
122 SQLITE3_MUTEX_INITIALIZER,
123 SQLITE3_MUTEX_INITIALIZER,
124 SQLITE3_MUTEX_INITIALIZER,
125 SQLITE3_MUTEX_INITIALIZER,
126 SQLITE3_MUTEX_INITIALIZER
drh437b9012007-08-28 16:34:42 +0000127 };
128 sqlite3_mutex *p;
129 switch( iType ){
130 case SQLITE_MUTEX_RECURSIVE: {
131 p = sqlite3MallocZero( sizeof(*p) );
132 if( p ){
drh0167f282007-11-28 14:04:57 +0000133#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
134 /* If recursive mutexes are not available, we will have to
135 ** build our own. See below. */
136 pthread_mutex_init(&p->mutex, 0);
137#else
drhed05efb2007-11-28 00:51:34 +0000138 /* Use a recursive mutex if it is available */
drh437b9012007-08-28 16:34:42 +0000139 pthread_mutexattr_t recursiveAttr;
140 pthread_mutexattr_init(&recursiveAttr);
141 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
142 pthread_mutex_init(&p->mutex, &recursiveAttr);
143 pthread_mutexattr_destroy(&recursiveAttr);
drhed05efb2007-11-28 00:51:34 +0000144#endif
drh437b9012007-08-28 16:34:42 +0000145 p->id = iType;
146 }
147 break;
148 }
149 case SQLITE_MUTEX_FAST: {
150 p = sqlite3MallocZero( sizeof(*p) );
151 if( p ){
152 p->id = iType;
153 pthread_mutex_init(&p->mutex, 0);
154 }
155 break;
156 }
157 default: {
158 assert( iType-2 >= 0 );
danielk197700e13612008-11-17 19:18:54 +0000159 assert( iType-2 < ArraySize(staticMutexes) );
drh437b9012007-08-28 16:34:42 +0000160 p = &staticMutexes[iType-2];
161 p->id = iType;
162 break;
163 }
164 }
165 return p;
166}
167
168
169/*
170** This routine deallocates a previously
171** allocated mutex. SQLite is careful to deallocate every
172** mutex that it allocates.
173*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000174static void pthreadMutexFree(sqlite3_mutex *p){
175 assert( p->nRef==0 );
176 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
177 pthread_mutex_destroy(&p->mutex);
178 sqlite3_free(p);
drh437b9012007-08-28 16:34:42 +0000179}
180
181/*
182** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
183** to enter a mutex. If another thread is already within the mutex,
184** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
185** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
186** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
187** be entered multiple times by the same thread. In such cases the,
188** mutex must be exited an equal number of times before another thread
189** can enter. If the same thread tries to enter any other kind of mutex
190** more than once, the behavior is undefined.
191*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000192static void pthreadMutexEnter(sqlite3_mutex *p){
193 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
drhed05efb2007-11-28 00:51:34 +0000194
drh0167f282007-11-28 14:04:57 +0000195#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000196 /* If recursive mutexes are not available, then we have to grow
197 ** our own. This implementation assumes that pthread_equal()
198 ** is atomic - that it cannot be deceived into thinking self
199 ** and p->owner are equal if p->owner changes between two values
200 ** that are not equal to self while the comparison is taking place.
drh5f3d6522007-11-28 13:55:55 +0000201 ** This implementation also assumes a coherent cache - that
202 ** separate processes cannot read different values from the same
203 ** address at the same time. If either of these two conditions
204 ** are not met, then the mutexes will fail and problems will result.
drhed05efb2007-11-28 00:51:34 +0000205 */
206 {
207 pthread_t self = pthread_self();
208 if( p->nRef>0 && pthread_equal(p->owner, self) ){
209 p->nRef++;
210 }else{
211 pthread_mutex_lock(&p->mutex);
212 assert( p->nRef==0 );
213 p->owner = self;
214 p->nRef = 1;
215 }
216 }
drh0167f282007-11-28 14:04:57 +0000217#else
218 /* Use the built-in recursive mutexes if they are available.
219 */
220 pthread_mutex_lock(&p->mutex);
221 p->owner = pthread_self();
222 p->nRef++;
drhed05efb2007-11-28 00:51:34 +0000223#endif
224
drhd0679ed2007-08-28 22:24:34 +0000225#ifdef SQLITE_DEBUG
226 if( p->trace ){
227 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
228 }
229#endif
drh437b9012007-08-28 16:34:42 +0000230}
danielk19776d2ab0e2008-06-17 17:21:18 +0000231static int pthreadMutexTry(sqlite3_mutex *p){
drh437b9012007-08-28 16:34:42 +0000232 int rc;
danielk19776d2ab0e2008-06-17 17:21:18 +0000233 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
drhed05efb2007-11-28 00:51:34 +0000234
drh0167f282007-11-28 14:04:57 +0000235#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000236 /* If recursive mutexes are not available, then we have to grow
237 ** our own. This implementation assumes that pthread_equal()
238 ** is atomic - that it cannot be deceived into thinking self
239 ** and p->owner are equal if p->owner changes between two values
240 ** that are not equal to self while the comparison is taking place.
drh5f3d6522007-11-28 13:55:55 +0000241 ** This implementation also assumes a coherent cache - that
242 ** separate processes cannot read different values from the same
243 ** address at the same time. If either of these two conditions
244 ** are not met, then the mutexes will fail and problems will result.
drhed05efb2007-11-28 00:51:34 +0000245 */
246 {
247 pthread_t self = pthread_self();
248 if( p->nRef>0 && pthread_equal(p->owner, self) ){
249 p->nRef++;
250 rc = SQLITE_OK;
drh3bbc0e72008-07-16 12:33:23 +0000251 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
drhed05efb2007-11-28 00:51:34 +0000252 assert( p->nRef==0 );
253 p->owner = self;
254 p->nRef = 1;
255 rc = SQLITE_OK;
256 }else{
257 rc = SQLITE_BUSY;
258 }
259 }
drh0167f282007-11-28 14:04:57 +0000260#else
261 /* Use the built-in recursive mutexes if they are available.
262 */
263 if( pthread_mutex_trylock(&p->mutex)==0 ){
264 p->owner = pthread_self();
265 p->nRef++;
266 rc = SQLITE_OK;
267 }else{
268 rc = SQLITE_BUSY;
269 }
drhed05efb2007-11-28 00:51:34 +0000270#endif
271
272#ifdef SQLITE_DEBUG
273 if( rc==SQLITE_OK && p->trace ){
274 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
275 }
276#endif
drh437b9012007-08-28 16:34:42 +0000277 return rc;
278}
279
280/*
281** The sqlite3_mutex_leave() routine exits a mutex that was
282** previously entered by the same thread. The behavior
283** is undefined if the mutex is not currently entered or
284** is not currently allocated. SQLite will never do either.
285*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000286static void pthreadMutexLeave(sqlite3_mutex *p){
danielk19771a9ed0b2008-06-18 09:45:56 +0000287 assert( pthreadMutexHeld(p) );
drh437b9012007-08-28 16:34:42 +0000288 p->nRef--;
289 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
drhed05efb2007-11-28 00:51:34 +0000290
drh0167f282007-11-28 14:04:57 +0000291#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000292 if( p->nRef==0 ){
293 pthread_mutex_unlock(&p->mutex);
294 }
drh0167f282007-11-28 14:04:57 +0000295#else
296 pthread_mutex_unlock(&p->mutex);
drhed05efb2007-11-28 00:51:34 +0000297#endif
298
drhd0679ed2007-08-28 22:24:34 +0000299#ifdef SQLITE_DEBUG
300 if( p->trace ){
301 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
302 }
303#endif
drh437b9012007-08-28 16:34:42 +0000304}
305
danielk19776d2ab0e2008-06-17 17:21:18 +0000306sqlite3_mutex_methods *sqlite3DefaultMutex(void){
307 static sqlite3_mutex_methods sMutex = {
308 pthreadMutexInit,
danielk19774a9d1f62008-06-19 08:51:23 +0000309 pthreadMutexEnd,
danielk19776d2ab0e2008-06-17 17:21:18 +0000310 pthreadMutexAlloc,
311 pthreadMutexFree,
312 pthreadMutexEnter,
313 pthreadMutexTry,
314 pthreadMutexLeave,
drha4189802008-06-19 16:07:07 +0000315#ifdef SQLITE_DEBUG
danielk19776d2ab0e2008-06-17 17:21:18 +0000316 pthreadMutexHeld,
317 pthreadMutexNotheld
drh1875f7a2008-12-08 18:19:17 +0000318#else
319 0,
320 0
drha4189802008-06-19 16:07:07 +0000321#endif
danielk19776d2ab0e2008-06-17 17:21:18 +0000322 };
323
324 return &sMutex;
drh437b9012007-08-28 16:34:42 +0000325}
danielk19776d2ab0e2008-06-17 17:21:18 +0000326
drh437b9012007-08-28 16:34:42 +0000327#endif /* SQLITE_MUTEX_PTHREAD */