blob: f0e0879af6c9c9d802ee00c617a9ca67ef412f96 [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
13**
danielk197700e13612008-11-17 19:18:54 +000014** $Id: mutex_unix.c,v 1.15 2008/11/17 19:18:55 danielk1977 Exp $
drh437b9012007-08-28 16:34:42 +000015*/
16#include "sqliteInt.h"
17
18/*
19** The code in this file is only used if we are compiling threadsafe
20** under unix with pthreads.
21**
22** Note that this implementation requires a version of pthreads that
23** supports recursive mutexes.
24*/
25#ifdef SQLITE_MUTEX_PTHREADS
26
27#include <pthread.h>
28
drhed05efb2007-11-28 00:51:34 +000029
drh437b9012007-08-28 16:34:42 +000030/*
31** Each recursive mutex is an instance of the following structure.
32*/
33struct sqlite3_mutex {
34 pthread_mutex_t mutex; /* Mutex controlling the lock */
35 int id; /* Mutex type */
36 int nRef; /* Number of entrances */
37 pthread_t owner; /* Thread that is within this mutex */
drhd0679ed2007-08-28 22:24:34 +000038#ifdef SQLITE_DEBUG
39 int trace; /* True to trace changes */
40#endif
drh437b9012007-08-28 16:34:42 +000041};
rse28f667f2008-03-29 12:47:27 +000042#ifdef SQLITE_DEBUG
43#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
44#else
45#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 }
46#endif
drh437b9012007-08-28 16:34:42 +000047
48/*
danielk19776d2ab0e2008-06-17 17:21:18 +000049** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
50** intended for use only inside assert() statements. On some platforms,
51** there might be race conditions that can cause these routines to
52** deliver incorrect results. In particular, if pthread_equal() is
53** not an atomic operation, then these routines might delivery
54** incorrect results. On most platforms, pthread_equal() is a
55** comparison of two integers and is therefore atomic. But we are
56** told that HPUX is not such a platform. If so, then these routines
57** will not always work correctly on HPUX.
58**
59** On those platforms where pthread_equal() is not atomic, SQLite
60** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
61** make sure no assert() statements are evaluated and hence these
62** routines are never called.
63*/
chw97185482008-11-17 08:05:31 +000064#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
danielk19776d2ab0e2008-06-17 17:21:18 +000065static int pthreadMutexHeld(sqlite3_mutex *p){
66 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
67}
68static int pthreadMutexNotheld(sqlite3_mutex *p){
69 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
70}
71#endif
72
73/*
drh40257ff2008-06-13 18:24:27 +000074** Initialize and deinitialize the mutex subsystem.
75*/
danielk19776d2ab0e2008-06-17 17:21:18 +000076static int pthreadMutexInit(void){ return SQLITE_OK; }
77static int pthreadMutexEnd(void){ return SQLITE_OK; }
drh40257ff2008-06-13 18:24:27 +000078
79/*
drh437b9012007-08-28 16:34:42 +000080** The sqlite3_mutex_alloc() routine allocates a new
81** mutex and returns a pointer to it. If it returns NULL
82** that means that a mutex could not be allocated. SQLite
83** will unwind its stack and return an error. The argument
84** to sqlite3_mutex_alloc() is one of these integer constants:
85**
86** <ul>
87** <li> SQLITE_MUTEX_FAST
88** <li> SQLITE_MUTEX_RECURSIVE
89** <li> SQLITE_MUTEX_STATIC_MASTER
90** <li> SQLITE_MUTEX_STATIC_MEM
91** <li> SQLITE_MUTEX_STATIC_MEM2
92** <li> SQLITE_MUTEX_STATIC_PRNG
93** <li> SQLITE_MUTEX_STATIC_LRU
94** </ul>
95**
96** The first two constants cause sqlite3_mutex_alloc() to create
97** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
98** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
99** The mutex implementation does not need to make a distinction
100** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
101** not want to. But SQLite will only request a recursive mutex in
102** cases where it really needs one. If a faster non-recursive mutex
103** implementation is available on the host platform, the mutex subsystem
104** might return such a mutex in response to SQLITE_MUTEX_FAST.
105**
106** The other allowed parameters to sqlite3_mutex_alloc() each return
107** a pointer to a static preexisting mutex. Three static mutexes are
108** used by the current version of SQLite. Future versions of SQLite
109** may add additional static mutexes. Static mutexes are for internal
110** use by SQLite only. Applications that use SQLite mutexes should
111** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
112** SQLITE_MUTEX_RECURSIVE.
113**
114** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
115** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
116** returns a different mutex on every call. But for the static
117** mutex types, the same mutex is returned on every call that has
118** the same type number.
119*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000120static sqlite3_mutex *pthreadMutexAlloc(int iType){
drh437b9012007-08-28 16:34:42 +0000121 static sqlite3_mutex staticMutexes[] = {
rse28f667f2008-03-29 12:47:27 +0000122 SQLITE3_MUTEX_INITIALIZER,
123 SQLITE3_MUTEX_INITIALIZER,
124 SQLITE3_MUTEX_INITIALIZER,
125 SQLITE3_MUTEX_INITIALIZER,
126 SQLITE3_MUTEX_INITIALIZER,
127 SQLITE3_MUTEX_INITIALIZER
drh437b9012007-08-28 16:34:42 +0000128 };
129 sqlite3_mutex *p;
130 switch( iType ){
131 case SQLITE_MUTEX_RECURSIVE: {
132 p = sqlite3MallocZero( sizeof(*p) );
133 if( p ){
drh0167f282007-11-28 14:04:57 +0000134#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
135 /* If recursive mutexes are not available, we will have to
136 ** build our own. See below. */
137 pthread_mutex_init(&p->mutex, 0);
138#else
drhed05efb2007-11-28 00:51:34 +0000139 /* Use a recursive mutex if it is available */
drh437b9012007-08-28 16:34:42 +0000140 pthread_mutexattr_t recursiveAttr;
141 pthread_mutexattr_init(&recursiveAttr);
142 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
143 pthread_mutex_init(&p->mutex, &recursiveAttr);
144 pthread_mutexattr_destroy(&recursiveAttr);
drhed05efb2007-11-28 00:51:34 +0000145#endif
drh437b9012007-08-28 16:34:42 +0000146 p->id = iType;
147 }
148 break;
149 }
150 case SQLITE_MUTEX_FAST: {
151 p = sqlite3MallocZero( sizeof(*p) );
152 if( p ){
153 p->id = iType;
154 pthread_mutex_init(&p->mutex, 0);
155 }
156 break;
157 }
158 default: {
159 assert( iType-2 >= 0 );
danielk197700e13612008-11-17 19:18:54 +0000160 assert( iType-2 < ArraySize(staticMutexes) );
drh437b9012007-08-28 16:34:42 +0000161 p = &staticMutexes[iType-2];
162 p->id = iType;
163 break;
164 }
165 }
166 return p;
167}
168
169
170/*
171** This routine deallocates a previously
172** allocated mutex. SQLite is careful to deallocate every
173** mutex that it allocates.
174*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000175static void pthreadMutexFree(sqlite3_mutex *p){
176 assert( p->nRef==0 );
177 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
178 pthread_mutex_destroy(&p->mutex);
179 sqlite3_free(p);
drh437b9012007-08-28 16:34:42 +0000180}
181
182/*
183** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
184** to enter a mutex. If another thread is already within the mutex,
185** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
186** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
187** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
188** be entered multiple times by the same thread. In such cases the,
189** mutex must be exited an equal number of times before another thread
190** can enter. If the same thread tries to enter any other kind of mutex
191** more than once, the behavior is undefined.
192*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000193static void pthreadMutexEnter(sqlite3_mutex *p){
194 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
drhed05efb2007-11-28 00:51:34 +0000195
drh0167f282007-11-28 14:04:57 +0000196#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000197 /* If recursive mutexes are not available, then we have to grow
198 ** our own. This implementation assumes that pthread_equal()
199 ** is atomic - that it cannot be deceived into thinking self
200 ** and p->owner are equal if p->owner changes between two values
201 ** that are not equal to self while the comparison is taking place.
drh5f3d6522007-11-28 13:55:55 +0000202 ** This implementation also assumes a coherent cache - that
203 ** separate processes cannot read different values from the same
204 ** address at the same time. If either of these two conditions
205 ** are not met, then the mutexes will fail and problems will result.
drhed05efb2007-11-28 00:51:34 +0000206 */
207 {
208 pthread_t self = pthread_self();
209 if( p->nRef>0 && pthread_equal(p->owner, self) ){
210 p->nRef++;
211 }else{
212 pthread_mutex_lock(&p->mutex);
213 assert( p->nRef==0 );
214 p->owner = self;
215 p->nRef = 1;
216 }
217 }
drh0167f282007-11-28 14:04:57 +0000218#else
219 /* Use the built-in recursive mutexes if they are available.
220 */
221 pthread_mutex_lock(&p->mutex);
222 p->owner = pthread_self();
223 p->nRef++;
drhed05efb2007-11-28 00:51:34 +0000224#endif
225
drhd0679ed2007-08-28 22:24:34 +0000226#ifdef SQLITE_DEBUG
227 if( p->trace ){
228 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
229 }
230#endif
drh437b9012007-08-28 16:34:42 +0000231}
danielk19776d2ab0e2008-06-17 17:21:18 +0000232static int pthreadMutexTry(sqlite3_mutex *p){
drh437b9012007-08-28 16:34:42 +0000233 int rc;
danielk19776d2ab0e2008-06-17 17:21:18 +0000234 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
drhed05efb2007-11-28 00:51:34 +0000235
drh0167f282007-11-28 14:04:57 +0000236#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000237 /* If recursive mutexes are not available, then we have to grow
238 ** our own. This implementation assumes that pthread_equal()
239 ** is atomic - that it cannot be deceived into thinking self
240 ** and p->owner are equal if p->owner changes between two values
241 ** that are not equal to self while the comparison is taking place.
drh5f3d6522007-11-28 13:55:55 +0000242 ** This implementation also assumes a coherent cache - that
243 ** separate processes cannot read different values from the same
244 ** address at the same time. If either of these two conditions
245 ** are not met, then the mutexes will fail and problems will result.
drhed05efb2007-11-28 00:51:34 +0000246 */
247 {
248 pthread_t self = pthread_self();
249 if( p->nRef>0 && pthread_equal(p->owner, self) ){
250 p->nRef++;
251 rc = SQLITE_OK;
drh3bbc0e72008-07-16 12:33:23 +0000252 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
drhed05efb2007-11-28 00:51:34 +0000253 assert( p->nRef==0 );
254 p->owner = self;
255 p->nRef = 1;
256 rc = SQLITE_OK;
257 }else{
258 rc = SQLITE_BUSY;
259 }
260 }
drh0167f282007-11-28 14:04:57 +0000261#else
262 /* Use the built-in recursive mutexes if they are available.
263 */
264 if( pthread_mutex_trylock(&p->mutex)==0 ){
265 p->owner = pthread_self();
266 p->nRef++;
267 rc = SQLITE_OK;
268 }else{
269 rc = SQLITE_BUSY;
270 }
drhed05efb2007-11-28 00:51:34 +0000271#endif
272
273#ifdef SQLITE_DEBUG
274 if( rc==SQLITE_OK && p->trace ){
275 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
276 }
277#endif
drh437b9012007-08-28 16:34:42 +0000278 return rc;
279}
280
281/*
282** The sqlite3_mutex_leave() routine exits a mutex that was
283** previously entered by the same thread. The behavior
284** is undefined if the mutex is not currently entered or
285** is not currently allocated. SQLite will never do either.
286*/
danielk19776d2ab0e2008-06-17 17:21:18 +0000287static void pthreadMutexLeave(sqlite3_mutex *p){
danielk19771a9ed0b2008-06-18 09:45:56 +0000288 assert( pthreadMutexHeld(p) );
drh437b9012007-08-28 16:34:42 +0000289 p->nRef--;
290 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
drhed05efb2007-11-28 00:51:34 +0000291
drh0167f282007-11-28 14:04:57 +0000292#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000293 if( p->nRef==0 ){
294 pthread_mutex_unlock(&p->mutex);
295 }
drh0167f282007-11-28 14:04:57 +0000296#else
297 pthread_mutex_unlock(&p->mutex);
drhed05efb2007-11-28 00:51:34 +0000298#endif
299
drhd0679ed2007-08-28 22:24:34 +0000300#ifdef SQLITE_DEBUG
301 if( p->trace ){
302 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
303 }
304#endif
drh437b9012007-08-28 16:34:42 +0000305}
306
danielk19776d2ab0e2008-06-17 17:21:18 +0000307sqlite3_mutex_methods *sqlite3DefaultMutex(void){
308 static sqlite3_mutex_methods sMutex = {
309 pthreadMutexInit,
danielk19774a9d1f62008-06-19 08:51:23 +0000310 pthreadMutexEnd,
danielk19776d2ab0e2008-06-17 17:21:18 +0000311 pthreadMutexAlloc,
312 pthreadMutexFree,
313 pthreadMutexEnter,
314 pthreadMutexTry,
315 pthreadMutexLeave,
drha4189802008-06-19 16:07:07 +0000316#ifdef SQLITE_DEBUG
danielk19776d2ab0e2008-06-17 17:21:18 +0000317 pthreadMutexHeld,
318 pthreadMutexNotheld
drha4189802008-06-19 16:07:07 +0000319#endif
danielk19776d2ab0e2008-06-17 17:21:18 +0000320 };
321
322 return &sMutex;
drh437b9012007-08-28 16:34:42 +0000323}
danielk19776d2ab0e2008-06-17 17:21:18 +0000324
drh437b9012007-08-28 16:34:42 +0000325#endif /* SQLITE_MUTEX_PTHREAD */