1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sql/SQLite364/mutex.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,149 @@
1.4 +/*
1.5 +** 2007 August 14
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** This file contains the C functions that implement mutexes.
1.16 +**
1.17 +** This file contains code that is common across all mutex implementations.
1.18 +
1.19 +**
1.20 +** $Id: mutex.c,v 1.29 2008/10/07 15:25:48 drh Exp $
1.21 +*/
1.22 +#include "sqliteInt.h"
1.23 +
1.24 +#ifndef SQLITE_MUTEX_OMIT
1.25 +/*
1.26 +** Initialize the mutex system.
1.27 +*/
1.28 +int sqlite3MutexInit(void){
1.29 + int rc = SQLITE_OK;
1.30 + if( sqlite3GlobalConfig.bCoreMutex ){
1.31 + if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
1.32 + /* If the xMutexAlloc method has not been set, then the user did not
1.33 + ** install a mutex implementation via sqlite3_config() prior to
1.34 + ** sqlite3_initialize() being called. This block copies pointers to
1.35 + ** the default implementation into the sqlite3GlobalConfig structure.
1.36 + **
1.37 + ** The danger is that although sqlite3_config() is not a threadsafe
1.38 + ** API, sqlite3_initialize() is, and so multiple threads may be
1.39 + ** attempting to run this function simultaneously. To guard write
1.40 + ** access to the sqlite3GlobalConfig structure, the 'MASTER' static mutex
1.41 + ** is obtained before modifying it.
1.42 + */
1.43 + sqlite3_mutex_methods *p = sqlite3DefaultMutex();
1.44 + sqlite3_mutex *pMaster = 0;
1.45 +
1.46 + rc = p->xMutexInit();
1.47 + if( rc==SQLITE_OK ){
1.48 + pMaster = p->xMutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
1.49 + assert(pMaster);
1.50 + p->xMutexEnter(pMaster);
1.51 + assert( sqlite3GlobalConfig.mutex.xMutexAlloc==0
1.52 + || sqlite3GlobalConfig.mutex.xMutexAlloc==p->xMutexAlloc
1.53 + );
1.54 + if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
1.55 + sqlite3GlobalConfig.mutex = *p;
1.56 + }
1.57 + p->xMutexLeave(pMaster);
1.58 + }
1.59 + }else{
1.60 + rc = sqlite3GlobalConfig.mutex.xMutexInit();
1.61 + }
1.62 + }
1.63 +
1.64 + return rc;
1.65 +}
1.66 +
1.67 +/*
1.68 +** Shutdown the mutex system. This call frees resources allocated by
1.69 +** sqlite3MutexInit().
1.70 +*/
1.71 +int sqlite3MutexEnd(void){
1.72 + int rc = SQLITE_OK;
1.73 + if( sqlite3GlobalConfig.mutex.xMutexEnd ){
1.74 + rc = sqlite3GlobalConfig.mutex.xMutexEnd();
1.75 + }
1.76 + return rc;
1.77 +}
1.78 +
1.79 +/*
1.80 +** Retrieve a pointer to a static mutex or allocate a new dynamic one.
1.81 +*/
1.82 +sqlite3_mutex *sqlite3_mutex_alloc(int id){
1.83 +#ifndef SQLITE_OMIT_AUTOINIT
1.84 + if( sqlite3_initialize() ) return 0;
1.85 +#endif
1.86 + return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
1.87 +}
1.88 +
1.89 +sqlite3_mutex *sqlite3MutexAlloc(int id){
1.90 + if( !sqlite3GlobalConfig.bCoreMutex ){
1.91 + return 0;
1.92 + }
1.93 + return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
1.94 +}
1.95 +
1.96 +/*
1.97 +** Free a dynamic mutex.
1.98 +*/
1.99 +void sqlite3_mutex_free(sqlite3_mutex *p){
1.100 + if( p ){
1.101 + sqlite3GlobalConfig.mutex.xMutexFree(p);
1.102 + }
1.103 +}
1.104 +
1.105 +/*
1.106 +** Obtain the mutex p. If some other thread already has the mutex, block
1.107 +** until it can be obtained.
1.108 +*/
1.109 +void sqlite3_mutex_enter(sqlite3_mutex *p){
1.110 + if( p ){
1.111 + sqlite3GlobalConfig.mutex.xMutexEnter(p);
1.112 + }
1.113 +}
1.114 +
1.115 +/*
1.116 +** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
1.117 +** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
1.118 +*/
1.119 +int sqlite3_mutex_try(sqlite3_mutex *p){
1.120 + int rc = SQLITE_OK;
1.121 + if( p ){
1.122 + return sqlite3GlobalConfig.mutex.xMutexTry(p);
1.123 + }
1.124 + return rc;
1.125 +}
1.126 +
1.127 +/*
1.128 +** The sqlite3_mutex_leave() routine exits a mutex that was previously
1.129 +** entered by the same thread. The behavior is undefined if the mutex
1.130 +** is not currently entered. If a NULL pointer is passed as an argument
1.131 +** this function is a no-op.
1.132 +*/
1.133 +void sqlite3_mutex_leave(sqlite3_mutex *p){
1.134 + if( p ){
1.135 + sqlite3GlobalConfig.mutex.xMutexLeave(p);
1.136 + }
1.137 +}
1.138 +
1.139 +#ifndef NDEBUG
1.140 +/*
1.141 +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
1.142 +** intended for use inside assert() statements.
1.143 +*/
1.144 +int sqlite3_mutex_held(sqlite3_mutex *p){
1.145 + return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
1.146 +}
1.147 +int sqlite3_mutex_notheld(sqlite3_mutex *p){
1.148 + return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
1.149 +}
1.150 +#endif
1.151 +
1.152 +#endif /* SQLITE_OMIT_MUTEX */