blob: 6f1bc9767db8e1a3ce002bcc10b77c72c9e6f51c [file] [log] [blame]
drh90f6a5b2007-08-15 13:04:54 +00001/*
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*************************************************************************
drh437b9012007-08-28 16:34:42 +000012** This file contains the C functions that implement mutexes.
drh90f6a5b2007-08-15 13:04:54 +000013**
drh18472fa2008-10-07 15:25:48 +000014** This file contains code that is common across all mutex implementations.
drh90f6a5b2007-08-15 13:04:54 +000015*/
16#include "sqliteInt.h"
17
drhd0bc5d22009-09-10 22:30:53 +000018#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
drhfe5bdb32009-09-10 17:45:00 +000019/*
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*/
24static SQLITE_WSD int mutexIsInit = 0;
mistachkin04abf082015-09-12 18:57:45 +000025#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
drhfe5bdb32009-09-10 17:45:00 +000026
27
drh18472fa2008-10-07 15:25:48 +000028#ifndef SQLITE_MUTEX_OMIT
danielk19776d2ab0e2008-06-17 17:21:18 +000029/*
30** Initialize the mutex system.
31*/
danielk1977d0251742008-06-18 18:57:42 +000032int sqlite3MutexInit(void){
danielk197759f8c082008-06-18 17:09:10 +000033 int rc = SQLITE_OK;
drh92d76522010-05-05 00:05:24 +000034 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 */
dan558814f2010-06-02 05:53:53 +000040 sqlite3_mutex_methods const *pFrom;
drh92d76522010-05-05 00:05:24 +000041 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
drhe64ca7b2009-07-16 18:21:17 +000042
drh92d76522010-05-05 00:05:24 +000043 if( sqlite3GlobalConfig.bCoreMutex ){
44 pFrom = sqlite3DefaultMutex();
45 }else{
46 pFrom = sqlite3NoopMutex();
danielk19776d2ab0e2008-06-17 17:21:18 +000047 }
drhf5ed7ad2015-06-15 14:43:25 +000048 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;
drh6081c1d2015-09-06 02:51:04 +000056 sqlite3MemoryBarrier();
drh92d76522010-05-05 00:05:24 +000057 pTo->xMutexAlloc = pFrom->xMutexAlloc;
danielk19776d2ab0e2008-06-17 17:21:18 +000058 }
mistachkin04abf082015-09-12 18:57:45 +000059 assert( sqlite3GlobalConfig.mutex.xMutexInit );
drh92d76522010-05-05 00:05:24 +000060 rc = sqlite3GlobalConfig.mutex.xMutexInit();
danielk19776d2ab0e2008-06-17 17:21:18 +000061
drhfe5bdb32009-09-10 17:45:00 +000062#ifdef SQLITE_DEBUG
63 GLOBAL(int, mutexIsInit) = 1;
64#endif
65
danielk19776d2ab0e2008-06-17 17:21:18 +000066 return rc;
67}
68
69/*
70** Shutdown the mutex system. This call frees resources allocated by
danielk1977d0251742008-06-18 18:57:42 +000071** sqlite3MutexInit().
danielk19776d2ab0e2008-06-17 17:21:18 +000072*/
danielk1977d0251742008-06-18 18:57:42 +000073int sqlite3MutexEnd(void){
danielk19776d2ab0e2008-06-17 17:21:18 +000074 int rc = SQLITE_OK;
danielk19770a549072009-02-17 16:29:10 +000075 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
76 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
77 }
drhfe5bdb32009-09-10 17:45:00 +000078
79#ifdef SQLITE_DEBUG
80 GLOBAL(int, mutexIsInit) = 0;
81#endif
82
danielk19776d2ab0e2008-06-17 17:21:18 +000083 return rc;
84}
85
86/*
87** Retrieve a pointer to a static mutex or allocate a new dynamic one.
88*/
89sqlite3_mutex *sqlite3_mutex_alloc(int id){
danielk1977bc10d772008-06-18 18:08:39 +000090#ifndef SQLITE_OMIT_AUTOINIT
drhd42d0be2014-07-30 21:10:12 +000091 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
mistachkin2d8ad512014-10-27 22:06:21 +000092 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
danielk1977bc10d772008-06-18 18:08:39 +000093#endif
mistachkin04abf082015-09-12 18:57:45 +000094 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
danielk1977075c23a2008-09-01 18:34:20 +000095 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
danielk19776d2ab0e2008-06-17 17:21:18 +000096}
97
danielk197759f8c082008-06-18 17:09:10 +000098sqlite3_mutex *sqlite3MutexAlloc(int id){
danielk1977075c23a2008-09-01 18:34:20 +000099 if( !sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000100 return 0;
101 }
drhfe5bdb32009-09-10 17:45:00 +0000102 assert( GLOBAL(int, mutexIsInit) );
mistachkin04abf082015-09-12 18:57:45 +0000103 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
danielk1977075c23a2008-09-01 18:34:20 +0000104 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
danielk197759f8c082008-06-18 17:09:10 +0000105}
106
danielk19776d2ab0e2008-06-17 17:21:18 +0000107/*
108** Free a dynamic mutex.
109*/
110void sqlite3_mutex_free(sqlite3_mutex *p){
111 if( p ){
mistachkin04abf082015-09-12 18:57:45 +0000112 assert( sqlite3GlobalConfig.mutex.xMutexFree );
danielk1977075c23a2008-09-01 18:34:20 +0000113 sqlite3GlobalConfig.mutex.xMutexFree(p);
danielk19776d2ab0e2008-06-17 17:21:18 +0000114 }
115}
116
117/*
118** Obtain the mutex p. If some other thread already has the mutex, block
119** until it can be obtained.
120*/
121void sqlite3_mutex_enter(sqlite3_mutex *p){
122 if( p ){
mistachkin04abf082015-09-12 18:57:45 +0000123 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
danielk1977075c23a2008-09-01 18:34:20 +0000124 sqlite3GlobalConfig.mutex.xMutexEnter(p);
danielk19776d2ab0e2008-06-17 17:21:18 +0000125 }
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*/
132int sqlite3_mutex_try(sqlite3_mutex *p){
133 int rc = SQLITE_OK;
134 if( p ){
mistachkin04abf082015-09-12 18:57:45 +0000135 assert( sqlite3GlobalConfig.mutex.xMutexTry );
danielk1977075c23a2008-09-01 18:34:20 +0000136 return sqlite3GlobalConfig.mutex.xMutexTry(p);
danielk19776d2ab0e2008-06-17 17:21:18 +0000137 }
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*/
147void sqlite3_mutex_leave(sqlite3_mutex *p){
148 if( p ){
mistachkin04abf082015-09-12 18:57:45 +0000149 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
danielk1977075c23a2008-09-01 18:34:20 +0000150 sqlite3GlobalConfig.mutex.xMutexLeave(p);
danielk19776d2ab0e2008-06-17 17:21:18 +0000151 }
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*/
159int sqlite3_mutex_held(sqlite3_mutex *p){
mistachkin04abf082015-09-12 18:57:45 +0000160 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
danielk1977075c23a2008-09-01 18:34:20 +0000161 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
danielk19776d2ab0e2008-06-17 17:21:18 +0000162}
163int sqlite3_mutex_notheld(sqlite3_mutex *p){
mistachkin04abf082015-09-12 18:57:45 +0000164 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
danielk1977075c23a2008-09-01 18:34:20 +0000165 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
danielk19776d2ab0e2008-06-17 17:21:18 +0000166}
167#endif
168
drhe4c88c02012-01-04 12:57:45 +0000169#endif /* !defined(SQLITE_MUTEX_OMIT) */