mutex_unix.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  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. **
  14. ** $Id: mutex_unix.c,v 1.5 2007/11/28 14:04:57 drh Exp $
  15. */
  16. #include "sqliteInt.h"
  17. /*
  18. ** The code in this file is only used if we are compiling threadsafe
  19. ** under unix with pthreads.
  20. **
  21. ** Note that this implementation requires a version of pthreads that
  22. ** supports recursive mutexes.
  23. */
  24. #ifdef SQLITE_MUTEX_PTHREADS
  25. #include <pthread.h>
  26. /*
  27. ** Each recursive mutex is an instance of the following structure.
  28. */
  29. struct sqlite3_mutex {
  30. pthread_mutex_t mutex; /* Mutex controlling the lock */
  31. int id; /* Mutex type */
  32. int nRef; /* Number of entrances */
  33. pthread_t owner; /* Thread that is within this mutex */
  34. #ifdef SQLITE_DEBUG
  35. int trace; /* True to trace changes */
  36. #endif
  37. };
  38. /*
  39. ** The sqlite3_mutex_alloc() routine allocates a new
  40. ** mutex and returns a pointer to it. If it returns NULL
  41. ** that means that a mutex could not be allocated. SQLite
  42. ** will unwind its stack and return an error. The argument
  43. ** to sqlite3_mutex_alloc() is one of these integer constants:
  44. **
  45. ** <ul>
  46. ** <li> SQLITE_MUTEX_FAST
  47. ** <li> SQLITE_MUTEX_RECURSIVE
  48. ** <li> SQLITE_MUTEX_STATIC_MASTER
  49. ** <li> SQLITE_MUTEX_STATIC_MEM
  50. ** <li> SQLITE_MUTEX_STATIC_MEM2
  51. ** <li> SQLITE_MUTEX_STATIC_PRNG
  52. ** <li> SQLITE_MUTEX_STATIC_LRU
  53. ** </ul>
  54. **
  55. ** The first two constants cause sqlite3_mutex_alloc() to create
  56. ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  57. ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  58. ** The mutex implementation does not need to make a distinction
  59. ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  60. ** not want to. But SQLite will only request a recursive mutex in
  61. ** cases where it really needs one. If a faster non-recursive mutex
  62. ** implementation is available on the host platform, the mutex subsystem
  63. ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  64. **
  65. ** The other allowed parameters to sqlite3_mutex_alloc() each return
  66. ** a pointer to a static preexisting mutex. Three static mutexes are
  67. ** used by the current version of SQLite. Future versions of SQLite
  68. ** may add additional static mutexes. Static mutexes are for internal
  69. ** use by SQLite only. Applications that use SQLite mutexes should
  70. ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  71. ** SQLITE_MUTEX_RECURSIVE.
  72. **
  73. ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  74. ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  75. ** returns a different mutex on every call. But for the static
  76. ** mutex types, the same mutex is returned on every call that has
  77. ** the same type number.
  78. */
  79. sqlite3_mutex *sqlite3_mutex_alloc(int iType){
  80. static sqlite3_mutex staticMutexes[] = {
  81. { PTHREAD_MUTEX_INITIALIZER, },
  82. { PTHREAD_MUTEX_INITIALIZER, },
  83. { PTHREAD_MUTEX_INITIALIZER, },
  84. { PTHREAD_MUTEX_INITIALIZER, },
  85. { PTHREAD_MUTEX_INITIALIZER, },
  86. };
  87. sqlite3_mutex *p;
  88. switch( iType ){
  89. case SQLITE_MUTEX_RECURSIVE: {
  90. p = sqlite3MallocZero( sizeof(*p) );
  91. if( p ){
  92. #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
  93. /* If recursive mutexes are not available, we will have to
  94. ** build our own. See below. */
  95. pthread_mutex_init(&p->mutex, 0);
  96. #else
  97. /* Use a recursive mutex if it is available */
  98. pthread_mutexattr_t recursiveAttr;
  99. pthread_mutexattr_init(&recursiveAttr);
  100. pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
  101. pthread_mutex_init(&p->mutex, &recursiveAttr);
  102. pthread_mutexattr_destroy(&recursiveAttr);
  103. #endif
  104. p->id = iType;
  105. }
  106. break;
  107. }
  108. case SQLITE_MUTEX_FAST: {
  109. p = sqlite3MallocZero( sizeof(*p) );
  110. if( p ){
  111. p->id = iType;
  112. pthread_mutex_init(&p->mutex, 0);
  113. }
  114. break;
  115. }
  116. default: {
  117. assert( iType-2 >= 0 );
  118. assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
  119. p = &staticMutexes[iType-2];
  120. p->id = iType;
  121. break;
  122. }
  123. }
  124. return p;
  125. }
  126. /*
  127. ** This routine deallocates a previously
  128. ** allocated mutex. SQLite is careful to deallocate every
  129. ** mutex that it allocates.
  130. */
  131. void sqlite3_mutex_free(sqlite3_mutex *p){
  132. assert( p );
  133. assert( p->nRef==0 );
  134. assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  135. pthread_mutex_destroy(&p->mutex);
  136. sqlite3_free(p);
  137. }
  138. /*
  139. ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  140. ** to enter a mutex. If another thread is already within the mutex,
  141. ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  142. ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
  143. ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
  144. ** be entered multiple times by the same thread. In such cases the,
  145. ** mutex must be exited an equal number of times before another thread
  146. ** can enter. If the same thread tries to enter any other kind of mutex
  147. ** more than once, the behavior is undefined.
  148. */
  149. void sqlite3_mutex_enter(sqlite3_mutex *p){
  150. assert( p );
  151. assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
  152. #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
  153. /* If recursive mutexes are not available, then we have to grow
  154. ** our own. This implementation assumes that pthread_equal()
  155. ** is atomic - that it cannot be deceived into thinking self
  156. ** and p->owner are equal if p->owner changes between two values
  157. ** that are not equal to self while the comparison is taking place.
  158. ** This implementation also assumes a coherent cache - that
  159. ** separate processes cannot read different values from the same
  160. ** address at the same time. If either of these two conditions
  161. ** are not met, then the mutexes will fail and problems will result.
  162. */
  163. {
  164. pthread_t self = pthread_self();
  165. if( p->nRef>0 && pthread_equal(p->owner, self) ){
  166. p->nRef++;
  167. }else{
  168. pthread_mutex_lock(&p->mutex);
  169. assert( p->nRef==0 );
  170. p->owner = self;
  171. p->nRef = 1;
  172. }
  173. }
  174. #else
  175. /* Use the built-in recursive mutexes if they are available.
  176. */
  177. pthread_mutex_lock(&p->mutex);
  178. p->owner = pthread_self();
  179. p->nRef++;
  180. #endif
  181. #ifdef SQLITE_DEBUG
  182. if( p->trace ){
  183. printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  184. }
  185. #endif
  186. }
  187. int sqlite3_mutex_try(sqlite3_mutex *p){
  188. int rc;
  189. assert( p );
  190. assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
  191. #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
  192. /* If recursive mutexes are not available, then we have to grow
  193. ** our own. This implementation assumes that pthread_equal()
  194. ** is atomic - that it cannot be deceived into thinking self
  195. ** and p->owner are equal if p->owner changes between two values
  196. ** that are not equal to self while the comparison is taking place.
  197. ** This implementation also assumes a coherent cache - that
  198. ** separate processes cannot read different values from the same
  199. ** address at the same time. If either of these two conditions
  200. ** are not met, then the mutexes will fail and problems will result.
  201. */
  202. {
  203. pthread_t self = pthread_self();
  204. if( p->nRef>0 && pthread_equal(p->owner, self) ){
  205. p->nRef++;
  206. rc = SQLITE_OK;
  207. }else if( pthread_mutex_lock(&p->mutex)==0 ){
  208. assert( p->nRef==0 );
  209. p->owner = self;
  210. p->nRef = 1;
  211. rc = SQLITE_OK;
  212. }else{
  213. rc = SQLITE_BUSY;
  214. }
  215. }
  216. #else
  217. /* Use the built-in recursive mutexes if they are available.
  218. */
  219. if( pthread_mutex_trylock(&p->mutex)==0 ){
  220. p->owner = pthread_self();
  221. p->nRef++;
  222. rc = SQLITE_OK;
  223. }else{
  224. rc = SQLITE_BUSY;
  225. }
  226. #endif
  227. #ifdef SQLITE_DEBUG
  228. if( rc==SQLITE_OK && p->trace ){
  229. printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  230. }
  231. #endif
  232. return rc;
  233. }
  234. /*
  235. ** The sqlite3_mutex_leave() routine exits a mutex that was
  236. ** previously entered by the same thread. The behavior
  237. ** is undefined if the mutex is not currently entered or
  238. ** is not currently allocated. SQLite will never do either.
  239. */
  240. void sqlite3_mutex_leave(sqlite3_mutex *p){
  241. assert( p );
  242. assert( sqlite3_mutex_held(p) );
  243. p->nRef--;
  244. assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
  245. #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
  246. if( p->nRef==0 ){
  247. pthread_mutex_unlock(&p->mutex);
  248. }
  249. #else
  250. pthread_mutex_unlock(&p->mutex);
  251. #endif
  252. #ifdef SQLITE_DEBUG
  253. if( p->trace ){
  254. printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  255. }
  256. #endif
  257. }
  258. /*
  259. ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  260. ** intended for use only inside assert() statements. On some platforms,
  261. ** there might be race conditions that can cause these routines to
  262. ** deliver incorrect results. In particular, if pthread_equal() is
  263. ** not an atomic operation, then these routines might delivery
  264. ** incorrect results. On most platforms, pthread_equal() is a
  265. ** comparison of two integers and is therefore atomic. But we are
  266. ** told that HPUX is not such a platform. If so, then these routines
  267. ** will not always work correctly on HPUX.
  268. **
  269. ** On those platforms where pthread_equal() is not atomic, SQLite
  270. ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
  271. ** make sure no assert() statements are evaluated and hence these
  272. ** routines are never called.
  273. */
  274. #ifndef NDEBUG
  275. int sqlite3_mutex_held(sqlite3_mutex *p){
  276. return p==0 || (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
  277. }
  278. int sqlite3_mutex_notheld(sqlite3_mutex *p){
  279. return p==0 || p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
  280. }
  281. #endif
  282. #endif /* SQLITE_MUTEX_PTHREAD */