blob: 4639a84277e272cecfcbba3920a98c9baea3c551 [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**
drh40257ff2008-06-13 18:24:27 +000014** $Id: mutex_unix.c,v 1.8 2008/06/13 18:24:27 drh 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/*
drh40257ff2008-06-13 18:24:27 +000049** Initialize and deinitialize the mutex subsystem.
50*/
51int sqlite3_mutex_init(void){ return SQLITE_OK; }
52int sqlite3_mutex_end(void){ return SQLITE_OK; }
53
54/*
drh437b9012007-08-28 16:34:42 +000055** The sqlite3_mutex_alloc() routine allocates a new
56** mutex and returns a pointer to it. If it returns NULL
57** that means that a mutex could not be allocated. SQLite
58** will unwind its stack and return an error. The argument
59** to sqlite3_mutex_alloc() is one of these integer constants:
60**
61** <ul>
62** <li> SQLITE_MUTEX_FAST
63** <li> SQLITE_MUTEX_RECURSIVE
64** <li> SQLITE_MUTEX_STATIC_MASTER
65** <li> SQLITE_MUTEX_STATIC_MEM
66** <li> SQLITE_MUTEX_STATIC_MEM2
67** <li> SQLITE_MUTEX_STATIC_PRNG
68** <li> SQLITE_MUTEX_STATIC_LRU
69** </ul>
70**
71** The first two constants cause sqlite3_mutex_alloc() to create
72** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
73** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
74** The mutex implementation does not need to make a distinction
75** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
76** not want to. But SQLite will only request a recursive mutex in
77** cases where it really needs one. If a faster non-recursive mutex
78** implementation is available on the host platform, the mutex subsystem
79** might return such a mutex in response to SQLITE_MUTEX_FAST.
80**
81** The other allowed parameters to sqlite3_mutex_alloc() each return
82** a pointer to a static preexisting mutex. Three static mutexes are
83** used by the current version of SQLite. Future versions of SQLite
84** may add additional static mutexes. Static mutexes are for internal
85** use by SQLite only. Applications that use SQLite mutexes should
86** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
87** SQLITE_MUTEX_RECURSIVE.
88**
89** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
90** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
91** returns a different mutex on every call. But for the static
92** mutex types, the same mutex is returned on every call that has
93** the same type number.
94*/
95sqlite3_mutex *sqlite3_mutex_alloc(int iType){
96 static sqlite3_mutex staticMutexes[] = {
rse28f667f2008-03-29 12:47:27 +000097 SQLITE3_MUTEX_INITIALIZER,
98 SQLITE3_MUTEX_INITIALIZER,
99 SQLITE3_MUTEX_INITIALIZER,
100 SQLITE3_MUTEX_INITIALIZER,
101 SQLITE3_MUTEX_INITIALIZER,
102 SQLITE3_MUTEX_INITIALIZER
drh437b9012007-08-28 16:34:42 +0000103 };
104 sqlite3_mutex *p;
105 switch( iType ){
106 case SQLITE_MUTEX_RECURSIVE: {
107 p = sqlite3MallocZero( sizeof(*p) );
108 if( p ){
drh0167f282007-11-28 14:04:57 +0000109#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
110 /* If recursive mutexes are not available, we will have to
111 ** build our own. See below. */
112 pthread_mutex_init(&p->mutex, 0);
113#else
drhed05efb2007-11-28 00:51:34 +0000114 /* Use a recursive mutex if it is available */
drh437b9012007-08-28 16:34:42 +0000115 pthread_mutexattr_t recursiveAttr;
116 pthread_mutexattr_init(&recursiveAttr);
117 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
118 pthread_mutex_init(&p->mutex, &recursiveAttr);
119 pthread_mutexattr_destroy(&recursiveAttr);
drhed05efb2007-11-28 00:51:34 +0000120#endif
drh437b9012007-08-28 16:34:42 +0000121 p->id = iType;
122 }
123 break;
124 }
125 case SQLITE_MUTEX_FAST: {
126 p = sqlite3MallocZero( sizeof(*p) );
127 if( p ){
128 p->id = iType;
129 pthread_mutex_init(&p->mutex, 0);
130 }
131 break;
132 }
133 default: {
134 assert( iType-2 >= 0 );
135 assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
136 p = &staticMutexes[iType-2];
137 p->id = iType;
138 break;
139 }
140 }
141 return p;
142}
143
144
145/*
146** This routine deallocates a previously
147** allocated mutex. SQLite is careful to deallocate every
148** mutex that it allocates.
149*/
150void sqlite3_mutex_free(sqlite3_mutex *p){
drh40257ff2008-06-13 18:24:27 +0000151 if( p ){
152 assert( p->nRef==0 );
153 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
154 pthread_mutex_destroy(&p->mutex);
155 sqlite3_free(p);
156 }
drh437b9012007-08-28 16:34:42 +0000157}
158
159/*
160** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
161** to enter a mutex. If another thread is already within the mutex,
162** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
163** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
164** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
165** be entered multiple times by the same thread. In such cases the,
166** mutex must be exited an equal number of times before another thread
167** can enter. If the same thread tries to enter any other kind of mutex
168** more than once, the behavior is undefined.
169*/
170void sqlite3_mutex_enter(sqlite3_mutex *p){
drh40257ff2008-06-13 18:24:27 +0000171 if( p==0 ) return;
drh437b9012007-08-28 16:34:42 +0000172 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
drhed05efb2007-11-28 00:51:34 +0000173
drh0167f282007-11-28 14:04:57 +0000174#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000175 /* If recursive mutexes are not available, then we have to grow
176 ** our own. This implementation assumes that pthread_equal()
177 ** is atomic - that it cannot be deceived into thinking self
178 ** and p->owner are equal if p->owner changes between two values
179 ** that are not equal to self while the comparison is taking place.
drh5f3d6522007-11-28 13:55:55 +0000180 ** This implementation also assumes a coherent cache - that
181 ** separate processes cannot read different values from the same
182 ** address at the same time. If either of these two conditions
183 ** are not met, then the mutexes will fail and problems will result.
drhed05efb2007-11-28 00:51:34 +0000184 */
185 {
186 pthread_t self = pthread_self();
187 if( p->nRef>0 && pthread_equal(p->owner, self) ){
188 p->nRef++;
189 }else{
190 pthread_mutex_lock(&p->mutex);
191 assert( p->nRef==0 );
192 p->owner = self;
193 p->nRef = 1;
194 }
195 }
drh0167f282007-11-28 14:04:57 +0000196#else
197 /* Use the built-in recursive mutexes if they are available.
198 */
199 pthread_mutex_lock(&p->mutex);
200 p->owner = pthread_self();
201 p->nRef++;
drhed05efb2007-11-28 00:51:34 +0000202#endif
203
drhd0679ed2007-08-28 22:24:34 +0000204#ifdef SQLITE_DEBUG
205 if( p->trace ){
206 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
207 }
208#endif
drh437b9012007-08-28 16:34:42 +0000209}
210int sqlite3_mutex_try(sqlite3_mutex *p){
211 int rc;
drh40257ff2008-06-13 18:24:27 +0000212 if( p==0 ) return SQLITE_OK;
drh437b9012007-08-28 16:34:42 +0000213 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
drhed05efb2007-11-28 00:51:34 +0000214
drh0167f282007-11-28 14:04:57 +0000215#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000216 /* If recursive mutexes are not available, then we have to grow
217 ** our own. This implementation assumes that pthread_equal()
218 ** is atomic - that it cannot be deceived into thinking self
219 ** and p->owner are equal if p->owner changes between two values
220 ** that are not equal to self while the comparison is taking place.
drh5f3d6522007-11-28 13:55:55 +0000221 ** This implementation also assumes a coherent cache - that
222 ** separate processes cannot read different values from the same
223 ** address at the same time. If either of these two conditions
224 ** are not met, then the mutexes will fail and problems will result.
drhed05efb2007-11-28 00:51:34 +0000225 */
226 {
227 pthread_t self = pthread_self();
228 if( p->nRef>0 && pthread_equal(p->owner, self) ){
229 p->nRef++;
230 rc = SQLITE_OK;
231 }else if( pthread_mutex_lock(&p->mutex)==0 ){
232 assert( p->nRef==0 );
233 p->owner = self;
234 p->nRef = 1;
235 rc = SQLITE_OK;
236 }else{
237 rc = SQLITE_BUSY;
238 }
239 }
drh0167f282007-11-28 14:04:57 +0000240#else
241 /* Use the built-in recursive mutexes if they are available.
242 */
243 if( pthread_mutex_trylock(&p->mutex)==0 ){
244 p->owner = pthread_self();
245 p->nRef++;
246 rc = SQLITE_OK;
247 }else{
248 rc = SQLITE_BUSY;
249 }
drhed05efb2007-11-28 00:51:34 +0000250#endif
251
252#ifdef SQLITE_DEBUG
253 if( rc==SQLITE_OK && p->trace ){
254 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
255 }
256#endif
drh437b9012007-08-28 16:34:42 +0000257 return rc;
258}
259
260/*
261** The sqlite3_mutex_leave() routine exits a mutex that was
262** previously entered by the same thread. The behavior
263** is undefined if the mutex is not currently entered or
264** is not currently allocated. SQLite will never do either.
265*/
266void sqlite3_mutex_leave(sqlite3_mutex *p){
drh40257ff2008-06-13 18:24:27 +0000267 if( p==0 ) return;
drh437b9012007-08-28 16:34:42 +0000268 assert( sqlite3_mutex_held(p) );
269 p->nRef--;
270 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
drhed05efb2007-11-28 00:51:34 +0000271
drh0167f282007-11-28 14:04:57 +0000272#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
drhed05efb2007-11-28 00:51:34 +0000273 if( p->nRef==0 ){
274 pthread_mutex_unlock(&p->mutex);
275 }
drh0167f282007-11-28 14:04:57 +0000276#else
277 pthread_mutex_unlock(&p->mutex);
drhed05efb2007-11-28 00:51:34 +0000278#endif
279
drhd0679ed2007-08-28 22:24:34 +0000280#ifdef SQLITE_DEBUG
281 if( p->trace ){
282 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
283 }
284#endif
drh437b9012007-08-28 16:34:42 +0000285}
286
287/*
288** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
289** intended for use only inside assert() statements. On some platforms,
290** there might be race conditions that can cause these routines to
291** deliver incorrect results. In particular, if pthread_equal() is
292** not an atomic operation, then these routines might delivery
293** incorrect results. On most platforms, pthread_equal() is a
294** comparison of two integers and is therefore atomic. But we are
295** told that HPUX is not such a platform. If so, then these routines
296** will not always work correctly on HPUX.
297**
298** On those platforms where pthread_equal() is not atomic, SQLite
299** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
300** make sure no assert() statements are evaluated and hence these
301** routines are never called.
302*/
303#ifndef NDEBUG
304int sqlite3_mutex_held(sqlite3_mutex *p){
305 return p==0 || (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
306}
307int sqlite3_mutex_notheld(sqlite3_mutex *p){
308 return p==0 || p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
309}
310#endif
311#endif /* SQLITE_MUTEX_PTHREAD */