drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2007 August 14 |
| 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 | ************************************************************************* |
drh | 437b901 | 2007-08-28 16:34:42 +0000 | [diff] [blame] | 12 | ** This file contains the C functions that implement mutexes. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 13 | ** |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 14 | ** This file contains code that is common across all mutex implementations. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
| 17 | |
drh | d0bc5d2 | 2009-09-10 22:30:53 +0000 | [diff] [blame] | 18 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT) |
drh | fe5bdb3 | 2009-09-10 17:45:00 +0000 | [diff] [blame] | 19 | /* |
| 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 |
| 22 | ** allocate a mutex while the system is uninitialized. |
| 23 | */ |
| 24 | static SQLITE_WSD int mutexIsInit = 0; |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 25 | #endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */ |
drh | fe5bdb3 | 2009-09-10 17:45:00 +0000 | [diff] [blame] | 26 | |
| 27 | |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 28 | #ifndef SQLITE_MUTEX_OMIT |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 29 | /* |
| 30 | ** Initialize the mutex system. |
| 31 | */ |
danielk1977 | d025174 | 2008-06-18 18:57:42 +0000 | [diff] [blame] | 32 | int sqlite3MutexInit(void){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 33 | int rc = SQLITE_OK; |
drh | 92d7652 | 2010-05-05 00:05:24 +0000 | [diff] [blame] | 34 | if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){ |
| 35 | /* If the xMutexAlloc method has not been set, then the user did not |
| 36 | ** install a mutex implementation via sqlite3_config() prior to |
| 37 | ** sqlite3_initialize() being called. This block copies pointers to |
| 38 | ** the default implementation into the sqlite3GlobalConfig structure. |
| 39 | */ |
dan | 558814f | 2010-06-02 05:53:53 +0000 | [diff] [blame] | 40 | sqlite3_mutex_methods const *pFrom; |
drh | 92d7652 | 2010-05-05 00:05:24 +0000 | [diff] [blame] | 41 | sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 42 | |
drh | 92d7652 | 2010-05-05 00:05:24 +0000 | [diff] [blame] | 43 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 44 | pFrom = sqlite3DefaultMutex(); |
| 45 | }else{ |
| 46 | pFrom = sqlite3NoopMutex(); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 47 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 48 | pTo->xMutexInit = pFrom->xMutexInit; |
| 49 | pTo->xMutexEnd = pFrom->xMutexEnd; |
| 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; |
drh | 6081c1d | 2015-09-06 02:51:04 +0000 | [diff] [blame] | 56 | sqlite3MemoryBarrier(); |
drh | 92d7652 | 2010-05-05 00:05:24 +0000 | [diff] [blame] | 57 | pTo->xMutexAlloc = pFrom->xMutexAlloc; |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 58 | } |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 59 | assert( sqlite3GlobalConfig.mutex.xMutexInit ); |
drh | 92d7652 | 2010-05-05 00:05:24 +0000 | [diff] [blame] | 60 | rc = sqlite3GlobalConfig.mutex.xMutexInit(); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 61 | |
drh | fe5bdb3 | 2009-09-10 17:45:00 +0000 | [diff] [blame] | 62 | #ifdef SQLITE_DEBUG |
| 63 | GLOBAL(int, mutexIsInit) = 1; |
| 64 | #endif |
| 65 | |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 66 | return rc; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | ** Shutdown the mutex system. This call frees resources allocated by |
danielk1977 | d025174 | 2008-06-18 18:57:42 +0000 | [diff] [blame] | 71 | ** sqlite3MutexInit(). |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 72 | */ |
danielk1977 | d025174 | 2008-06-18 18:57:42 +0000 | [diff] [blame] | 73 | int sqlite3MutexEnd(void){ |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 74 | int rc = SQLITE_OK; |
danielk1977 | 0a54907 | 2009-02-17 16:29:10 +0000 | [diff] [blame] | 75 | if( sqlite3GlobalConfig.mutex.xMutexEnd ){ |
| 76 | rc = sqlite3GlobalConfig.mutex.xMutexEnd(); |
| 77 | } |
drh | fe5bdb3 | 2009-09-10 17:45:00 +0000 | [diff] [blame] | 78 | |
| 79 | #ifdef SQLITE_DEBUG |
| 80 | GLOBAL(int, mutexIsInit) = 0; |
| 81 | #endif |
| 82 | |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 83 | return rc; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
| 88 | */ |
| 89 | sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
danielk1977 | bc10d77 | 2008-06-18 18:08:39 +0000 | [diff] [blame] | 90 | #ifndef SQLITE_OMIT_AUTOINIT |
drh | d42d0be | 2014-07-30 21:10:12 +0000 | [diff] [blame] | 91 | if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; |
mistachkin | 2d8ad51 | 2014-10-27 22:06:21 +0000 | [diff] [blame] | 92 | if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; |
danielk1977 | bc10d77 | 2008-06-18 18:08:39 +0000 | [diff] [blame] | 93 | #endif |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 94 | assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 95 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 96 | } |
| 97 | |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 98 | sqlite3_mutex *sqlite3MutexAlloc(int id){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 99 | if( !sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
drh | fe5bdb3 | 2009-09-10 17:45:00 +0000 | [diff] [blame] | 102 | assert( GLOBAL(int, mutexIsInit) ); |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 103 | assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 104 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 105 | } |
| 106 | |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 107 | /* |
| 108 | ** Free a dynamic mutex. |
| 109 | */ |
| 110 | void sqlite3_mutex_free(sqlite3_mutex *p){ |
| 111 | if( p ){ |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 112 | assert( sqlite3GlobalConfig.mutex.xMutexFree ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 113 | sqlite3GlobalConfig.mutex.xMutexFree(p); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | ** Obtain the mutex p. If some other thread already has the mutex, block |
| 119 | ** until it can be obtained. |
| 120 | */ |
| 121 | void sqlite3_mutex_enter(sqlite3_mutex *p){ |
| 122 | if( p ){ |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 123 | assert( sqlite3GlobalConfig.mutex.xMutexEnter ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 124 | sqlite3GlobalConfig.mutex.xMutexEnter(p); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another |
| 130 | ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. |
| 131 | */ |
| 132 | int sqlite3_mutex_try(sqlite3_mutex *p){ |
| 133 | int rc = SQLITE_OK; |
| 134 | if( p ){ |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 135 | assert( sqlite3GlobalConfig.mutex.xMutexTry ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 136 | return sqlite3GlobalConfig.mutex.xMutexTry(p); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 137 | } |
| 138 | return rc; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | ** The sqlite3_mutex_leave() routine exits a mutex that was previously |
| 143 | ** entered by the same thread. The behavior is undefined if the mutex |
| 144 | ** is not currently entered. If a NULL pointer is passed as an argument |
| 145 | ** this function is a no-op. |
| 146 | */ |
| 147 | void sqlite3_mutex_leave(sqlite3_mutex *p){ |
| 148 | if( p ){ |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 149 | assert( sqlite3GlobalConfig.mutex.xMutexLeave ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 150 | sqlite3GlobalConfig.mutex.xMutexLeave(p); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | #ifndef NDEBUG |
| 155 | /* |
| 156 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
| 157 | ** intended for use inside assert() statements. |
| 158 | */ |
| 159 | int sqlite3_mutex_held(sqlite3_mutex *p){ |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 160 | assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 161 | return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 162 | } |
| 163 | int sqlite3_mutex_notheld(sqlite3_mutex *p){ |
mistachkin | 04abf08 | 2015-09-12 18:57:45 +0000 | [diff] [blame] | 164 | assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld ); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 165 | return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); |
danielk1977 | 6d2ab0e | 2008-06-17 17:21:18 +0000 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | |
drh | e4c88c0 | 2012-01-04 12:57:45 +0000 | [diff] [blame] | 169 | #endif /* !defined(SQLITE_MUTEX_OMIT) */ |